Skip to content

Commit

Permalink
make json value decoding work for any json
Browse files Browse the repository at this point in the history
  • Loading branch information
aewering committed Feb 23, 2024
1 parent eda2a5f commit fc123c7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
48 changes: 30 additions & 18 deletions src/Generator/OneOf.elm
Original file line number Diff line number Diff line change
Expand Up @@ -197,26 +197,38 @@ toAST opts { oneOfName, options } =

( optModName, optDataType ) =
Mapper.Name.externalize o.dataType

optionFieldDecoder =
case o.fieldType of
Primitive p _ ->
Meta.JsonDecode.forPrimitive p

Embedded e ->
Generator.Message.embeddedJsonDecoder e

Enumeration e ->
C.fqFun (e.package ++ [ e.name ]) <|
Common.jsonDecoderName e.name
in
wrapEmbeddedWithLazy <|
C.apply
[ Meta.JsonDecode.map
, C.parens (C.applyBinOp (C.fqVal optModName optDataType) C.composer Meta.Basics.just)
, C.apply
[ Meta.JsonDecode.field
, C.string o.fieldName
, case o.fieldType of
Primitive p _ ->
Meta.JsonDecode.forPrimitive p

Embedded e ->
Generator.Message.embeddedJsonDecoder e

Enumeration e ->
C.fqFun (e.package ++ [ e.name ]) <|
Common.jsonDecoderName e.name
]
]
case oneOfName of
"Proto__Google__Protobuf__Value__Kind__Kind" ->
C.apply
[ Meta.JsonDecode.map
, C.parens (C.applyBinOp (C.fqVal optModName optDataType) C.composer Meta.Basics.just)
, optionFieldDecoder
]

_ ->
C.apply
[ Meta.JsonDecode.map
, C.parens (C.applyBinOp (C.fqVal optModName optDataType) C.composer Meta.Basics.just)
, C.apply
[ Meta.JsonDecode.field
, C.string o.fieldName
, optionFieldDecoder
]
]
)
options
++ [ C.apply [ C.fqVal Meta.JsonDecode.moduleName "succeed", Meta.Basics.nothing ] ]
Expand Down
23 changes: 7 additions & 16 deletions src/Proto/Google/Protobuf/Internals_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -412,30 +412,21 @@ jsonDecodeProto__Google__Protobuf__Value__Kind__Kind =
Json.Decode.oneOf
[ Json.Decode.map
(Proto.Google.Protobuf.Value.Kind.NullValue >> Just)
(Json.Decode.field "nullValue" Proto.Google.Protobuf.NullValue.jsonDecodeNullValue)
, Json.Decode.map
(Proto.Google.Protobuf.Value.Kind.NumberValue >> Just)
(Json.Decode.field "numberValue" Json.Decode.float)
, Json.Decode.map
(Proto.Google.Protobuf.Value.Kind.StringValue >> Just)
(Json.Decode.field "stringValue" Json.Decode.string)
, Json.Decode.map
(Proto.Google.Protobuf.Value.Kind.BoolValue >> Just)
(Json.Decode.field "boolValue" Json.Decode.bool)
Proto.Google.Protobuf.NullValue.jsonDecodeNullValue
, Json.Decode.map (Proto.Google.Protobuf.Value.Kind.NumberValue >> Just) Json.Decode.float
, Json.Decode.map (Proto.Google.Protobuf.Value.Kind.StringValue >> Just) Json.Decode.string
, Json.Decode.map (Proto.Google.Protobuf.Value.Kind.BoolValue >> Just) Json.Decode.bool
, Json.Decode.lazy <|
\_ ->
Json.Decode.map
(Proto.Google.Protobuf.Value.Kind.StructValue >> Just)
(Json.Decode.field "structValue" jsonDecodeProto__Google__Protobuf__Struct)
jsonDecodeProto__Google__Protobuf__Struct
, Json.Decode.lazy <|
\_ ->
Json.Decode.map
(Proto.Google.Protobuf.Value.Kind.ListValue >> Just)
(Json.Decode.field
"listValue"
(Json.Decode.map Proto__Google__Protobuf__ListValue_ <|
Json.Decode.lazy <| \_ -> jsonDecodeProto__Google__Protobuf__ListValue
)
(Json.Decode.map Proto__Google__Protobuf__ListValue_ <|
Json.Decode.lazy <| \_ -> jsonDecodeProto__Google__Protobuf__ListValue
)
, Json.Decode.succeed Nothing
]
Expand Down

0 comments on commit fc123c7

Please sign in to comment.