Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jan 26 21:07
    scala-steward synchronize #139
  • Jan 26 14:33

    mpilquist on v1.11.0

    (compare)

  • Jan 26 14:32

    mpilquist on 1.11.x

    Setting version to 1.11.0 Updated version to 1.11.1-SNAPS… (compare)

  • Jan 25 10:28
    billpcs starred scodec/scodec
  • Jan 22 22:01
    benhanna starred scodec/scodec
  • Jan 21 20:36
    scala-steward opened #139
  • Jan 21 14:51
    mpilquist closed #130
  • Jan 21 14:48
    mpilquist closed #119
  • Jan 21 14:48
    mpilquist commented #119
  • Jan 21 14:48

    mpilquist on 1.11.x

    Scala 2.13.0-M4 Merge branch 'series/1.10.x' in… Upgraded to latest scala-collec… and 2 more (compare)

  • Jan 21 14:48
    mpilquist closed #138
  • Jan 21 14:40
    mpilquist opened #138
  • Jan 21 14:37

    mpilquist on 1.11.x

    Created 1.11 branch (compare)

  • Jan 21 14:31

    mpilquist on xuwei-k

    Upgraded to 2.13.0-M5 (compare)

  • Jan 21 14:19

    mpilquist on xuwei-k

    Scala 2.13.0-M4 Merge branch 'series/1.10.x' in… Upgraded to latest scala-collec… (compare)

  • Jan 21 14:03

    mpilquist on 1.10.x

    Update sbt-scalajs, scalajs-com… Merge pull request #129 from sc… (compare)

  • Jan 21 14:03
    mpilquist closed #129
  • Jan 21 14:03
    mpilquist closed #126
  • Jan 21 14:03

    mpilquist on 1.10.x

    Pad-left on pbcd codec. Fixes #… Using padLeft in pbcd instead o… Merge pull request #133 from lJ… (compare)

  • Jan 21 14:03
    mpilquist closed #133
Igal Tabachnik
@hmemcpy
hmm... this might be a good direction to be honest
it's a choice
ok, I'll play with it :D
btw, found this cheatsheet, it's very helpful https://github.com/jrudolph/scodec-cheatsheet/blob/master/README.md
Paul Snively
@paul-snively
Cool!
mark lister
@marklister
@tpolecat is this no good?
scala> case class Foo(i:Int,j:Int)
defined class Foo

scala> (uint8~uint8).decode(hex"0102".bits).require.map(Foo.tupled)
res21: scodec.DecodeResult[Foo] = DecodeResult(Foo(1,2),BitVector(empty))
Rob Norris
@tpolecat
Yeah I need to go the other way so I could do Foo.apply and then x => Foo.unapply(x).get but I don't like that last bit. But it also doesn't work with a ~ b ~ c because the type is ((A, B), C) and not (A, B, C)
mark lister
@marklister
And flattenLeftPairs produces an HList... mmmm
Paul Snively
@paul-snively
Yeah, isn’t flattening a nested tuple a Shapeless example?
Diego E. Alonso Blas
@diesalbla
Good morning.
Has anything built some type-class based library for decoding/encoding data into scodec-byte-vectors? Bit like what circe does with Json
Michael Pilquist
@mpilquist
No cursors per se but Encoder/Decoder/Codec along with opt-in derivation
Dinesh Narayanan
@ndchandar
Hello,
I am a scodec beginner. I need some help on how to possibly implement myPacketCodec1/myPacketCodec2 in this example https://gist.github.com/ndchandar/a3bb3419bcce6b0c1b143db7f37f07be
Basically i am not able to visualize how to encode case classes that are dependent on other product types
Igal Tabachnik
@hmemcpy
Evening folks! I have a reply that supposed to return the number of items to process, but sometimes returns -1
I'm doing something like listOfN(size, BulkReply.decoderNoTag).as[MultiBulkReply], and it works great. How can I handle the case where size decodes -1? I would like to create a MultiBulkReply with an empty list
case class MultiBulkReply(messages: List[BulkReply]) is its definition
Diego E. Alonso Blas
@diesalbla
Good morning.
I am having problems to find a scodec binary for Scala 2.13. Are there any releases planned for this?
Michael Pilquist
@mpilquist
@diesalbla scodec-core 1.11.4 is cross published for 2.11, 2.12, 2.13
I need to update the scodec website with the recent releases, or perhaps just remove that page as it’s always out of date
Diego E. Alonso Blas
@diesalbla
Microsites are not too bad, and are included in the same repo as the source code :)
Ackerley Tng
@ackerleytng
hi! I'm trying to decode a vector of objects, but i want to stop early if a decoded field matches some value. any tips for implementing something like that?
Tristan Lohman
@gatorcse
decode as a Stream of those objects, do .takeWhile(_.someValue == terminalCondition).to[Vector]
@ackerleytng ☝️
actually you’ll need a call to .compile before .to[Vector]
Ackerley Tng
@ackerleytng
Thanks! The above wouldn't work though, it'll not even take one, since my first value isn't the condition. Is there a take while including or something
Diego E. Alonso Blas
@diesalbla
@ackerleytng There is a some takeThrough methods, that take the greatest prefix where all elements meet the condition, in addition of the first element that doesn't.
So for you it may be something like takeThrough ( _.yourField =!= endMark)
Paul Snively
@paul-snively
This isn’t really using scodec the way scodec is designed, for lack of a better term.
Paul Snively
@paul-snively
It looks like you expect a 16-bit value that is the size of the rest of the vector, followed by that many byres that must be either a “letter” or a “digit.”
You can express that with scodec combinators.
matdtr
@matdtr
How can i do that? Can you show me an example of scodec combinators?
Paul Snively
@paul-snively
Paul Snively
@paul-snively
@matdtr: OK, so, to try to answer your question:
Paul Snively
@paul-snively
import scodec._, scodec.codecs._, scodec.bits._

val digit = byte.exmap ( { b => val c = b.toChar; if (c >= '0' && c <= '9') Attempt.successful(c) else Attempt.failure(Err(s"'$c' is not a digit")) }, { (c: Char) => Attempt.successful(c.toByte) } )

val asciiLetter = byte.exmap ( { b => val c = b.toChar; if (c >= 'A' && c <= 'Z') Attempt.successful(c) else Attempt.failure(Err(s"'$c' is not an ASCII letter")) }, { (c: Char) => Attempt.successful(c.toByte) } )

val letterOrDigit = choice(asciiLetter, digit)

val letterOrDigit16String = variableSizeBytes(uint16, list(letterOrDigit)).exmap((l: List[Char]) => Attempt.successful(l.mkString), (s: String) => Attempt.successful(s.toList))
Paul Snively
@paul-snively
So:
val goodBits = (uint16 ~ ascii).encode(goodExample.size, goodExample).toOption.get
val badBits = (uint16 ~ ascii).encode(badExample.size, badExample).toOption.get

letterOrDigit16String.decode(goodBits)
res45: Attempt[DecodeResult[String]] = Successful(DecodeResult("F9ZG289A", Bytes(Chunk(View(scodec.bits.ByteVector$AtEmpty$@515ab3f2, 0L, 0L)), 0L)))

letterOrDigit16String.decode(badBits)
res46: Attempt[DecodeResult[String]] = Failure(Composite(List(General("'@' is not an ASCII letter", List()), General("'@' is not a digit", List())), List("4")))
Refactored codecs:
import scodec._, scodec.codecs._, scodec.bits._

val digit = byte.exmap ( { b => val c = b.toChar; if (c >= '0' && c <= '9') Attempt.successful(c) else Attempt.failure(Err(s"'$c' is not a digit")) }, { (c: Char) => Attempt.successful(c.toByte) } )

val asciiLetter = byte.exmap ( { b => val c = b.toChar; if (c >= 'A' && c <= 'Z') Attempt.successful(c) else Attempt.failure(Err(s"'$c' is not an ASCII letter")) }, { (c: Char) => Attempt.successful(c.toByte) } )

val letterOrDigit = choice(asciiLetter, digit)

val letterOrDigitString = list(letterOrDigit).exmap((l: List[Char]) => Attempt.successful(l.mkString), (s: String) => Attempt.successful(s.toList))

val letterOrDigit16String = variableSizeBytes(uint16, letterOrDigitString)
Paul Snively
@paul-snively
@matdtr: So the key concept here is that a "combinator" is a function that only takes other functions as arguments and only returns a function as its result.
@matdtr: scodec is a "combinator library," meaning a library of combinators you are expected both to use and to extend by writing your own combinators.
Paul Snively
@paul-snively
@matdtr: As you can see above, one way to write a new combinator is to exmap over an existing one, like byte. digit and asciiLetter both reflect that there are fewer of either of those than bytes, so going from Byte to either can fail. The "e" in exmap means "error," and the "x" means "across" or "bidirectional," so exmap takes two functions that return Attempts, transforming the Codec[Byte] to a Codec[Char] that can fail when the Char isn't in range.
@matdtr: letterOrDigit uses the choice combinator to say "either asciiLetter or digit is good."
@matdtr: letterOrDigitString uses the list combinator to round-trip between a List[Char] and a String with the letterOrDigit combinator.
Paul Snively
@paul-snively
@matdtr: Finally, letterOrDigit16String says "first read 16 bytes as an unsigned integer, then read that many more bytes as a letterOrDigitString."
@matdtr: And that's it.
@matdtr: You can see from my quick example above that one nice thing about it is that when you give it an invalid BitVector, the error tells you both why the asciiLetter and the digit combinators failed on it.
@matdtr: And this a nice example of functional programming. All we're doing is mapping over values and if a condition is true, returning one value, and if it's false, returning a different value.
Paul Snively
@paul-snively
@matdtr: There's no throwing or assigning to vars or any of that crazy error-prone stuff.
Ackerley Tng
@ackerleytng
@diesalbla is this part of scodec or some other library?
Diego E. Alonso Blas
@diesalbla
Hmmm ah, I was thinking of fs2.Stream. Sorry.