Skip to content

Commit

Permalink
Merge branch 'master' into robjs-structmerge
Browse files Browse the repository at this point in the history
  • Loading branch information
robshakir committed Oct 3, 2017
2 parents 7bbf1b3 + b99b926 commit c15fecd
Show file tree
Hide file tree
Showing 28 changed files with 222 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ YANG Type | Go Type | Notes
`int{8,16,32,64}` | `int{8,16,32,64}` |
`uint{8,16,32,64}` | `uint{8,16,32,64}` |
`bool` | `bool` |
`empty` | `bool` |
`empty` | `bool` (derived) |
`string` | `string` |
`union` | `interface{}` | A `union` is represented as an empty interface, with validation intending to be done whilst mapping into the `//ops/openconfig/lib/go` library.
`enumeration` | `int64` | Each enumeration is generated as a new type based on Go's int64, names are assigned to each value of the enumeration akin to the `proto` library.
`identityref` | `int64` | The identityref's "base" is mapped using the same process as the an enumeration leaf.
`decimal64` | `float64` |
`binary` | `[]byte` |
`binary` | `[]byte` (derived) |
`bits` | `interface{}` | TODO(robjs): Add support for `bits`, this is low priority as it is not used in any OpenConfig schema.

### YANG Lists
Expand Down
2 changes: 1 addition & 1 deletion scripts/coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

go get github.com/go-playground/overalls && go get github.com/mattn/goveralls

overalls -project=github.com/openconfig/ygot -covermode=count -debug
overalls -project=github.com/openconfig/ygot -covermode=count -ignore=".git,vendor,demo,experimental/ygotutils,generator,ytypes/schema_tests"
goveralls -coverprofile=overalls.coverprofile -service travis-ci


4 changes: 4 additions & 0 deletions ygen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ func TestSimpleStructs(t *testing.T) {
GenerateFakeRoot: true,
},
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/root-entities.formatted-txt"),
}, {
name: "module with empty leaf",
inFiles: []string{filepath.Join(TestRoot, "testdata/structs/empty.yang")},
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/empty.formatted-txt"),
}}

for _, tt := range tests {
Expand Down
5 changes: 4 additions & 1 deletion ygen/genstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ func newGenState() *genState {
return &genState{
// Mark the name that is used for the binary type as a reserved name
// within the output structs.
definedGlobals: map[string]bool{ygot.BinaryTypeName: true},
definedGlobals: map[string]bool{
ygot.BinaryTypeName: true,
ygot.EmptyTypeName: true,
},
uniqueDirectoryNames: make(map[string]string),
uniqueEnumeratedTypedefNames: make(map[string]string),
uniqueIdentityNames: make(map[string]string),
Expand Down
6 changes: 4 additions & 2 deletions ygen/goelements.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,10 @@ func (s *genState) yangTypeToGoType(args resolveTypeArgs, compressOCPaths bool)
return &mappedType{nativeType: "bool"}, nil
case yang.Yempty:
// Empty is a YANG type that either exists or doesn't, therefore
// map it to a boolean to indicate its presence or not.
return &mappedType{nativeType: "bool"}, nil
// map it to a boolean to indicate its presence or not. The empty
// type name uses a specific name in the generated code, such that
// it can be identified for marshalling.
return &mappedType{nativeType: ygot.EmptyTypeName}, nil
case yang.Ystring:
return &mappedType{nativeType: "string"}, nil
case yang.Yunion:
Expand Down
2 changes: 1 addition & 1 deletion ygen/goelements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func TestYangTypeToGoType(t *testing.T) {
}, {
name: "simple empty resolution",
in: &yang.YangType{Kind: yang.Yempty, Name: "empty"},
want: &mappedType{nativeType: "bool"},
want: &mappedType{nativeType: "YANGEmpty"},
}, {
name: "simple boolean resolution",
in: &yang.YangType{Kind: yang.Ybool, Name: "bool"},
Expand Down
15 changes: 12 additions & 3 deletions ygen/gogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,16 @@ import (
{{- end }}
)
// Binary is a type that is used for fields that have a YANG type of
// {{ .BinaryTypeName }} is a type that is used for fields that have a YANG type of
// binary. It is used such that binary fields can be distinguished from
// leaf-lists of uint8s (which are mapped to []uint8, equivalent to
// []byte in reflection).
type Binary []byte
type {{ .BinaryTypeName }} []byte
// {{ .EmptyTypeName }} is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type {{ .EmptyTypeName }} bool
{{- if .GenerateSchema }}
Expand Down Expand Up @@ -709,6 +714,8 @@ func writeGoHeader(yangFiles, includePaths []string, cfg GeneratorConfig) (strin
GeneratingBinary string // GeneratingBinary is the name of the binary generating the code.
GenerateSchema bool // GenerateSchema stores whether the generator requested that the schema was to be stored with the output code.
GoOptions GoOpts // GoOptions stores additional Go-specific options for the output code, including package paths.
BinaryTypeName string // BinaryTypeName is the name of the type used for YANG binary types.
EmptyTypeName string // EmptyTypeName is the name of the type used for YANG empty types.
}{
PackageName: cfg.PackageName,
YANGFiles: yangFiles,
Expand All @@ -717,6 +724,8 @@ func writeGoHeader(yangFiles, includePaths []string, cfg GeneratorConfig) (strin
GeneratingBinary: cfg.Caller,
GenerateSchema: cfg.GenerateJSONSchema,
GoOptions: cfg.GoOptions,
BinaryTypeName: ygot.BinaryTypeName,
EmptyTypeName: ygot.EmptyTypeName,
}

var buf bytes.Buffer
Expand Down Expand Up @@ -894,7 +903,7 @@ func writeGoStruct(targetStruct *yangDirectory, goStructElements map[string]*yan
// code using a slice of the type that the element was mapped to.
fType = fmt.Sprintf("[]%s", fType)
scalarField = false
case mtype.isEnumeratedValue == true, mtype.nativeType == "interface{}", mtype.nativeType == "Binary":
case mtype.isEnumeratedValue == true, mtype.nativeType == "interface{}", mtype.nativeType == ygot.BinaryTypeName, mtype.nativeType == ygot.EmptyTypeName:
// If the value is an enumerated value, then we did not represent it
// as a pointer within the struct, so mark it as a scalar field such
// that the template does not attempt to prefix it with an asterisk.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

var (
SchemaTree map[string]*yang.Entry
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

var (
SchemaTree map[string]*yang.Entry
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

var (
SchemaTree map[string]*yang.Entry
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

var (
SchemaTree map[string]*yang.Entry
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

var (
SchemaTree map[string]*yang.Entry
)
Expand Down
5 changes: 5 additions & 0 deletions ygen/testdata/structs/choice-case-example.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// ChoiceCaseExample_ChoiceCaseAnonymousCase represents the /choice-case-example/choice-case-anonymous-case YANG schema element.
type ChoiceCaseExample_ChoiceCaseAnonymousCase struct {
A *string `path:"/choice-case-anonymous-case/a" module:"choice-case-example"`
Expand Down
62 changes: 62 additions & 0 deletions ygen/testdata/structs/empty.formatted-txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Package ocstructs is a generated package which contains definitions
of structs which represent a YANG schema. The generated schema can be
compressed by a series of transformations (compression was false
in this case).

This package was generated by codegen-tests
using the following YANG input files:
- testdata/structs/empty.yang
Imported modules were sourced from:
*/
package ocstructs

import (
"encoding/json"
"fmt"
"reflect"

"github.com/openconfig/ygot/ygot"
)

// Binary is a type that is used for fields that have a YANG type of
// binary. It is used such that binary fields can be distinguished from
// leaf-lists of uint8s (which are mapped to []uint8, equivalent to
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// Empty_Test represents the /empty/test YANG schema element.
type Empty_Test struct {
Config *Empty_Test_Config `path:"/test/config" module:"empty"`
State *Empty_Test_State `path:"/test/state" module:"empty"`
}

// IsYANGGoStruct ensures that Empty_Test implements the yang.GoStruct
// interface. This allows functions that need to handle this struct to
// identify it as being generated by ygen.
func (*Empty_Test) IsYANGGoStruct() {}

// Empty_Test_Config represents the /empty/test/config YANG schema element.
type Empty_Test_Config struct {
E YANGEmpty `path:"e" module:"empty"`
}

// IsYANGGoStruct ensures that Empty_Test_Config implements the yang.GoStruct
// interface. This allows functions that need to handle this struct to
// identify it as being generated by ygen.
func (*Empty_Test_Config) IsYANGGoStruct() {}

// Empty_Test_State represents the /empty/test/state YANG schema element.
type Empty_Test_State struct {
E YANGEmpty `path:"e" module:"empty"`
}

// IsYANGGoStruct ensures that Empty_Test_State implements the yang.GoStruct
// interface. This allows functions that need to handle this struct to
// identify it as being generated by ygen.
func (*Empty_Test_State) IsYANGGoStruct() {}
19 changes: 19 additions & 0 deletions ygen/testdata/structs/empty.yang
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module empty {
prefix "e";
namespace "urn:e";

grouping e-cfg {
leaf e { type empty; }
}

container test {
container config {
uses e-cfg;
}

container state {
config false;
uses e-cfg;
}
}
}
5 changes: 5 additions & 0 deletions ygen/testdata/structs/openconfig-augmented.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// Device represents the /device YANG schema element.
type Device struct {
Native *Native `path:"" rootname:"native" module:"openconfig-simple-target"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// BGP represents the /openconfig-camelcase/bgp YANG schema element.
type BGP struct {
Neighbor map[string]*BGP_Neighbor `path:"/bgp/neighbors/neighbor" module:"openconfig-camelcase"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// Foo represents the /openconfig-enumcamelcase/foo YANG schema element.
type Foo struct {
Bar E_OpenConfigCamelCase_OpenconfigEnumcamelcase_Bar `path:"/foo/bar" module:"openconfig-enumcamelcase"`
Expand Down
5 changes: 5 additions & 0 deletions ygen/testdata/structs/openconfig-fakeroot-nc.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// Device represents the /device YANG schema element.
type Device struct {
Interfaces *OpenconfigFakeroot_Interfaces `path:"" rootname:"interfaces" module:"openconfig-fakeroot"`
Expand Down
5 changes: 5 additions & 0 deletions ygen/testdata/structs/openconfig-fakeroot.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// Device represents the /device YANG schema element.
type Device struct {
Interface map[string]*Interface `path:"interfaces/interface" rootname:"interface" module:"openconfig-fakeroot"`
Expand Down
5 changes: 5 additions & 0 deletions ygen/testdata/structs/openconfig-list-enum-key.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// Top represents the /openconfig-list-enum-key/top YANG schema element.
type Top struct {
Ekm map[Top_Ekm_Key]*Top_Ekm `path:"/top/multi-key/ekm" module:"openconfig-list-enum-key"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// OpenconfigSimple_Parent represents the /openconfig-simple/parent YANG schema element.
type OpenconfigSimple_Parent struct {
Child *OpenconfigSimple_Parent_Child `path:"/parent/child" module:"openconfig-simple"`
Expand Down
5 changes: 5 additions & 0 deletions ygen/testdata/structs/openconfig-simple.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// Parent represents the /openconfig-simple/parent YANG schema element.
type Parent struct {
Child *Parent_Child `path:"/parent/child" module:"openconfig-simple"`
Expand Down
5 changes: 5 additions & 0 deletions ygen/testdata/structs/openconfig-unione.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// Platform represents the /openconfig-unione/platform YANG schema element.
type Platform struct {
Component *Platform_Component `path:"/platform/component" module:"openconfig-unione"`
Expand Down
5 changes: 5 additions & 0 deletions ygen/testdata/structs/openconfig-withlist.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
// []byte in reflection).
type Binary []byte

// YANGEmpty is a type that is used for fields that have a YANG type of
// empty. It is used such that empty fields can be distinguished from boolean fields
// in the generated code.
type YANGEmpty bool

// Model represents the /openconfig-withlist/model YANG schema element.
type Model struct {
MultiKey map[Model_MultiKey_Key]*Model_MultiKey `path:"/model/b/multi-key" module:"openconfig-withlist"`
Expand Down

0 comments on commit c15fecd

Please sign in to comment.