Skip to content

Commit

Permalink
Fix issue with int64map json code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
aewering committed Jan 7, 2024
1 parent bb3ec0e commit 0103b0e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
7 changes: 2 additions & 5 deletions src/Generator/Message.elm
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,8 @@ fieldTypeToEncoder cardinality fieldType =
fieldTypeToJsonMapKey : FieldType -> C.Expression
fieldTypeToJsonMapKey fieldType =
case fieldType of
Primitive Prim_String _ ->
Meta.Basics.identity

Primitive (Prim_Int32 _) _ ->
C.fqFun [ "String" ] "fromInt"
Primitive primitive _ ->
Meta.JsonEncode.primitiveToMapKey primitive

_ ->
C.string "ERROR: This should not happen. Map keys are supposed to be primitive only."
Expand Down
87 changes: 83 additions & 4 deletions src/Meta/JsonEncode.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Meta.JsonEncode exposing (..)

import Elm.CodeGen as C exposing (Expression, ModuleName, TypeAnnotation)
import Json.Encode
import Model exposing (IntFlavor(..), Primitive(..))


Expand Down Expand Up @@ -70,7 +69,7 @@ forPrimitive prim =
int64

Prim_Int64 UInt ->
int64
uint64

Prim_Int64 Fixed ->
int64
Expand All @@ -94,6 +93,55 @@ forPrimitive prim =
float


primitiveToMapKey : Primitive -> Expression
primitiveToMapKey prim =
case prim of
Prim_Int32 Int_ ->
int32_toMapKey

Prim_Int32 SInt ->
int32_toMapKey

Prim_Int32 UInt ->
int32_toMapKey

Prim_Int32 Fixed ->
int32_toMapKey

Prim_Int32 SFixed ->
int32_toMapKey

Prim_Int64 Int_ ->
int64_toMapKey

Prim_Int64 SInt ->
int64_toMapKey

Prim_Int64 UInt ->
uint64_toMapKey

Prim_Int64 Fixed ->
int64_toMapKey

Prim_Int64 SFixed ->
int64_toMapKey

Prim_String ->
C.fun "identity"

Prim_Bool ->
C.string "ERROR: Bool is not a valid key for a protobuf map!"

Prim_Float ->
C.string "ERROR: Float is not a valid key for a protobuf map!"

Prim_Bytes ->
C.string "ERROR: Bytes is not a valid key for a protobuf map!"

Prim_Double ->
C.string "ERROR: Double is not a valid key for a protobuf map!"


int32 : Expression
int32 =
C.fqFun moduleName "int"
Expand All @@ -116,9 +164,40 @@ bytes =

int64 : Expression
int64 =
C.parens <| C.applyBinOp (C.fqFun [ "Protobuf", "Utils", "Int64" ] "toSignedString") C.composer string
C.parens <| C.applyBinOp int64_toString C.composer string


int32_toMapKey : Expression
int32_toMapKey =
C.fqFun [ "String" ] "fromInt"


int64_toMapKey : Expression
int64_toMapKey =
C.parens
(C.lambda [ C.tuplePattern [ C.varPattern "upper", C.varPattern "lower" ] ] <|
C.pipe (C.apply [ C.fqFun [ "Protobuf", "Types", "Int64" ] "fromInts", C.val "upper", C.val "lower" ]) [ int64_toString ]
)


uint64_toMapKey : Expression
uint64_toMapKey =
C.parens
(C.lambda [ C.tuplePattern [ C.varPattern "upper", C.varPattern "lower" ] ] <|
C.pipe (C.apply [ C.fqFun [ "Protobuf", "Types", "Int64" ] "fromInts", C.val "upper", C.val "lower" ]) [ uint64_toString ]
)


int64_toString : Expression
int64_toString =
C.fqFun [ "Protobuf", "Utils", "Int64" ] "toSignedString"


uint64_toString : Expression
uint64_toString =
C.fqFun [ "Protobuf", "Utils", "Int64" ] "toUnsignedString"


uint64 : Expression
uint64 =
C.parens <| C.applyBinOp (C.fqFun [ "Protobuf", "Utils", "Int64" ] "toUnsignedString") C.composer string
C.parens <| C.applyBinOp uint64_toString C.composer string

0 comments on commit 0103b0e

Please sign in to comment.