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

Make Schema Tree public #895

Merged
merged 6 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions ygen/schematree.go → yangschema/yangschema.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Google Inc.
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ygen
package yangschema

import (
"bytes"
Expand All @@ -24,19 +24,19 @@ import (
"github.com/openconfig/ygot/util"
)

// schemaTree contains a ctree.Tree that stores a copy of the YANG schema tree
// Tree contains a ctree.Tree that stores a copy of the YANG schema tree
// containing only leaf entries, such that schema paths can be referenced.
type schemaTree struct {
type Tree struct {
ctree.Tree
}

// buildSchemaTree maps a set of yang.Entry pointers into a ctree structure.
// BuildTree maps a set of yang.Entry pointers into a ctree structure.
// Only leaf or leaflist values are mapped, since these are the only entities
// that can be referenced by XPATH expressions within a YANG schema.
// It returns an error if there is duplication within the set of entries. The
// paths that are used within the schema are represented as a slice of strings.
func buildSchemaTree(entries []*yang.Entry) (*schemaTree, error) {
t := &schemaTree{}
func BuildTree(entries []*yang.Entry) (*Tree, error) {
t := &Tree{}
for _, e := range entries {
pp := strings.Split(e.Path(), "/")
// We only want to find entities that are at the root of the
Expand All @@ -62,12 +62,12 @@ func buildSchemaTree(entries []*yang.Entry) (*schemaTree, error) {
return t, nil
}

// resolveLeafrefTarget takes an input path and context entry and
// ResolveLeafrefTarget takes an input path and context entry and
// determines the type of the leaf that is referred to by the path, such that
// it can be mapped to a native language type. It returns the yang.YangType that
// is associated with the target, and the target yang.Entry, such that the
// caller can map this to the relevant language type.
func (t *schemaTree) resolveLeafrefTarget(path string, contextEntry *yang.Entry) (*yang.Entry, error) {
func (t *Tree) ResolveLeafrefTarget(path string, contextEntry *yang.Entry) (*yang.Entry, error) {
if t == nil {
// This should not be possible if the calling code generation is
// well structured and builds the schematree during parsing of YANG
Expand Down Expand Up @@ -95,7 +95,7 @@ func (t *schemaTree) resolveLeafrefTarget(path string, contextEntry *yang.Entry)

// schemaTreeChildrenAdd adds the children of the supplied yang.Entry to the
// supplied ctree.Tree recursively.
func schemaTreeChildrenAdd(t *schemaTree, e *yang.Entry) error {
func schemaTreeChildrenAdd(t *Tree, e *yang.Entry) error {
for _, ch := range util.Children(e) {
chPath := strings.Split(ch.Path(), "/")
// chPath is of the form []string{"", "module", "entity", "child"}
Expand Down
16 changes: 8 additions & 8 deletions ygen/schematree_test.go → yangschema/yangschema_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 Google Inc.
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ygen
package yangschema

import (
"testing"
Expand Down Expand Up @@ -118,16 +118,16 @@ func TestBuildSchemaTree(t *testing.T) {
}}

for _, tt := range tests {
got, err := buildSchemaTree(tt.inEntries)
got, err := BuildTree(tt.inEntries)
if err != nil {
t.Errorf("%s: buildSchemaTree(%v): got unexpected error building tree: %v", tt.name, tt.inEntries, err)
t.Errorf("%s: BuildTree(%v): got unexpected error building tree: %v", tt.name, tt.inEntries, err)
continue
}

for _, want := range tt.wantElements {
gotElement := got.GetLeafValue(want.path)
if diff := pretty.Compare(gotElement, want.value); diff != "" {
t.Errorf("%s: buildSchemaTree(%v): got incorrect value for element %v, diff(-got,+want)\n:%s", tt.name, tt.inEntries, want.path, diff)
t.Errorf("%s: BuildTree(%v): got incorrect value for element %v, diff(-got,+want)\n:%s", tt.name, tt.inEntries, want.path, diff)
continue
}
}
Expand Down Expand Up @@ -221,11 +221,11 @@ func TestResolveLeafrefTargetType(t *testing.T) {
for _, tt := range tests {
// Since we are outside of the build of a module, need to initialise
// the schematree.
st, err := buildSchemaTree(tt.inEntries)
st, err := BuildTree(tt.inEntries)
if err != nil {
t.Errorf("%s: buildSchemaTree(%v): got unexpected error: %v", tt.name, tt.inEntries, err)
t.Errorf("%s: BuildTree(%v): got unexpected error: %v", tt.name, tt.inEntries, err)
}
got, err := st.resolveLeafrefTarget(tt.inPath, tt.inContextEntry)
got, err := st.ResolveLeafrefTarget(tt.inPath, tt.inContextEntry)
if err != nil {
if !tt.wantErr {
t.Errorf("%s: resolveLeafrefTargetPath(%v, %v): got unexpected error: %v", tt.name, tt.inPath, tt.inContextEntry, err)
Expand Down
5 changes: 3 additions & 2 deletions ygen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/genutil"
"github.com/openconfig/ygot/util"
"github.com/openconfig/ygot/yangschema"

"github.com/openconfig/ygot/internal/igenutil"

Expand Down Expand Up @@ -195,7 +196,7 @@ type mappedYANGDefinitions struct {
enumEntries map[string]*yang.Entry
// schematree is a copy of the YANG schema tree, containing only leaf
// entries, such that schema paths can be referenced.
schematree *schemaTree
schematree *yangschema.Tree
// modules is the set of parsed YANG modules that are being processed as part of the
// code generatio, expressed as a slice of yang.Entry pointers.
modules []*yang.Entry
Expand Down Expand Up @@ -255,7 +256,7 @@ func mappedDefinitions(yangFiles, includePaths []string, opts IROptions) (*mappe
// Build the schematree for the modules provided - we build for all of the
// root elements, since we might need to reference a part of the schema that
// we are not outputting for leafref lookups.
st, err := buildSchemaTree(treeElems)
st, err := yangschema.BuildTree(treeElems)
if err != nil {
return nil, []error{err}
}
Expand Down
7 changes: 4 additions & 3 deletions ygen/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/genutil"
"github.com/openconfig/ygot/util"
"github.com/openconfig/ygot/yangschema"
"github.com/openconfig/ygot/ygot"
)

Expand Down Expand Up @@ -142,10 +143,10 @@ func GetOrderedPathDirectories(directory map[string]*Directory) []string {
}

// getOrderedDirDetails takes in a language-specific LangMapper, a map of
// Directory objects containing the raw AST information, a schemaTree, and IR
// Directory objects containing the raw AST information, a SchemaTree, and IR
// generation options, and returns a map of ParsedDirectory objects that form
// the primary component of ygen's IR output.
func getOrderedDirDetails(langMapper LangMapper, directory map[string]*Directory, schematree *schemaTree, opts IROptions) (map[string]*ParsedDirectory, error) {
func getOrderedDirDetails(langMapper LangMapper, directory map[string]*Directory, schematree *yangschema.Tree, opts IROptions) (map[string]*ParsedDirectory, error) {
dirDets := map[string]*ParsedDirectory{}
for _, dirPath := range GetOrderedPathDirectories(directory) {
dir := directory[dirPath]
Expand Down Expand Up @@ -237,7 +238,7 @@ func getOrderedDirDetails(langMapper LangMapper, directory map[string]*Directory

var target *yang.Entry
if field.Type != nil && field.Type.Kind == yang.Yleafref {
if target, err = schematree.resolveLeafrefTarget(field.Type.Path, field); err != nil {
if target, err = schematree.ResolveLeafrefTarget(field.Type.Path, field); err != nil {
return nil, fmt.Errorf("unable to resolve leafref field: %v", err)
}
}
Expand Down
11 changes: 6 additions & 5 deletions ygen/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/genutil"
"github.com/openconfig/ygot/yangschema"
"github.com/openconfig/ygot/ygot"
)

Expand Down Expand Up @@ -103,7 +104,7 @@ type LangMapperBaseSetup interface {
// setSchemaTree is used to supply a copy of the YANG schema tree to
// the mapped such that leaves of type leafref can be resolved to
// their target leaves.
setSchemaTree(*schemaTree)
setSchemaTree(*yangschema.Tree)

// InjectEnumSet is intended to be called by unit tests in order to set up the
// LangMapperBase such that generated enumeration/identity names can be looked
Expand All @@ -128,7 +129,7 @@ type LangMapperBase struct {

// schematree is a copy of the YANG schema tree, containing only leaf
// entries, such that schema paths can be referenced.
schematree *schemaTree
schematree *yangschema.Tree
}

// setEnumSet is used to supply a set of enumerated values to the
Expand All @@ -150,7 +151,7 @@ func (s *LangMapperBase) setEnumSet(e *enumSet) {
// In testing contexts outside of GenerateIR, however, the corresponding
// exported Inject method needs to be called in order for certain built-in
// methods of LangMapperBase to be available for use.
func (s *LangMapperBase) setSchemaTree(st *schemaTree) {
func (s *LangMapperBase) setSchemaTree(st *yangschema.Tree) {
s.schematree = st
}

Expand All @@ -175,7 +176,7 @@ func (s *LangMapperBase) InjectEnumSet(entries map[string]*yang.Entry, compressP
// set of yang.Entry pointers into a ctree structure.
// It returns an error if there is duplication within the set of entries.
func (s *LangMapperBase) InjectSchemaTree(entries []*yang.Entry) error {
schematree, err := buildSchemaTree(entries)
schematree, err := yangschema.BuildTree(entries)
if err != nil {
return err
}
Expand All @@ -192,7 +193,7 @@ func (s *LangMapperBase) InjectSchemaTree(entries []*yang.Entry) error {
// In testing contexts, this function requires InjectSchemaTree to be called
// prior to being usable.
func (b *LangMapperBase) ResolveLeafrefTarget(path string, contextEntry *yang.Entry) (*yang.Entry, error) {
return b.schematree.resolveLeafrefTarget(path, contextEntry)
return b.schematree.ResolveLeafrefTarget(path, contextEntry)
}

// EnumeratedTypedefTypeName retrieves the name of an enumerated typedef (i.e.,
Expand Down
Loading