Skip to content

Commit 503bcf8

Browse files
committed
Add default zero values
1 parent 70aa47a commit 503bcf8

13 files changed

+570
-519
lines changed

README.md

+32-31
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,34 @@ Complies with [protojson](https://protobuf.dev/programming-guides/proto3/#json).
1212
|-----------------|-------------------------------------|------------------------------------|
1313
| `map<K, V>` | `{ [string]: V }` | All keys are converted to strings. |
1414
| `repeated V` | `[...V]` | |
15-
| `bool` | `bool` | |
16-
| `string` | `string` | |
17-
| `bytes` | `bytes` | |
18-
| `int32` | `int32` | |
19-
| `fixed32` | `int32` | |
20-
| `uint32` | `uint32` | |
21-
| `int64` | `int64` | |
22-
| `fixed64` | `fixed64` | |
23-
| `uint32` | `uint64` | |
24-
| `float` | `float32` | |
25-
| `double` | `float64` | |
26-
| `Any` | `null \| { "@type": string, ... }` | |
27-
| `Struct` | `null \| { [string]: _ }` | |
28-
| `Value` | `null \| _` | |
29-
| `ListValue` | `null \| [...]` | |
30-
| `NullValue` | `null \| null` | |
31-
| `BoolValue` | `null \| bool` | |
32-
| `StringValue` | `null \| string` | |
33-
| `Int32Value` | `null \| int32` | |
34-
| `UInt32Value` | `null \| uint32` | |
35-
| `Int64Value` | `null \| int64` | |
36-
| `UInt64Value` | `null \| uint64` | |
37-
| `FloatValue` | `null \| float32` | |
38-
| `DoubleValue` | `null \| double` | |
39-
| `Empty` | `null \| close({})` | |
40-
| `Timestamp` | `null \| string` | See the [`Timestamp`](#timestamp) section for more information. |
41-
| `Duration` | `null \| string` | See the [`Duration`](#duration) section for more information. |
42-
| `FieldMask` | `null \| { paths: [...string] }` | |
15+
| `bool` | `*false \| bool` | |
16+
| `string` | `*"" \| string` | |
17+
| `bytes` | `*'' \| bytes` | |
18+
| `int32` | `*0 \| int32` | |
19+
| `fixed32` | `*0 \| int32` | |
20+
| `uint32` | `*0 \| uint32` | |
21+
| `int64` | `*0 \| int64` | |
22+
| `fixed64` | `*0 \| fixed64` | |
23+
| `uint32` | `*0 \| uint64` | |
24+
| `float` | `*0 \| float32` | |
25+
| `double` | `*0 \| float64` | |
26+
| `Any` | `*null \| { "@type": string, ... }` | |
27+
| `Struct` | `*null \| { [string]: _ }` | |
28+
| `Value` | `*null \| _` | |
29+
| `ListValue` | `*null \| [...]` | |
30+
| `NullValue` | `*null \| null` | |
31+
| `BoolValue` | `*null \| bool` | |
32+
| `StringValue` | `*null \| string` | |
33+
| `Int32Value` | `*null \| int32` | |
34+
| `UInt32Value` | `*null \| uint32` | |
35+
| `Int64Value` | `*null \| int64` | |
36+
| `UInt64Value` | `*null \| uint64` | |
37+
| `FloatValue` | `*null \| float32` | |
38+
| `DoubleValue` | `*null \| double` | |
39+
| `Empty` | `*null \| close({})` | |
40+
| `Timestamp` | `*null \| string` | See the [`Timestamp`](#timestamp) section for more information. |
41+
| `Duration` | `*null \| string` | See the [`Duration`](#duration) section for more information. |
42+
| `FieldMask` | `*null \| { paths: [...string] }` | |
4343

4444
### Message
4545

@@ -57,10 +57,11 @@ To:
5757

5858
```cue
5959
#Foo: {
60-
name: string
60+
name: *"" | string
6161
name: !="xxx"
62-
age: int32
62+
age: *0 | int32
6363
age: <100
64+
ageNextYear: *0 | int32
6465
ageNextYear: age+1
6566
}
6667
```
@@ -77,7 +78,7 @@ enum Bar {
7778
To:
7879

7980
```cue
80-
#Bar: #Bar_ZERO | #Bar_ONE
81+
#Bar: *#Bar_ZERO | #Bar_ONE
8182
8283
#Bar_ZERO: "ZERO"
8384
#Bar_ONE: "ONE"

example/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
```bash
22
cp ../pkg/options/cue.proto vendor/cue.proto
3-
buf generate proto
4-
cue export ./gen/foo_gen.cue foo.cue | tee foo.json
3+
buf generate src
4+
cue export ./gen/example/v1/example_gen.cue example_values.cue | tee example_values.json
5+
cue export ./gen/example/v1/example_gen.cue example_default.cue | tee example_default.json
56
```

example/buf.work.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
version: v1
22
directories:
3-
- proto
3+
- src
44
- vendor

example/example_default.cue

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
basic: #Basic
2+
basic: {
3+
}
4+
wellKnownTypes: #WellKnownTypes
5+
wellKnownTypes: {
6+
}
7+
fieldOptions: #FieldOptions
8+
fieldOptions: {
9+
}

example/example_default.json

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"basic": {
3+
"message": {
4+
"field": ""
5+
},
6+
"messageNested": {
7+
"inner": {
8+
"innerEnum": "Inner_InnerEnum_ZERO",
9+
"innerInnerEnum": "Inner_Inner_InnerEnum_ZERO"
10+
},
11+
"innerInner": {
12+
"enum": "Inner_Inner_InnerEnum_ZERO"
13+
},
14+
"innerEnum": "InnerEnum_ZERO",
15+
"innerInnerEnum": "Inner_InnerEnum_ZERO",
16+
"innerInnerInnerEnum": "Inner_Inner_InnerEnum_ZERO"
17+
},
18+
"enum": "ZERO",
19+
"bool": false,
20+
"string": "",
21+
"bytes": "",
22+
"int32": 0,
23+
"fixed32": 0,
24+
"uint32": 0,
25+
"int64": 0,
26+
"fixed64": 0,
27+
"uint64": 0,
28+
"float": 0,
29+
"double": 0,
30+
"mapStringString": {},
31+
"mapStringMessage": {},
32+
"mapKeyConvertedToString": {},
33+
"mapStringInt32value": {},
34+
"strings": [],
35+
"messages": [],
36+
"int32NonOptional": 0,
37+
"messageNonOptional": {
38+
"field": ""
39+
}
40+
},
41+
"wellKnownTypes": {
42+
"any": null,
43+
"struct": null,
44+
"value": null,
45+
"list": [],
46+
"null": null,
47+
"bool": null,
48+
"string": null,
49+
"bytes": null,
50+
"int32": null,
51+
"int64": null,
52+
"uint32": null,
53+
"uint64": null,
54+
"float": null,
55+
"double": null,
56+
"empty": null,
57+
"timestamp": null,
58+
"duration": null,
59+
"fieldMask": null
60+
},
61+
"fieldOptions": {
62+
"name": "",
63+
"age": 0,
64+
"ageOfNextYear": 1
65+
}
66+
}

example/foo.cue example/example_values.cue

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ wellKnownTypes: {
6969
empty: {}
7070
timestamp: "2023-07-17T16:23:00+09:00"
7171
duration: "1h2m3s"
72-
fieldMask: paths: ["a.b.c."]
72+
fieldMask: paths: ["a.b.c"]
7373
}
7474
fieldOptions: #FieldOptions
7575
fieldOptions: {

example/foo.json example/example_values.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"duration": "1h2m3s",
9494
"fieldMask": {
9595
"paths": [
96-
"a.b.c."
96+
"a.b.c"
9797
]
9898
}
9999
},

0 commit comments

Comments
 (0)