Skip to content

Commit 4b2511d

Browse files
fridewaldlpil
authored andcommitted
fix issues with nullable types #10
* wrap nullable types in Option * use correct decoder function
1 parent 4d927b5 commit 4b2511d

28 files changed

+145
-55
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v1.1.3 - tba
44

55
- Fixed typo in helper function
6+
- Fixed issues with nullable types
67

78
## v1.1.2 - 2025-01-08
89

birdie_snapshots/codegen_discriminator_as_elements_test.accepted

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
version: 1.2.1
2+
version: 1.2.5
33
title: codegen_discriminator_as_elements_test
44
file: ./test/json_typedef_test.gleam
55
test_name: codegen_discriminator_as_elements_test
@@ -58,7 +58,7 @@ import gleam/option
5858
pub type DataElement {
5959
DataElementDown(
6060
depth: Float,
61-
note: String,
61+
note: option.Option(String),
6262
)
6363
DataElementUp(
6464
height: Int,
@@ -80,7 +80,7 @@ pub fn data_decoder() -> decode.Decoder(List(DataElement)) {
8080
DataElementDown(depth:, note:)
8181
})
8282
|> decode.field("depth", decode.float)
83-
|> decode.field("note", decode.nullable(decode.string))
83+
|> decode.field("note", decode.optional(decode.string))
8484
_ -> decode.fail("DataElement")
8585
}
8686
}))

birdie_snapshots/codegen_discriminator_nullable_as_elements_test.accepted

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
version: 1.2.1
2+
version: 1.2.5
33
title: codegen_discriminator_nullable_as_elements_test
44
file: ./test/json_typedef_test.gleam
55
test_name: codegen_discriminator_nullable_as_elements_test
@@ -58,7 +58,7 @@ import gleam/option
5858
pub type DataElement {
5959
DataElementDown(
6060
depth: Float,
61-
note: String,
61+
note: option.Option(String),
6262
)
6363
DataElementUp(
6464
height: Int,
@@ -80,7 +80,7 @@ pub fn data_decoder() -> decode.Decoder(List(option.Option(DataElement))) {
8080
DataElementDown(depth:, note:)
8181
})
8282
|> decode.field("depth", decode.float)
83-
|> decode.field("note", decode.nullable(decode.string))
83+
|> decode.field("note", decode.optional(decode.string))
8484
_ -> decode.fail("DataElement")
8585
}
8686
}))

birdie_snapshots/codegen_discriminator_nullable_test.accepted

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
version: 1.2.1
2+
version: 1.2.5
33
title: codegen_discriminator_nullable_test
44
file: ./test/json_typedef_test.gleam
55
test_name: codegen_discriminator_nullable_test
@@ -54,7 +54,7 @@ import gleam/option
5454
pub type Data {
5555
DataDown(
5656
depth: Float,
57-
note: String,
57+
note: option.Option(String),
5858
)
5959
DataUp(
6060
height: Int,
@@ -76,7 +76,7 @@ pub fn data_decoder() -> decode.Decoder(option.Option(Data)) {
7676
DataDown(depth:, note:)
7777
})
7878
|> decode.field("depth", decode.float)
79-
|> decode.field("note", decode.nullable(decode.string))
79+
|> decode.field("note", decode.optional(decode.string))
8080
_ -> decode.fail("Data")
8181
}
8282
})

birdie_snapshots/codegen_discriminator_test.accepted

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
version: 1.2.1
2+
version: 1.2.5
33
title: codegen_discriminator_test
44
file: ./test/json_typedef_test.gleam
55
test_name: codegen_discriminator_test
@@ -54,7 +54,7 @@ import gleam/option
5454
pub type Data {
5555
DataDown(
5656
depth: Float,
57-
note: String,
57+
note: option.Option(String),
5858
)
5959
DataUp(
6060
height: Int,
@@ -76,7 +76,7 @@ pub fn data_decoder() -> decode.Decoder(Data) {
7676
DataDown(depth:, note:)
7777
})
7878
|> decode.field("depth", decode.float)
79-
|> decode.field("note", decode.nullable(decode.string))
79+
|> decode.field("note", decode.optional(decode.string))
8080
_ -> decode.fail("Data")
8181
}
8282
})

birdie_snapshots/codegen_element_enum_directions_nullable_test.accepted

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
version: 1.2.1
2+
version: 1.2.5
33
title: codegen_element_enum_directions_nullable_test
44
file: ./test/json_typedef_test.gleam
55
test_name: codegen_element_enum_directions_nullable_test
@@ -31,7 +31,7 @@ pub type DataElement {
3131
}
3232

3333
pub fn data_decoder() -> decode.Decoder(List(option.Option(DataElement))) {
34-
decode.list(decode.nullable(decode.then(decode.string, fn(s) {
34+
decode.list(decode.optional(decode.then(decode.string, fn(s) {
3535
case s {
3636
"UP" -> decode.into(Up)
3737
"DOWN" -> decode.into(Down)

birdie_snapshots/codegen_properties_mixed_test.accepted

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
version: 1.2.1
2+
version: 1.2.5
33
title: codegen_properties_mixed_test
44
file: ./test/json_typedef_test.gleam
55
test_name: codegen_properties_mixed_test
@@ -76,7 +76,7 @@ fn helper__optional_property(
7676
key: String,
7777
value: option.Option(a),
7878
to_json: fn(a) -> json.Json,
79-
) -> List(#(String, json.Json)), {
79+
) -> List(#(String, json.Json)) {
8080
case value {
8181
option.Some(value) -> [#(key, to_json(value)), ..object]
8282
option.None -> object

birdie_snapshots/codegen_properties_nullable_in_elements_test.accepted

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
version: 1.2.1
2+
version: 1.2.5
33
title: codegen_properties_nullable_in_elements_test
44
file: ./test/json_typedef_test.gleam
55
test_name: codegen_properties_nullable_in_elements_test
@@ -39,7 +39,7 @@ pub type DataElement {
3939
}
4040

4141
pub fn data_decoder() -> decode.Decoder(List(option.Option(DataElement))) {
42-
decode.list(decode.nullable(decode.into({
42+
decode.list(decode.optional(decode.into({
4343
use count <- decode.parameter
4444
DataElement(count:)
4545
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
version: 1.2.5
3+
title: codegen_properties_nullable_in_type_test
4+
file: ./test/json_typedef_test.gleam
5+
test_name: codegen_properties_nullable_in_type_test
6+
---
7+
RootSchema(
8+
[],
9+
Properties(
10+
False,
11+
[],
12+
PropertiesSchema(
13+
[
14+
#("key", Type(True, [], String)),
15+
#(
16+
"amount",
17+
Type(False, [], Uint8),
18+
),
19+
],
20+
[],
21+
False,
22+
),
23+
),
24+
)
25+
26+
-----------------------------------------------------------
27+
28+
import decode
29+
import gleam/json
30+
import gleam/option
31+
32+
pub type Data {
33+
Data(
34+
amount: Int,
35+
key: option.Option(String),
36+
)
37+
}
38+
39+
pub fn data_decoder() -> decode.Decoder(Data) {
40+
decode.into({
41+
use amount <- decode.parameter
42+
use key <- decode.parameter
43+
Data(amount:, key:)
44+
})
45+
|> decode.field("amount", decode.int)
46+
|> decode.field("key", decode.optional(decode.string))
47+
}
48+
49+
pub fn data_to_json(data: Data) -> json.Json {
50+
json.object([
51+
#("key", json.nullable(data.key, json.string)),
52+
#("amount", json.int(data.amount)),
53+
])
54+
}

birdie_snapshots/codegen_properties_nullable_test.accepted

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
version: 1.2.1
2+
version: 1.2.5
33
title: codegen_properties_nullable_test
44
file: ./test/json_typedef_test.gleam
55
test_name: codegen_properties_nullable_test
@@ -40,7 +40,7 @@ pub type Data {
4040
}
4141

4242
pub fn data_decoder() -> decode.Decoder(option.Option(Data)) {
43-
decode.nullable(decode.into({
43+
decode.optional(decode.into({
4444
use amount <- decode.parameter
4545
use key <- decode.parameter
4646
Data(amount:, key:)

0 commit comments

Comments
 (0)