-
Notifications
You must be signed in to change notification settings - Fork 107
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
initial commit for leaf setters #281
Open
adibrastegarnia
wants to merge
5
commits into
openconfig:master
Choose a base branch
from
adibrastegarnia:add-leaf-setters
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fa4bf01
initial commit for leaf setters
e878408
initial commit for adding leaf setters
b6096fb
Merge branch 'add-leaf-setters' of https://github.com/adibrastegarnia…
65852e4
add leaf setters functions
da31991
goLeafSetterTemplate has been fixed
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -310,6 +310,26 @@ type generatedLeafGetter struct { | |
Receiver string | ||
} | ||
|
||
// generatedLeafSetter is used to represent the parameters required to generate a | ||
// setter for a leaf within the generated Go code. | ||
type generatedLeafSetter struct { | ||
// Name is the name of the field. It is used as a suffix to Get to generate | ||
// the getter. | ||
Name string | ||
// Type is the type of the field, returned by the generated method. | ||
Type string | ||
// Zero is the value that should be returned if the field is set to nil. | ||
Zero string | ||
// Default is the default value specified in the YANG schema for the type | ||
// or leaf. | ||
Default *string | ||
// IsPtr stores whether the value is a pointer, such that it can be checked | ||
// against nil, or against the zero value. | ||
IsPtr bool | ||
// Receiver is the name of the receiver for the getter method. | ||
Receiver string | ||
} | ||
|
||
var ( | ||
// goCommonHeaderTemplate is populated and output at the top of the generated code package | ||
goCommonHeaderTemplate = ` | ||
|
@@ -730,6 +750,19 @@ func (t *{{ .Receiver }}) Get{{ .Name }}() {{ .Type }} { | |
} | ||
return {{ if .IsPtr -}} * {{- end -}} t.{{ .Name }} | ||
} | ||
` | ||
|
||
// goLeafSetterTemplate defines a template for a function that, for a | ||
// particular leaf, generates a setter method. | ||
goLeafSetterTemplate = ` | ||
// Set{{ .Name }} sets the value of the leaf {{ .Name }} from the {{ .Receiver }} | ||
// struct. | ||
func (t *{{ .Receiver }}) Set{{ .Name }}(val string) { | ||
if t == nil || val == "" { | ||
return | ||
} | ||
t.{{ .Name }} = ygot.String(val) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above re: whether you can use This likely highlights a test coverage gap -- we should add tests to |
||
} | ||
` | ||
|
||
// goDeleteListTemplate defines a template for a function that, for a | ||
|
@@ -1031,6 +1064,7 @@ func (t *{{ .ParentReceiver }}) To_{{ .Name }}(i interface{}) ({{ .Name }}, erro | |
"getList": makeTemplate("getList", goListGetterTemplate), | ||
"getContainer": makeTemplate("getContainer", goContainerGetterTemplate), | ||
"getLeaf": makeTemplate("getLeaf", goLeafGetterTemplate), | ||
"setLeaf": makeTemplate("setLeaf", goLeafSetterTemplate), | ||
} | ||
|
||
// templateHelperFunctions specifies a set of functions that are supplied as | ||
|
@@ -1190,6 +1224,11 @@ func writeGoStruct(targetStruct *yangDirectory, goStructElements map[string]*yan | |
// is set to true. | ||
var associatedLeafGetters []*generatedLeafGetter | ||
|
||
// associatedLeafSetters is a slice of structs which define the set of leaf setters | ||
// to generated for the struct. It is only populated if the GenerateLeafSetters option | ||
// is set to true. | ||
var associatedLeafSetters []*generatedLeafSetter | ||
|
||
// Sort the list of fields into alphabetical order to ensure determinstic output of code, | ||
// and minimise diffs. | ||
var fieldNames []string | ||
|
@@ -1388,6 +1427,20 @@ func writeGoStruct(targetStruct *yangDirectory, goStructElements map[string]*yan | |
}) | ||
} | ||
|
||
if goOpts.GenerateLeafSetters { | ||
// If we are generating leaf setters, then append the relevant information | ||
// to the associatedLeafSetters slice to be generated along with other | ||
// associated methods. | ||
associatedLeafSetters = append(associatedLeafSetters, &generatedLeafSetter{ | ||
Name: fieldName, | ||
Type: fType, | ||
Zero: zeroValue, | ||
IsPtr: scalarField, | ||
Receiver: targetStruct.name, | ||
Default: defaultValue, | ||
}) | ||
} | ||
|
||
fieldDef = &goStructField{ | ||
Name: fieldName, | ||
Type: fType, | ||
|
@@ -1518,6 +1571,12 @@ func writeGoStruct(targetStruct *yangDirectory, goStructElements map[string]*yan | |
} | ||
} | ||
|
||
if goOpts.GenerateLeafSetters { | ||
if err := generateLeafSetters(&methodBuf, associatedLeafSetters); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
|
||
if err := generateGetListKey(&methodBuf, targetStruct, definedNameMap); err != nil { | ||
errs = append(errs, err) | ||
} | ||
|
@@ -1653,6 +1712,18 @@ func generateLeafGetters(buf *bytes.Buffer, leaves []*generatedLeafGetter) error | |
return errs.Err() | ||
} | ||
|
||
// generateLeafSetters generates SetXXX methods for the leaf fields described by | ||
// the supplied slice of generatedLeafSetter structs. | ||
func generateLeafSetters(buf *bytes.Buffer, leaves []*generatedLeafSetter) error { | ||
var errs errlist.List | ||
for _, l := range leaves { | ||
if err := goTemplates["setLeaf"].Execute(buf, l); err != nil { | ||
errs.Add(err) | ||
} | ||
} | ||
return errs.Err() | ||
} | ||
|
||
// generateGetOrCreateList generates a getter function similar to that created | ||
// by the generateGetOrCreateStruct function for maps within the generated Go | ||
// code (which represent YANG lists). It handles both simple and composite key | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that you can just take a
string
as an argument, there are different types of fields that we have within the generated structs. Given that this is the case, we need to handle different inputs here. Determining the type that is needed should likely be from the input type that is in thegeneratedLeafSetter
that you created. There are some other details that you'll need though, such as the name of the generated union, such that you can callTo_XXX
for these types.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robshakir
Thanks for your comments. That is a good point. I will start working on it but before that what types you would suggest to support? I will let you know if I have more questions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I figured that out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robshakir
May I ask you how do you generate the expected output for the testcases? It says the following but it is not clear for me:
This package was generated by codegen-tests
using the following YANG input files: .....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robshakir
Please take a look at this code and I think it handles any type. Please let me know what you think. I also have a test case ready but I am not sure how do you generate the expected output in advance.
`