Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Register
Sign in
Toggle navigation
Menu
Open sidebar
pub
protoc-gen-validate
Commits
4349a359
Unverified
Commit
4349a359
authored
6 years ago
by
Chris Roche
Committed by
GitHub
6 years ago
Browse files
Options
Download
Email Patches
Plain Diff
Properly handle repeated enums (#140)
parent
b04e031a
main
v0.6.1
v0.6.0
v0.5.1
v0.5.0
v0.4.1
v0.4.1-java
v0.4.0
v0.4.0-java
v0.3.0-java
v0.2.0-java
v0.1.0
v0.0.14
v0.0.13
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
templates/cc/enum.go
+5
-1
templates/cc/enum.go
templates/cc/register.go
+13
-8
templates/cc/register.go
templates/cc/repeated.go
+1
-1
templates/cc/repeated.go
tests/harness/cases/enums.proto
+6
-0
tests/harness/cases/enums.proto
tests/harness/executor/cases.go
+12
-0
tests/harness/executor/cases.go
with
37 additions
and
10 deletions
+37
-10
templates/cc/enum.go
+
5
-
1
View file @
4349a359
...
...
@@ -6,7 +6,11 @@ const enumTpl = `
{{ template "in" . }}
{{ if $r.GetDefinedOnly }}
if (!{{ package $f.Type.Enum }}::{{ (typ $f).Element }}_IsValid({{ accessor . }})) {
{{ if $f.Type.IsRepeated }}
if (!{{ class $f.Type.Element.Enum }}_IsValid({{ accessor . }})) {
{{ else }}
if (!{{ class $f.Type.Enum }}_IsValid({{ accessor . }})) {
{{ end }}
{{ err . "value must be one of the defined enum values" }}
}
{{ end }}
...
...
This diff is collapsed.
Click to expand it.
templates/cc/register.go
+
13
-
8
View file @
4349a359
...
...
@@ -10,8 +10,8 @@ import (
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/duration"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/lyft/protoc-gen-star"
"github.com/lyft/protoc-gen-star/lang/go"
pgs
"github.com/lyft/protoc-gen-star"
pgsgo
"github.com/lyft/protoc-gen-star/lang/go"
"github.com/lyft/protoc-gen-validate/templates/shared"
)
...
...
@@ -143,15 +143,20 @@ func (fns CCFuncs) hasAccessor(ctx shared.RuleContext) string {
fns
.
methodName
(
ctx
.
Field
.
Name
()))
}
func
(
fns
CCFuncs
)
classBaseName
(
msg
pgs
.
Message
)
string
{
if
m
,
ok
:=
msg
.
Parent
()
.
(
pgs
.
Message
);
ok
{
return
fmt
.
Sprintf
(
"%s_%s"
,
fns
.
classBaseName
(
m
),
msg
.
Name
()
.
String
())
type
childEntity
interface
{
pgs
.
Entity
Parent
()
pgs
.
ParentEntity
}
func
(
fns
CCFuncs
)
classBaseName
(
ent
childEntity
)
string
{
if
m
,
ok
:=
ent
.
Parent
()
.
(
pgs
.
Message
);
ok
{
return
fmt
.
Sprintf
(
"%s_%s"
,
fns
.
classBaseName
(
m
),
ent
.
Name
()
.
String
())
}
return
msg
.
Name
()
.
String
()
return
ent
.
Name
()
.
String
()
}
func
(
fns
CCFuncs
)
className
(
msg
pgs
.
Message
)
string
{
return
fns
.
packageName
(
msg
)
+
"::"
+
fns
.
classBaseName
(
msg
)
func
(
fns
CCFuncs
)
className
(
ent
childEntity
)
string
{
return
fns
.
packageName
(
ent
)
+
"::"
+
fns
.
classBaseName
(
ent
)
}
func
(
fns
CCFuncs
)
packageName
(
msg
pgs
.
Entity
)
string
{
...
...
This diff is collapsed.
Click to expand it.
templates/cc/repeated.go
+
1
-
1
View file @
4349a359
...
...
@@ -45,7 +45,7 @@ const repTpl = `
{{ if or $r.GetUnique (ne (.Elem "" "").Typ "none") }}
for (int i = 0; i < {{ accessor . }}.size(); i++) {
const
{{ $typ }}
& item = {{ accessor . }}.Get(i);
const
auto
& item = {{ accessor . }}.Get(i);
(void)item;
{{ if $r.GetUnique }}
...
...
This diff is collapsed.
Click to expand it.
tests/harness/cases/enums.proto
+
6
-
0
View file @
4349a359
...
...
@@ -39,3 +39,9 @@ message EnumNotIn { TestEnum val = 1 [(validate.rules).enum = { not_in: [1]
message
EnumAliasNotIn
{
TestEnumAlias
val
=
1
[(
validate.rules
)
.
enum
=
{
not_in
:
[
1
]}];
}
message
EnumExternal
{
other_package.Embed.Enumerated
val
=
1
[(
validate.rules
)
.
enum.defined_only
=
true
];
}
message
RepeatedEnumDefined
{
repeated
TestEnum
val
=
1
[(
validate.rules
)
.
repeated
.
items.enum.defined_only
=
true
];
}
message
RepeatedExternalEnumDefined
{
repeated
other_package.Embed.Enumerated
val
=
1
[(
validate.rules
)
.
repeated
.
items.enum.defined_only
=
true
];
}
message
MapEnumDefined
{
map
<
string
,
TestEnum
>
val
=
1
[(
validate.rules
)
.
map.values.enum.defined_only
=
true
];
}
message
MapExternalEnumDefined
{
map
<
string
,
other_package.Embed.Enumerated
>
val
=
1
[(
validate.rules
)
.
map.values.enum.defined_only
=
true
];
}
This diff is collapsed.
Click to expand it.
tests/harness/executor/cases.go
+
12
-
0
View file @
4349a359
...
...
@@ -917,6 +917,18 @@ var enumCases = []TestCase{
{
"enum external - defined_only - valid"
,
&
cases
.
EnumExternal
{
Val
:
other_package
.
Embed_VALUE
},
true
},
{
"enum external - defined_only - invalid"
,
&
cases
.
EnumExternal
{
Val
:
math
.
MaxInt32
},
false
},
{
"enum repeated - defined_only - valid"
,
&
cases
.
RepeatedEnumDefined
{
Val
:
[]
cases
.
TestEnum
{
cases
.
TestEnum_ONE
,
cases
.
TestEnum_TWO
}},
true
},
{
"enum repeated - defined_only - invalid"
,
&
cases
.
RepeatedEnumDefined
{
Val
:
[]
cases
.
TestEnum
{
cases
.
TestEnum_ONE
,
math
.
MaxInt32
}},
false
},
{
"enum repeated (external) - defined_only - valid"
,
&
cases
.
RepeatedExternalEnumDefined
{
Val
:
[]
other_package
.
Embed_Enumerated
{
other_package
.
Embed_VALUE
}},
true
},
{
"enum repeated (external) - defined_only - invalid"
,
&
cases
.
RepeatedExternalEnumDefined
{
Val
:
[]
other_package
.
Embed_Enumerated
{
math
.
MaxInt32
}},
false
},
{
"enum map - defined_only - valid"
,
&
cases
.
MapEnumDefined
{
Val
:
map
[
string
]
cases
.
TestEnum
{
"foo"
:
cases
.
TestEnum_TWO
}},
true
},
{
"enum map - defined_only - invalid"
,
&
cases
.
MapEnumDefined
{
Val
:
map
[
string
]
cases
.
TestEnum
{
"foo"
:
math
.
MaxInt32
}},
false
},
{
"enum map (external) - defined_only - valid"
,
&
cases
.
MapExternalEnumDefined
{
Val
:
map
[
string
]
other_package
.
Embed_Enumerated
{
"foo"
:
other_package
.
Embed_VALUE
}},
true
},
{
"enum map (external) - defined_only - invalid"
,
&
cases
.
MapExternalEnumDefined
{
Val
:
map
[
string
]
other_package
.
Embed_Enumerated
{
"foo"
:
math
.
MaxInt32
}},
false
},
}
var
messageCases
=
[]
TestCase
{
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Snippets