Skip to content

Commit fc123c7

Browse files
author
aewering
committed
make json value decoding work for any json
1 parent eda2a5f commit fc123c7

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

src/Generator/OneOf.elm

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,26 +197,38 @@ toAST opts { oneOfName, options } =
197197

198198
( optModName, optDataType ) =
199199
Mapper.Name.externalize o.dataType
200+
201+
optionFieldDecoder =
202+
case o.fieldType of
203+
Primitive p _ ->
204+
Meta.JsonDecode.forPrimitive p
205+
206+
Embedded e ->
207+
Generator.Message.embeddedJsonDecoder e
208+
209+
Enumeration e ->
210+
C.fqFun (e.package ++ [ e.name ]) <|
211+
Common.jsonDecoderName e.name
200212
in
201213
wrapEmbeddedWithLazy <|
202-
C.apply
203-
[ Meta.JsonDecode.map
204-
, C.parens (C.applyBinOp (C.fqVal optModName optDataType) C.composer Meta.Basics.just)
205-
, C.apply
206-
[ Meta.JsonDecode.field
207-
, C.string o.fieldName
208-
, case o.fieldType of
209-
Primitive p _ ->
210-
Meta.JsonDecode.forPrimitive p
211-
212-
Embedded e ->
213-
Generator.Message.embeddedJsonDecoder e
214-
215-
Enumeration e ->
216-
C.fqFun (e.package ++ [ e.name ]) <|
217-
Common.jsonDecoderName e.name
218-
]
219-
]
214+
case oneOfName of
215+
"Proto__Google__Protobuf__Value__Kind__Kind" ->
216+
C.apply
217+
[ Meta.JsonDecode.map
218+
, C.parens (C.applyBinOp (C.fqVal optModName optDataType) C.composer Meta.Basics.just)
219+
, optionFieldDecoder
220+
]
221+
222+
_ ->
223+
C.apply
224+
[ Meta.JsonDecode.map
225+
, C.parens (C.applyBinOp (C.fqVal optModName optDataType) C.composer Meta.Basics.just)
226+
, C.apply
227+
[ Meta.JsonDecode.field
228+
, C.string o.fieldName
229+
, optionFieldDecoder
230+
]
231+
]
220232
)
221233
options
222234
++ [ C.apply [ C.fqVal Meta.JsonDecode.moduleName "succeed", Meta.Basics.nothing ] ]

src/Proto/Google/Protobuf/Internals_.elm

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -412,30 +412,21 @@ jsonDecodeProto__Google__Protobuf__Value__Kind__Kind =
412412
Json.Decode.oneOf
413413
[ Json.Decode.map
414414
(Proto.Google.Protobuf.Value.Kind.NullValue >> Just)
415-
(Json.Decode.field "nullValue" Proto.Google.Protobuf.NullValue.jsonDecodeNullValue)
416-
, Json.Decode.map
417-
(Proto.Google.Protobuf.Value.Kind.NumberValue >> Just)
418-
(Json.Decode.field "numberValue" Json.Decode.float)
419-
, Json.Decode.map
420-
(Proto.Google.Protobuf.Value.Kind.StringValue >> Just)
421-
(Json.Decode.field "stringValue" Json.Decode.string)
422-
, Json.Decode.map
423-
(Proto.Google.Protobuf.Value.Kind.BoolValue >> Just)
424-
(Json.Decode.field "boolValue" Json.Decode.bool)
415+
Proto.Google.Protobuf.NullValue.jsonDecodeNullValue
416+
, Json.Decode.map (Proto.Google.Protobuf.Value.Kind.NumberValue >> Just) Json.Decode.float
417+
, Json.Decode.map (Proto.Google.Protobuf.Value.Kind.StringValue >> Just) Json.Decode.string
418+
, Json.Decode.map (Proto.Google.Protobuf.Value.Kind.BoolValue >> Just) Json.Decode.bool
425419
, Json.Decode.lazy <|
426420
\_ ->
427421
Json.Decode.map
428422
(Proto.Google.Protobuf.Value.Kind.StructValue >> Just)
429-
(Json.Decode.field "structValue" jsonDecodeProto__Google__Protobuf__Struct)
423+
jsonDecodeProto__Google__Protobuf__Struct
430424
, Json.Decode.lazy <|
431425
\_ ->
432426
Json.Decode.map
433427
(Proto.Google.Protobuf.Value.Kind.ListValue >> Just)
434-
(Json.Decode.field
435-
"listValue"
436-
(Json.Decode.map Proto__Google__Protobuf__ListValue_ <|
437-
Json.Decode.lazy <| \_ -> jsonDecodeProto__Google__Protobuf__ListValue
438-
)
428+
(Json.Decode.map Proto__Google__Protobuf__ListValue_ <|
429+
Json.Decode.lazy <| \_ -> jsonDecodeProto__Google__Protobuf__ListValue
439430
)
440431
, Json.Decode.succeed Nothing
441432
]

0 commit comments

Comments
 (0)