Skip to content

Commit e4637f1

Browse files
authored
Merge pull request #42 from datatrails/dev/henry/10492-simple-filtering
handle int32s in marshaller
2 parents f72469a + 5d676ff commit e4637f1

File tree

8 files changed

+631
-0
lines changed

8 files changed

+631
-0
lines changed

datatrails-common-api/marshalers/simpleoneof/simpleoneof.go

+14
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,20 @@ func (m *WrappedDecoder) Decode(obj any) error {
773773

774774
newMsg.Set(f, enumValue)
775775

776+
case protoreflect.Int32Kind:
777+
{
778+
// We know to expect an int32 so we can be explicit about it.
779+
var hopefulInt32 int32
780+
ee := json.Unmarshal(data[string(f.Name())], &hopefulInt32)
781+
if ee != nil {
782+
logger.Sugar.Infof("ERR: %v", ee)
783+
return ee
784+
}
785+
786+
v = protoreflect.ValueOf(hopefulInt32)
787+
newMsg.Set(f, v)
788+
}
789+
776790
default:
777791
if f.IsList() {
778792
vv := []json.RawMessage{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package simpleoneof
2+
3+
import (
4+
"bytes"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/datatrails/go-datatrails-common-api-gen/unittestapi"
9+
"github.com/datatrails/go-datatrails-common/logger"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
// message Int32Test {
14+
// int32 integer_value = 1;
15+
// }
16+
17+
func TestDecodeInt32(t *testing.T) {
18+
logger.New("NOOP")
19+
defer logger.OnExit()
20+
21+
tests := []struct {
22+
name string
23+
input []byte
24+
shouldError bool
25+
shouldBe int32
26+
}{
27+
{
28+
name: "int32 value decodes okay",
29+
input: []byte(`{"integer_value":123}`),
30+
shouldError: false,
31+
shouldBe: 123,
32+
},
33+
{
34+
name: "zero int32 value decodes okay",
35+
input: []byte(`{"integer_value":0}`),
36+
shouldError: false,
37+
shouldBe: 0,
38+
},
39+
{
40+
name: "integer larger than int32 errors",
41+
input: []byte(`{"integer_value":3000000000}`),
42+
shouldError: true,
43+
},
44+
{
45+
name: "number with fractional component should error",
46+
input: []byte(`{"integer_value":123.45}`),
47+
shouldError: true,
48+
},
49+
{
50+
name: "number with zero fractional component should error",
51+
input: []byte(`{"integer_value":123.0}`),
52+
shouldError: true,
53+
},
54+
}
55+
56+
for _, test := range tests {
57+
t.Run(test.name, func(t *testing.T) {
58+
inputReader := bytes.NewReader(test.input)
59+
protoMsg := unittestapi.Int32Test{}
60+
61+
marshaller := NewFlatMarshaler([]reflect.Type{}, []reflect.Type{})
62+
decoder := marshaller.NewDecoder(inputReader)
63+
64+
err := decoder.Decode(&protoMsg)
65+
if test.shouldError {
66+
assert.Error(t, err)
67+
} else {
68+
assert.NoError(t, err)
69+
assert.Equal(t, test.shouldBe, protoMsg.IntegerValue)
70+
}
71+
})
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
2+
<h1>Methods</h1>
3+
<h1>Types</h1>
4+
<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:macro-id="4b19e0eb-1e4d-4ff8-9d75-66a33d046a80">
5+
<ac:parameter ac:name="">Int32Test</ac:parameter>
6+
</ac:structured-macro>
7+
<h2>Int32Test</h2>
8+
<table data-layout="default" ac:local-id="f67a39c9-77b9-481e-8d7b-721abe827483">
9+
<colgroup>
10+
<col style="width: 330.0px;" />
11+
<col style="width: 360.0px;" />
12+
<col style="width: 200.0px;" />
13+
<col style="width: 710.0px;" />
14+
</colgroup>
15+
<tbody>
16+
<tr>
17+
<th><p><strong>Name</strong></p></th>
18+
<th><p><strong>Type</strong></p></th>
19+
<th><p><strong>Labels</strong></p></th>
20+
<th><p><strong>Description</strong></p></th>
21+
</tr>
22+
<tr>
23+
<td><p>integer_value</p></td>
24+
<td><p><a href="#int32">int32</a></p></td>
25+
<td><p></p></td>
26+
<td><p> </p></td>
27+
</tr>
28+
</tbody>
29+
</table>
30+
<p />
31+
<h1>Scalar Value Types</h1>
32+
<table data-layout="default" ac:local-id="c3c18cd5-0914-4312-b0d0-47b95f108465">
33+
<tbody>
34+
<tr>
35+
<td><p><strong>.proto Type</strong></p></td>
36+
<td><p><strong>Notes</strong></p></td>
37+
<td><p><strong>C++</strong></p></td>
38+
<td><p><strong>Java</strong></p></td>
39+
<td><p><strong>Python</strong></p></td>
40+
<td><p><strong>Go</strong></p></td>
41+
<td><p><strong>C#</strong></p></td>
42+
<td><p><strong>PHP</strong></p></td>
43+
<td><p><strong>Ruby</strong></p></td>
44+
</tr>
45+
46+
<tr>
47+
<td><p>double<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">double</ac:parameter></ac:structured-macro></p></td>
48+
<td><p></p></td>
49+
<td><p>double</p></td>
50+
<td><p>double</p></td>
51+
<td><p>float</p></td>
52+
<td><p>float64</p></td>
53+
<td><p>double</p></td>
54+
<td><p>float</p></td>
55+
<td><p>Float</p></td>
56+
</tr>
57+
58+
<tr>
59+
<td><p>float<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">float</ac:parameter></ac:structured-macro></p></td>
60+
<td><p></p></td>
61+
<td><p>float</p></td>
62+
<td><p>float</p></td>
63+
<td><p>float</p></td>
64+
<td><p>float32</p></td>
65+
<td><p>float</p></td>
66+
<td><p>float</p></td>
67+
<td><p>Float</p></td>
68+
</tr>
69+
70+
<tr>
71+
<td><p>int32<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">int32</ac:parameter></ac:structured-macro></p></td>
72+
<td><p>Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead.</p></td>
73+
<td><p>int32</p></td>
74+
<td><p>int</p></td>
75+
<td><p>int</p></td>
76+
<td><p>int32</p></td>
77+
<td><p>int</p></td>
78+
<td><p>integer</p></td>
79+
<td><p>Bignum or Fixnum (as required)</p></td>
80+
</tr>
81+
82+
<tr>
83+
<td><p>int64<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">int64</ac:parameter></ac:structured-macro></p></td>
84+
<td><p>Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead.</p></td>
85+
<td><p>int64</p></td>
86+
<td><p>long</p></td>
87+
<td><p>int/long</p></td>
88+
<td><p>int64</p></td>
89+
<td><p>long</p></td>
90+
<td><p>integer/string</p></td>
91+
<td><p>Bignum</p></td>
92+
</tr>
93+
94+
<tr>
95+
<td><p>uint32<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">uint32</ac:parameter></ac:structured-macro></p></td>
96+
<td><p>Uses variable-length encoding.</p></td>
97+
<td><p>uint32</p></td>
98+
<td><p>int</p></td>
99+
<td><p>int/long</p></td>
100+
<td><p>uint32</p></td>
101+
<td><p>uint</p></td>
102+
<td><p>integer</p></td>
103+
<td><p>Bignum or Fixnum (as required)</p></td>
104+
</tr>
105+
106+
<tr>
107+
<td><p>uint64<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">uint64</ac:parameter></ac:structured-macro></p></td>
108+
<td><p>Uses variable-length encoding.</p></td>
109+
<td><p>uint64</p></td>
110+
<td><p>long</p></td>
111+
<td><p>int/long</p></td>
112+
<td><p>uint64</p></td>
113+
<td><p>ulong</p></td>
114+
<td><p>integer/string</p></td>
115+
<td><p>Bignum or Fixnum (as required)</p></td>
116+
</tr>
117+
118+
<tr>
119+
<td><p>sint32<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">sint32</ac:parameter></ac:structured-macro></p></td>
120+
<td><p>Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s.</p></td>
121+
<td><p>int32</p></td>
122+
<td><p>int</p></td>
123+
<td><p>int</p></td>
124+
<td><p>int32</p></td>
125+
<td><p>int</p></td>
126+
<td><p>integer</p></td>
127+
<td><p>Bignum or Fixnum (as required)</p></td>
128+
</tr>
129+
130+
<tr>
131+
<td><p>sint64<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">sint64</ac:parameter></ac:structured-macro></p></td>
132+
<td><p>Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s.</p></td>
133+
<td><p>int64</p></td>
134+
<td><p>long</p></td>
135+
<td><p>int/long</p></td>
136+
<td><p>int64</p></td>
137+
<td><p>long</p></td>
138+
<td><p>integer/string</p></td>
139+
<td><p>Bignum</p></td>
140+
</tr>
141+
142+
<tr>
143+
<td><p>fixed32<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">fixed32</ac:parameter></ac:structured-macro></p></td>
144+
<td><p>Always four bytes. More efficient than uint32 if values are often greater than 2^28.</p></td>
145+
<td><p>uint32</p></td>
146+
<td><p>int</p></td>
147+
<td><p>int</p></td>
148+
<td><p>uint32</p></td>
149+
<td><p>uint</p></td>
150+
<td><p>integer</p></td>
151+
<td><p>Bignum or Fixnum (as required)</p></td>
152+
</tr>
153+
154+
<tr>
155+
<td><p>fixed64<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">fixed64</ac:parameter></ac:structured-macro></p></td>
156+
<td><p>Always eight bytes. More efficient than uint64 if values are often greater than 2^56.</p></td>
157+
<td><p>uint64</p></td>
158+
<td><p>long</p></td>
159+
<td><p>int/long</p></td>
160+
<td><p>uint64</p></td>
161+
<td><p>ulong</p></td>
162+
<td><p>integer/string</p></td>
163+
<td><p>Bignum</p></td>
164+
</tr>
165+
166+
<tr>
167+
<td><p>sfixed32<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">sfixed32</ac:parameter></ac:structured-macro></p></td>
168+
<td><p>Always four bytes.</p></td>
169+
<td><p>int32</p></td>
170+
<td><p>int</p></td>
171+
<td><p>int</p></td>
172+
<td><p>int32</p></td>
173+
<td><p>int</p></td>
174+
<td><p>integer</p></td>
175+
<td><p>Bignum or Fixnum (as required)</p></td>
176+
</tr>
177+
178+
<tr>
179+
<td><p>sfixed64<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">sfixed64</ac:parameter></ac:structured-macro></p></td>
180+
<td><p>Always eight bytes.</p></td>
181+
<td><p>int64</p></td>
182+
<td><p>long</p></td>
183+
<td><p>int/long</p></td>
184+
<td><p>int64</p></td>
185+
<td><p>long</p></td>
186+
<td><p>integer/string</p></td>
187+
<td><p>Bignum</p></td>
188+
</tr>
189+
190+
<tr>
191+
<td><p>bool<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">bool</ac:parameter></ac:structured-macro></p></td>
192+
<td><p></p></td>
193+
<td><p>bool</p></td>
194+
<td><p>boolean</p></td>
195+
<td><p>boolean</p></td>
196+
<td><p>bool</p></td>
197+
<td><p>bool</p></td>
198+
<td><p>boolean</p></td>
199+
<td><p>TrueClass/FalseClass</p></td>
200+
</tr>
201+
202+
<tr>
203+
<td><p>string<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">string</ac:parameter></ac:structured-macro></p></td>
204+
<td><p>A string must always contain UTF-8 encoded or 7-bit ASCII text.</p></td>
205+
<td><p>string</p></td>
206+
<td><p>String</p></td>
207+
<td><p>str/unicode</p></td>
208+
<td><p>string</p></td>
209+
<td><p>string</p></td>
210+
<td><p>string</p></td>
211+
<td><p>String (UTF-8)</p></td>
212+
</tr>
213+
214+
<tr>
215+
<td><p>bytes<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:local-id="510e24cf-902a-48c4-a9ca-15d9c11ee007" ac:macro-id="358ca23f11a312a9c85b2d4258e342e2"><ac:parameter ac:name="">bytes</ac:parameter></ac:structured-macro></p></td>
216+
<td><p>May contain any arbitrary sequence of bytes.</p></td>
217+
<td><p>string</p></td>
218+
<td><p>ByteString</p></td>
219+
<td><p>str</p></td>
220+
<td><p>[]byte</p></td>
221+
<td><p>ByteString</p></td>
222+
<td><p>string</p></td>
223+
<td><p>String (ASCII-8BIT)</p></td>
224+
</tr>
225+
226+
</tbody>
227+
</table>

0 commit comments

Comments
 (0)