Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for supplying custom encoders to ScalaPB ones #15

Open
s5bug opened this issue May 13, 2019 · 1 comment
Open

Allow for supplying custom encoders to ScalaPB ones #15

s5bug opened this issue May 13, 2019 · 1 comment

Comments

@s5bug
Copy link

s5bug commented May 13, 2019

For example, the API I'm using requires me to send Map[String, A] as {"key": $string, "value": $a} and not {$string: $a}. I've created a custom Map[String, A: Encoder] to do this but it isn't picked up by scalapb-circe.

@s5bug s5bug closed this as completed May 13, 2019
@s5bug s5bug reopened this May 13, 2019
@s5bug
Copy link
Author

s5bug commented May 17, 2019

Minimal case:

case class KeyValuePair[T: Encoder](key: String, value: T)

implicit def encodeKVPair[T: Encoder]: Encoder[KeyValuePair[T]] = (a: KeyValuePair[T]) => JsonObject("key" -> a.key.asJson, "value" -> a.value.asJson).asJson

implicit def customMapEncoder[T: Encoder]: Encoder[Map[String, T]] = (a: Map[String, T]) => {
  a.toList.map { case (k, v) => KeyValuePair(k, v) }.asJson
}

def pack[I <: GeneratedMessage with Message[I]](i: I): MessageData = JsonFormat.toJson(i)

Call pack with some sort of pb message that has a Map value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant