Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support explicitly setting field tags #339

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions ygen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"text/template"

Expand Down Expand Up @@ -1094,6 +1095,38 @@ func safeProtoIdentifierName(name string) string {

// protoTagForEntry returns a protobuf tag value for the entry e.
func protoTagForEntry(e *yang.Entry) (uint32, error) {
var offset uint32
for _, ext := range e.Exts {
if ext.Keyword == "occodegenext:field-number-offset" {
off, err := strconv.ParseUint(ext.Argument, 10, 32)
if err != nil {
return 0, fmt.Errorf("could not parse %s %s: %s", ext.Keyword, ext.Argument, err)
}
offset = uint32(off)
if offset == 0 {
return 0, fmt.Errorf("%s cannot be 0", ext.Keyword)
}
break
}
}
for _, ext := range e.Exts {
if ext.Keyword == "occodegenext:field-number" {
fn, err := strconv.ParseUint(ext.Argument, 10, 32)
if err != nil {
return 0, fmt.Errorf("could not parse %s %s: %s", ext.Keyword, ext.Argument, err)
}
if fn == 0 {
return 0, fmt.Errorf("%s cannot be 0", ext.Keyword)
}
annotatedFieldNumber := uint32(fn) + offset
if annotatedFieldNumber >= 19000 && annotatedFieldNumber <= 19999 {
return 0, fmt.Errorf("%s field number %d in Protobuf reserved range",
e.Name, annotatedFieldNumber)
}
return annotatedFieldNumber, nil
}
}

return fieldTag(e.Path())
}

Expand Down
96 changes: 96 additions & 0 deletions ygen/protogen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,102 @@ func TestGenProto3Msg(t *testing.T) {
}},
},
},
}, {
name: "simple message with only scalar fields and field number extensions",
inMsg: &Directory{
Name: "MessageName",
Entry: &yang.Entry{
Name: "message-name",
Dir: map[string]*yang.Entry{},
Kind: yang.DirectoryEntry,
},
Fields: map[string]*yang.Entry{
"field-one": {
Name: "field-one",
Type: &yang.YangType{Kind: yang.Ystring},
Exts: []*yang.Statement{
{
Keyword: "occodegenext:field-number-offset",
Argument: "100",
},
},
},
"field-two": {
Name: "field-two",
Type: &yang.YangType{Kind: yang.Yint8},
Exts: []*yang.Statement{
{
Keyword: "occodegenext:field-number",
Argument: "1",
},
},
},
"field-three": {
Name: "field-three",
Type: &yang.YangType{Kind: yang.Yint8},
Exts: []*yang.Statement{
{
Keyword: "occodegenext:field-number",
Argument: "1",
},
{
Keyword: "occodegenext:field-number-offset",
Argument: "100",
},
},
},
},
Path: []string{"", "root", "message-name"},
},
inBasePackage: "base",
inEnumPackage: "enums",
wantMsgs: map[string]*protoMsg{
"MessageName": {
Name: "MessageName",
YANGPath: "/root/message-name",
Fields: []*protoMsgField{{
Tag: 410095931,
Name: "field_one",
Type: "ywrapper.StringValue",
}, {
Tag: 1,
Name: "field_two",
Type: "ywrapper.IntValue",
}, {
Tag: 101,
Name: "field_three",
Type: "ywrapper.IntValue",
}},
},
},
}, {
name: "message with field number in reserved range",
wenovus marked this conversation as resolved.
Show resolved Hide resolved
inMsg: &Directory{
Name: "MessageName",
Entry: &yang.Entry{
Name: "message-name",
Dir: map[string]*yang.Entry{},
Kind: yang.DirectoryEntry,
},
Fields: map[string]*yang.Entry{
"field-one": {
Name: "field-one",
Type: &yang.YangType{Kind: yang.Ystring},
Exts: []*yang.Statement{
{
Keyword: "occodegenext:field-number",
Argument: "1",
},
{
Keyword: "occodegenext:field-number-offset",
Argument: "18999",
},
},
},
},
Path: []string{"", "root", "message-name"},
},
wantErr: true,
}}

for _, tt := range tests {
Expand Down