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 UNSET enums' String() return "UNSET" #436

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion ygen/gogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2111,7 +2111,7 @@ func writeGoEnum(inputEnum *yangEnum) (goEnumCodeSnippet, error) {
// initialised to be UNSET, such that it is possible to determine that the enumerated value
// was not modified.
values := map[int64]string{
0: "UNSET",
0: ygot.GoEnumZeroName,
}

// origValues stores the original set of value names, these are not maintained to be
Expand Down
2 changes: 1 addition & 1 deletion ygen/schema_tests/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestEnumStringFunction(t *testing.T) {
}, {
desc: "out-of-range: UNSET",
inEnum: exampleoc.Interface_OperStatus_UNSET,
want: "out-of-range E_Interface_OperStatus enum value: 0",
want: "UNSET",
}, {
desc: "out-of-range: too high",
inEnum: exampleoc.E_Interface_OperStatus(100),
Expand Down
6 changes: 6 additions & 0 deletions ygot/struct_validation_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import (
)

const (
// GoEnumZeroName is the string value used to represent an unset
// enumeration/identity value in Go.
GoEnumZeroName = "UNSET"
// indentString represents the default indentation string used for
// JSON. Three spaces are used based on the legacy use of EmitJSON.
indentString string = " "
Expand Down Expand Up @@ -187,6 +190,9 @@ func enumFieldToString(field reflect.Value, appendModuleName bool) (string, bool
func EnumLogString(e GoEnum, val int64, enumTypeName string) string {
enumDef, ok := e.ΛMap()[enumTypeName][val]
if !ok {
if val == 0 {
return GoEnumZeroName
}
return fmt.Sprintf("out-of-range %s enum value: %v", enumTypeName, val)
}
return enumDef.Name
Expand Down
2 changes: 1 addition & 1 deletion ygot/struct_validation_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func TestEnumLogString(t *testing.T) {
inEnum: EUNSET,
inVal: int64(EUNSET),
inEnumTypeName: "enumTest",
want: "out-of-range enumTest enum value: 0",
want: "UNSET",
}, {
desc: "way out of range",
inEnum: EONE,
Expand Down