Skip to content

Commit

Permalink
Merge pull request #120 from nilslice/pr-119
Browse files Browse the repository at this point in the history
merge-pr: fix for #117
  • Loading branch information
nilslice authored Sep 17, 2019
2 parents 7c01b45 + 3679545 commit c61398f
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 21 deletions.
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ jobs:
if [ "$CHECK" = 0 ]; then
exit 1
fi
- run:
name: check that proto.lock records aggregate options with array values
command: |
set +o pipefail
rm proto.lock && protolock init
cat proto.lock | grep "4.56"
cat proto.lock | grep "7.89"
plugin_nodejs:
docker:
- image: circleci/node:8
Expand Down
47 changes: 27 additions & 20 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,30 +372,37 @@ func parseOptions(opts []*proto.Option) []Option {
}

func parseOption(o *proto.Option) Option {
option := Option{
Name: o.Name,
}
if isAggregatedOption(o) {
option.Aggregated = parseAggregatedValues(o)
} else {
option.Value = o.Constant.Source
}
return option
return recurseLiteral(o.Name, &o.Constant)
}

func parseAggregatedValues(o *proto.Option) []Option {
var aggOpts []Option
for _, nl := range o.Constant.OrderedMap {
aggOpts = append(aggOpts, Option{
Name: nl.Name,
Value: nl.Source,
})
func recurseLiteral(name string, lit *proto.Literal) Option {

if lit.OrderedMap != nil {
var opts []Option
for _, l := range lit.OrderedMap {
opts = append(opts, recurseLiteral(l.Name, l.Literal))
}
return Option{
Name: name,
Aggregated: opts,
}
}

if lit.Array != nil {
var opts []Option
for _, l := range lit.Array {
opts = append(opts, recurseLiteral("", l))
}
return Option{
Name: name,
Aggregated: opts,
}
}
return aggOpts
}

func isAggregatedOption(o *proto.Option) bool {
return o.Constant.Source == "" && o.Constant.OrderedMap != nil
return Option{
Name: name,
Value: lit.Source,
}
}

func protoWithImport(apply func(p *proto.Import)) proto.Handler {
Expand Down
14 changes: 14 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ message Channel {
string description = 3 [(custom_options_commas) = { personal: true, internal: false, owner: "some owner" }];
map<string, int32> attributes = 4;
string address = 5 [(custom_options) = { personal: true internal: false owner: "some owner" }];
float array = 6 [(validate.rules).floats = {in: [4.56, 7.89]}];
float map = 7 [(validate.rules).keys = {map: { a: [4.56, 7.89], c: d}}];
}
`

Expand Down Expand Up @@ -269,6 +271,18 @@ func TestParseIncludingNestedFieldOptionsAggregated(t *testing.T) {
assert.Equal(t, "false", entry.Messages[0].Fields[3].Options[0].Aggregated[1].Value)
assert.Equal(t, "owner", entry.Messages[0].Fields[3].Options[0].Aggregated[2].Name)
assert.Equal(t, "some owner", entry.Messages[0].Fields[3].Options[0].Aggregated[2].Value)
assert.Equal(t, "in", entry.Messages[0].Fields[4].Options[0].Aggregated[0].Name)
assert.Equal(t, "", entry.Messages[0].Fields[4].Options[0].Aggregated[0].Value)
assert.Equal(t, "4.56", entry.Messages[0].Fields[4].Options[0].Aggregated[0].Aggregated[0].Value)
assert.Equal(t, "7.89", entry.Messages[0].Fields[4].Options[0].Aggregated[0].Aggregated[1].Value)
assert.Equal(t, "map", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Name)
assert.Equal(t, "", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Value)
assert.Equal(t, "a", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[0].Name)
assert.Equal(t, "", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[0].Value)
assert.Equal(t, "4.56", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[0].Aggregated[0].Value)
assert.Equal(t, "7.89", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[0].Aggregated[1].Value)
assert.Equal(t, "c", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[1].Name)
assert.Equal(t, "d", entry.Messages[0].Fields[5].Options[0].Aggregated[0].Aggregated[1].Value)
}

func TestParseIncludingEnumFieldOptions(t *testing.T) {
Expand Down
52 changes: 52 additions & 0 deletions proto.lock
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,30 @@
{
"name": "owner",
"value": "some owner"
},
{
"name": "arr",
"aggregated": [
{
"value": "1.2"
},
{
"value": "3.4"
}
]
},
{
"name": "map",
"aggregated": [
{
"name": "a",
"value": "b"
},
{
"name": "c",
"value": "d"
}
]
}
]
}
Expand Down Expand Up @@ -475,6 +499,34 @@
"type": "bool"
}
]
},
{
"name": "FloatIn",
"fields": [
{
"id": 1,
"name": "val",
"type": "float",
"options": [
{
"name": "(validate.rules).float",
"aggregated": [
{
"name": "in",
"aggregated": [
{
"value": "4.56"
},
{
"value": "7.89"
}
]
}
]
}
]
}
]
}
],
"services": [
Expand Down
2 changes: 1 addition & 1 deletion testdata/imports_options.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ message Channel2 {
string name = 2 [(personal) = true, (owner) = 'test'];
string description = 3 [(custom_options_commas) = { personal: true, internal: false, owner: "some owner" }];
map<string, int32> map = 4 [(personal) = true];
string address = 5 [(custom_options) = { personal: true internal: false owner: "some owner" }];
string address = 5 [(custom_options) = { personal: true internal: false owner: "some owner", arr: [1.2, 3.4], map: { a:b, c:d } }];
}

enum TestEnumOption {
Expand Down
2 changes: 2 additions & 0 deletions testdata/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ service ChannelChanger {
rpc Next(stream NextRequest) returns (Channel);
rpc Previous(PreviousRequest) returns (stream Channel);
}

message FloatIn { float val = 1 [(validate.rules).float = {in: [4.56, 7.89]}]; }

0 comments on commit c61398f

Please sign in to comment.