From 0103b0ebabcf70630bbbc25c91ffcacf5b29f238 Mon Sep 17 00:00:00 2001 From: aewering Date: Sun, 7 Jan 2024 16:46:04 +0100 Subject: [PATCH] Fix issue with int64map json code generation --- src/Generator/Message.elm | 7 +--- src/Meta/JsonEncode.elm | 87 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/src/Generator/Message.elm b/src/Generator/Message.elm index 119bcbc..1f72e6e 100644 --- a/src/Generator/Message.elm +++ b/src/Generator/Message.elm @@ -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." diff --git a/src/Meta/JsonEncode.elm b/src/Meta/JsonEncode.elm index d11d94b..f6a3c76 100644 --- a/src/Meta/JsonEncode.elm +++ b/src/Meta/JsonEncode.elm @@ -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(..)) @@ -70,7 +69,7 @@ forPrimitive prim = int64 Prim_Int64 UInt -> - int64 + uint64 Prim_Int64 Fixed -> int64 @@ -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" @@ -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