Skip to content

Commit

Permalink
Run go generate ./... for CI check (#703)
Browse files Browse the repository at this point in the history
* Run go generate ./... for CI check

* Regenerate exampleoc but don't depend on it for tests

* Delete `StoreRawSchema` from `GeneratorConfig` since it's not being used (#705)

* Delete `StoreRawSchema` from `GeneratorConfig` since it's not being used

I don't see the value of why we need this flag: is it to save memory?
For `exampleoc` this variable took 27MB, a negligible amount.

* Reorganize Code Generation flags and Delete `ygen.GeneratorConfig` (#706)

* ygen now only has `IROptions`.
* `CodeGenerator` in gogen/protogen now carry IROptions and their language-specific post-IR generation options.
* Code generation specific flags that are not used during IR generation are moved to `gogen` and `protogen`, duplicating if used by both (e.g. CallerName).
* `SkipEnumDeduplication` is moved from ParseOptions to TransformationOptions.
  • Loading branch information
wenovus committed Jun 9, 2022
1 parent c22d85c commit 947677e
Show file tree
Hide file tree
Showing 21 changed files with 961 additions and 836 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/go.yml
Expand Up @@ -118,6 +118,10 @@ jobs:
test-go-build $f $go_file
done
- name: Make sure exampleoc doesn't error or panic
run: go generate ./...
working-directory: go/src/github.com/openconfig/ygot

static_analysis:
name: Static Analysis
runs-on: ubuntu-latest
Expand Down
32 changes: 11 additions & 21 deletions Makefile
@@ -1,30 +1,20 @@
# ygot Makefile
#
# This makefile is used by Travis CI to run tests against the ygot library.
# This makefile is used by GitHub Actions CI to run tests against the ygot library.
#
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

test:
go test ./...
generate:
cd ${ROOT_DIR}/demo/getting_started && SRCDIR=${ROOT_DIR} go generate
cd ${ROOT_DIR}/proto/ywrapper && SRCDIR=${ROOT_DIR} go generate
cd $(ROOT_DIR)/proto/yext && SRCDIR=${ROOT_DIR} go generate
cd $(ROOT_DIR)/demo/uncompressed && SRCDIR=${ROOT_DIR} go generate
cd $(ROOT_DIR)/demo/protobuf_getting_started && SRCDIR=${ROOT_DIR} ./update.sh
cd $(ROOT_DIR)/integration_tests/uncompressed && SRCDIR=${ROOT_DIR} go generate
cd $(ROOT_DIR)/integration_tests/annotations/apb && SRCDIR=${ROOT_DIR} go generate
cd $(ROOT_DIR)/integration_tests/annotations/proto2apb && SRCDIR=${ROOT_DIR} go generate
go generate ./demo/getting_started
go generate ./proto/ywrapper
go generate ./proto/yext
go generate ./demo/uncompressed
go generate ./demo/protobuf_getting_started
go generate ./integration_tests/uncompressed
go generate ./integration_tests/annotations/apb
go generate ./integration_tests/annotations/proto2apb
clean:
rm -f ${ROOT_DIR}/demo/getting_started/pkg/ocdemo/oc.go
rm -f ${ROOT_DIR}/demo/uncompressed/pkg/demo/uncompressed.go
deps:
go get -t -d ./ygot
go get -t -d ./ygen
go get -t -d ./generator
go get -t -d ./proto_generator
go get -t -d ./exampleoc
go get -t -d ./ytypes
go get -t -d ./demo/gnmi_telemetry
rm -f demo/getting_started/pkg/ocdemo/oc.go
rm -f demo/uncompressed/pkg/demo/uncompressed.go
install: deps generate
all: clean deps generate test
20 changes: 10 additions & 10 deletions demo/getting_started/interfaces_test.go
Expand Up @@ -22,23 +22,23 @@ var TestRoot string
func TestGenerateCode(t *testing.T) {
tests := []struct {
name string
inConfig *ygen.GeneratorConfig
inGoOpts *gogen.GoOpts
inIROpts ygen.IROptions
inGoOpts gogen.GoOpts
inFiles []string
inPaths []string
}{{
name: "openconfig interfaces",
inConfig: &ygen.GeneratorConfig{
inIROpts: ygen.IROptions{
ParseOptions: ygen.ParseOpts{
ExcludeModules: []string{"ietf-interfaces"},
},
TransformationOptions: ygen.TransformationOpts{
CompressBehaviour: genutil.PreferIntendedConfig,
GenerateFakeRoot: true,
},
GenerateJSONSchema: true,
},
inGoOpts: &gogen.GoOpts{
inGoOpts: gogen.GoOpts{
GenerateJSONSchema: true,
GenerateSimpleUnions: true,
},
inFiles: []string{
Expand All @@ -48,16 +48,16 @@ func TestGenerateCode(t *testing.T) {
inPaths: []string{filepath.Join(TestRoot, "yang")},
}, {
name: "openconfig interfaces with no compression",
inConfig: &ygen.GeneratorConfig{
inIROpts: ygen.IROptions{
ParseOptions: ygen.ParseOpts{
ExcludeModules: []string{"ietf-interfaces"},
},
TransformationOptions: ygen.TransformationOpts{
GenerateFakeRoot: true,
},
GenerateJSONSchema: true,
},
inGoOpts: &gogen.GoOpts{
inGoOpts: gogen.GoOpts{
GenerateJSONSchema: true,
GenerateSimpleUnions: true,
},
inFiles: []string{
Expand All @@ -68,10 +68,10 @@ func TestGenerateCode(t *testing.T) {
}}

for _, tt := range tests {
cg := gogen.NewCodeGenerator(tt.inConfig, tt.inGoOpts)
cg := gogen.New("", tt.inIROpts, tt.inGoOpts)
got, err := cg.Generate(tt.inFiles, tt.inPaths)
if err != nil {
t.Errorf("%s: Generate(%v, %v): Config: %v, got unexpected error: %v", tt.name, tt.inFiles, tt.inPaths, tt.inConfig, err)
t.Errorf("%s: Generate(%v, %v): Config: %v, got unexpected error: %v", tt.name, tt.inFiles, tt.inPaths, tt.inIROpts, err)
continue
}

Expand Down
2 changes: 2 additions & 0 deletions demo/protobuf_getting_started/demo.go
Expand Up @@ -17,6 +17,8 @@
// to generate a Protobuf form of the OpenConfig RIB model.
package main

//go:generate ./update.sh

import (
"fmt"

Expand Down
2 changes: 0 additions & 2 deletions demo/protobuf_getting_started/update.sh
Expand Up @@ -17,8 +17,6 @@ go run ../../proto_generator/protogenerator.go \
-base_import_path="github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto" \
-go_package_base="github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto" \
-path=yang -output_dir=ribproto \
-typedef_enum_with_defmod \
-consistent_union_enum_names \
-enum_package_name=enums -package_name=openconfig \
-exclude_modules=ietf-interfaces \
yang/rib/openconfig-rib-bgp.yang
Expand Down
44 changes: 23 additions & 21 deletions generator/generator.go
Expand Up @@ -325,29 +325,30 @@ func main() {
}

// Perform the code generation.
cg := gogen.NewCodeGenerator(&ygen.GeneratorConfig{
ParseOptions: ygen.ParseOpts{
ExcludeModules: modsExcluded,
SkipEnumDeduplication: *skipEnumDedup,
YANGParseOptions: yang.Options{
IgnoreSubmoduleCircularDependencies: *ignoreCircDeps,
cg := gogen.New(
"",
ygen.IROptions{
ParseOptions: ygen.ParseOpts{
ExcludeModules: modsExcluded,
YANGParseOptions: yang.Options{
IgnoreSubmoduleCircularDependencies: *ignoreCircDeps,
},
},
TransformationOptions: ygen.TransformationOpts{
CompressBehaviour: compressBehaviour,
GenerateFakeRoot: *generateFakeRoot,
FakeRootName: *fakeRootName,
SkipEnumDeduplication: *skipEnumDedup,
ShortenEnumLeafNames: *shortenEnumLeafNames,
EnumOrgPrefixesToTrim: enumOrgPrefixesToTrim,
UseDefiningModuleForTypedefEnumNames: *useDefiningModuleForTypedefEnumNames,
EnumerationsUseUnderscores: true,
},
},
TransformationOptions: ygen.TransformationOpts{
CompressBehaviour: compressBehaviour,
IgnoreShadowSchemaPaths: *ignoreShadowSchemaPaths,
GenerateFakeRoot: *generateFakeRoot,
FakeRootName: *fakeRootName,
ShortenEnumLeafNames: *shortenEnumLeafNames,
EnumOrgPrefixesToTrim: enumOrgPrefixesToTrim,
UseDefiningModuleForTypedefEnumNames: *useDefiningModuleForTypedefEnumNames,
EnumerationsUseUnderscores: true,
},
PackageName: *packageName,
GenerateJSONSchema: *generateSchema,
IncludeDescriptions: *includeDescriptions,
},
&gogen.GoOpts{
gogen.GoOpts{
PackageName: *packageName,
GenerateJSONSchema: *generateSchema,
IncludeDescriptions: *includeDescriptions,
YgotImportPath: *ygotImportPath,
YtypesImportPath: *ytypesImportPath,
GoyangImportPath: *goyangImportPath,
Expand All @@ -364,6 +365,7 @@ func main() {
GenerateSimpleUnions: *generateSimpleUnions,
IncludeModelData: *includeModelData,
AppendEnumSuffixForSimpleUnionEnums: *appendEnumSuffixForSimpleUnionEnums,
IgnoreShadowSchemaPaths: *ignoreShadowSchemaPaths,
},
)

Expand Down
58 changes: 34 additions & 24 deletions gogen/codegen.go
Expand Up @@ -15,19 +15,31 @@ import (
// CodeGenerator is a structure that is used to pass arguments as to
// how the output Go code should be generated.
type CodeGenerator struct {
// Config stores the configuration parameters used for code generation.
Config ygen.GeneratorConfig
// Caller is the name of the binary calling the generator library, it is
// included in the header of output files for debugging purposes. If a
// string is not specified, the location of the library is utilised.
Caller string
// IROptions stores the configuration parameters used for IR generation.
IROptions ygen.IROptions
// GoOptions stores a struct which stores Go code generation specific
// options for the code generaton.
// options for code generaton post IR generation.
GoOptions GoOpts
}

// GoOpts stores Go specific options for the code generation library.
type GoOpts struct {
// PackageName is the name that should be used for the generating package.
PackageName string
// GenerateJSONSchema stores a boolean which defines whether to generate
// the JSON corresponding to the YANG schema parsed to generate the
// output code.
GenerateJSONSchema bool
// IncludeDescriptions specifies that YANG entry descriptions are added
// to the JSON schema. Is false by default, to reduce the size of generated schema
IncludeDescriptions bool
// SchemaVarName is the name for the variable which stores the compressed
// JSON schema in the generated Go code. JSON schema output is only
// produced if the GenerateJSONSchema YANGCodeGenerator field is set to
// true.
// produced if the GenerateJSONSchema field is set to true.
SchemaVarName string
// GoyangImportPath specifies the path that should be used in the generated
// code for importing the goyang/pkg/yang package.
Expand Down Expand Up @@ -100,6 +112,10 @@ type GoOpts struct {
// only applies when useDefiningModuleForTypedefEnumNames is also set
// to true.
AppendEnumSuffixForSimpleUnionEnums bool
// IgnoreShadowSchemaPaths indicates whether when OpenConfig path
// compression is enabled, that the shadowed paths are to be ignored
// while while unmarshalling.
IgnoreShadowSchemaPaths bool
}

// GeneratedCode contains generated code snippets that can be processed by the calling
Expand Down Expand Up @@ -133,25 +149,19 @@ type GeneratedCode struct {
// - structname - the name of the struct that was generated for the schema element.
JSONSchemaCode string
// RawJSONSchema stores the JSON document which is serialised and stored in JSONSchemaCode.
// It is populated only if the StoreRawSchema CodeGenerator boolean is set to true.
RawJSONSchema []byte
// EnumTypeMap is a Go map that allows YANG schemapaths to be mapped to reflect.Type values.
EnumTypeMap string
}

// NewCodeGenerator returns a new instance of the CodeGenerator
// New returns a new instance of the CodeGenerator
// struct to the calling function.
func NewCodeGenerator(c *ygen.GeneratorConfig, goopts *GoOpts) *CodeGenerator {
cg := &CodeGenerator{}

if c != nil {
cg.Config = *c
}
if goopts != nil {
cg.GoOptions = *goopts
func New(callerName string, opts ygen.IROptions, goOpts GoOpts) *CodeGenerator {
return &CodeGenerator{
Caller: callerName,
IROptions: opts,
GoOptions: goOpts,
}

return cg
}

// checkForBinaryKeys returns a non-empty list of errors if the input directory
Expand Down Expand Up @@ -187,8 +197,8 @@ func checkForBinaryKeys(dir *ygen.ParsedDirectory) []error {
// If errors are encountered during code generation, an error is returned.
func (cg *CodeGenerator) Generate(yangFiles, includePaths []string) (*GeneratedCode, util.Errors) {
opts := ygen.IROptions{
ParseOptions: cg.Config.ParseOptions,
TransformationOptions: cg.Config.TransformationOptions,
ParseOptions: cg.IROptions.ParseOptions,
TransformationOptions: cg.IROptions.TransformationOptions,
NestedDirectories: false,
AbsoluteMapPaths: false,
AppendEnumSuffixForSimpleUnionEnums: cg.GoOptions.AppendEnumSuffixForSimpleUnionEnums,
Expand All @@ -201,8 +211,8 @@ func (cg *CodeGenerator) Generate(yangFiles, includePaths []string) (*GeneratedC
}

var rootName string
if cg.Config.TransformationOptions.GenerateFakeRoot {
rootName = cg.Config.TransformationOptions.FakeRootName
if cg.IROptions.TransformationOptions.GenerateFakeRoot {
rootName = cg.IROptions.TransformationOptions.FakeRootName
if rootName == "" {
rootName = igenutil.DefaultRootName
}
Expand Down Expand Up @@ -240,7 +250,7 @@ func (cg *CodeGenerator) Generate(yangFiles, includePaths []string) (*GeneratedC
codegenErr = util.AppendErrs(codegenErr, errs)
continue
}
structOut, errs := writeGoStruct(dir, ir.Directories, generatedUnions, opts.TransformationOptions.IgnoreShadowSchemaPaths, cg.GoOptions, cg.Config.GenerateJSONSchema)
structOut, errs := writeGoStruct(dir, ir.Directories, generatedUnions, cg.GoOptions)
if errs != nil {
codegenErr = util.AppendErrs(codegenErr, errs)
continue
Expand Down Expand Up @@ -301,9 +311,9 @@ func (cg *CodeGenerator) Generate(yangFiles, includePaths []string) (*GeneratedC
var rawSchema []byte
var jsonSchema string
var enumTypeMapCode string
if cg.Config.GenerateJSONSchema {
if cg.GoOptions.GenerateJSONSchema {
var err error
rawSchema, err = ir.SchemaTree(cg.Config.IncludeDescriptions)
rawSchema, err = ir.SchemaTree(cg.GoOptions.IncludeDescriptions)
if err != nil {
codegenErr = util.AppendErr(codegenErr, fmt.Errorf("error marshalling JSON schema: %v", err))
}
Expand Down

0 comments on commit 947677e

Please sign in to comment.