Skip to content

Commit c61398f

Browse files
authored
Merge pull request #120 from nilslice/pr-119
merge-pr: fix for #117
2 parents 7c01b45 + 3679545 commit c61398f

File tree

6 files changed

+104
-21
lines changed

6 files changed

+104
-21
lines changed

.circleci/config.yml

+8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ jobs:
9292
if [ "$CHECK" = 0 ]; then
9393
exit 1
9494
fi
95+
- run:
96+
name: check that proto.lock records aggregate options with array values
97+
command: |
98+
set +o pipefail
99+
100+
rm proto.lock && protolock init
101+
cat proto.lock | grep "4.56"
102+
cat proto.lock | grep "7.89"
95103
plugin_nodejs:
96104
docker:
97105
- image: circleci/node:8

parse.go

+27-20
Original file line numberDiff line numberDiff line change
@@ -372,30 +372,37 @@ func parseOptions(opts []*proto.Option) []Option {
372372
}
373373

374374
func parseOption(o *proto.Option) Option {
375-
option := Option{
376-
Name: o.Name,
377-
}
378-
if isAggregatedOption(o) {
379-
option.Aggregated = parseAggregatedValues(o)
380-
} else {
381-
option.Value = o.Constant.Source
382-
}
383-
return option
375+
return recurseLiteral(o.Name, &o.Constant)
384376
}
385377

386-
func parseAggregatedValues(o *proto.Option) []Option {
387-
var aggOpts []Option
388-
for _, nl := range o.Constant.OrderedMap {
389-
aggOpts = append(aggOpts, Option{
390-
Name: nl.Name,
391-
Value: nl.Source,
392-
})
378+
func recurseLiteral(name string, lit *proto.Literal) Option {
379+
380+
if lit.OrderedMap != nil {
381+
var opts []Option
382+
for _, l := range lit.OrderedMap {
383+
opts = append(opts, recurseLiteral(l.Name, l.Literal))
384+
}
385+
return Option{
386+
Name: name,
387+
Aggregated: opts,
388+
}
389+
}
390+
391+
if lit.Array != nil {
392+
var opts []Option
393+
for _, l := range lit.Array {
394+
opts = append(opts, recurseLiteral("", l))
395+
}
396+
return Option{
397+
Name: name,
398+
Aggregated: opts,
399+
}
393400
}
394-
return aggOpts
395-
}
396401

397-
func isAggregatedOption(o *proto.Option) bool {
398-
return o.Constant.Source == "" && o.Constant.OrderedMap != nil
402+
return Option{
403+
Name: name,
404+
Value: lit.Source,
405+
}
399406
}
400407

401408
func protoWithImport(apply func(p *proto.Import)) proto.Handler {

parse_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ message Channel {
100100
string description = 3 [(custom_options_commas) = { personal: true, internal: false, owner: "some owner" }];
101101
map<string, int32> attributes = 4;
102102
string address = 5 [(custom_options) = { personal: true internal: false owner: "some owner" }];
103+
float array = 6 [(validate.rules).floats = {in: [4.56, 7.89]}];
104+
float map = 7 [(validate.rules).keys = {map: { a: [4.56, 7.89], c: d}}];
103105
}
104106
`
105107

@@ -269,6 +271,18 @@ func TestParseIncludingNestedFieldOptionsAggregated(t *testing.T) {
269271
assert.Equal(t, "false", entry.Messages[0].Fields[3].Options[0].Aggregated[1].Value)
270272
assert.Equal(t, "owner", entry.Messages[0].Fields[3].Options[0].Aggregated[2].Name)
271273
assert.Equal(t, "some owner", entry.Messages[0].Fields[3].Options[0].Aggregated[2].Value)
274+
assert.Equal(t, "in", entry.Messages[0].Fields[4].Options[0].Aggregated[0].Name)
275+
assert.Equal(t, "", entry.Messages[0].Fields[4].Options[0].Aggregated[0].Value)
276+
assert.Equal(t, "4.56", entry.Messages[0].Fields[4].Options[0].Aggregated[0].Aggregated[0].Value)
277+
assert.Equal(t, "7.89", entry.Messages[0].Fields[4].Options[0].Aggregated[0].Aggregated[1].Value)
278+
assert.Equal(t, "map", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Name)
279+
assert.Equal(t, "", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Value)
280+
assert.Equal(t, "a", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[0].Name)
281+
assert.Equal(t, "", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[0].Value)
282+
assert.Equal(t, "4.56", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[0].Aggregated[0].Value)
283+
assert.Equal(t, "7.89", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[0].Aggregated[1].Value)
284+
assert.Equal(t, "c", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[1].Name)
285+
assert.Equal(t, "d", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[1].Value)
272286
}
273287

274288
func TestParseIncludingEnumFieldOptions(t *testing.T) {

proto.lock

+52
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,30 @@
212212
{
213213
"name": "owner",
214214
"value": "some owner"
215+
},
216+
{
217+
"name": "arr",
218+
"aggregated": [
219+
{
220+
"value": "1.2"
221+
},
222+
{
223+
"value": "3.4"
224+
}
225+
]
226+
},
227+
{
228+
"name": "map",
229+
"aggregated": [
230+
{
231+
"name": "a",
232+
"value": "b"
233+
},
234+
{
235+
"name": "c",
236+
"value": "d"
237+
}
238+
]
215239
}
216240
]
217241
}
@@ -475,6 +499,34 @@
475499
"type": "bool"
476500
}
477501
]
502+
},
503+
{
504+
"name": "FloatIn",
505+
"fields": [
506+
{
507+
"id": 1,
508+
"name": "val",
509+
"type": "float",
510+
"options": [
511+
{
512+
"name": "(validate.rules).float",
513+
"aggregated": [
514+
{
515+
"name": "in",
516+
"aggregated": [
517+
{
518+
"value": "4.56"
519+
},
520+
{
521+
"value": "7.89"
522+
}
523+
]
524+
}
525+
]
526+
}
527+
]
528+
}
529+
]
478530
}
479531
],
480532
"services": [

testdata/imports_options.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ message Channel2 {
1717
string name = 2 [(personal) = true, (owner) = 'test'];
1818
string description = 3 [(custom_options_commas) = { personal: true, internal: false, owner: "some owner" }];
1919
map<string, int32> map = 4 [(personal) = true];
20-
string address = 5 [(custom_options) = { personal: true internal: false owner: "some owner" }];
20+
string address = 5 [(custom_options) = { personal: true internal: false owner: "some owner", arr: [1.2, 3.4], map: { a:b, c:d } }];
2121
}
2222

2323
enum TestEnumOption {

testdata/test.proto

+2
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,5 @@ service ChannelChanger {
8181
rpc Next(stream NextRequest) returns (Channel);
8282
rpc Previous(PreviousRequest) returns (stream Channel);
8383
}
84+
85+
message FloatIn { float val = 1 [(validate.rules).float = {in: [4.56, 7.89]}]; }

0 commit comments

Comments
 (0)