From b7e157533d1347b4da8e69b973689b63666b8695 Mon Sep 17 00:00:00 2001 From: lily Date: Mon, 7 May 2018 11:55:55 -0700 Subject: [PATCH] #763: Adding support for presence type in golan --- sdk/go/core/tests/types_test.go | 36 +++++++++++++++++-- sdk/go/core/ydk/path/path.go | 3 ++ sdk/go/core/ydk/types/types.go | 26 ++++++++++++-- .../printer/go/class_constructor_printer.py | 2 ++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/sdk/go/core/tests/types_test.go b/sdk/go/core/tests/types_test.go index 7258cd58e..32fe44eb9 100644 --- a/sdk/go/core/tests/types_test.go +++ b/sdk/go/core/tests/types_test.go @@ -353,8 +353,34 @@ func (suite *SanityTypesTestSuite) TestCapitalLetters() { suite.Equal(types.EntityEqual(entityRead, &native), true) } -func TestSanityTypesTestSuite(t *testing.T) { - suite.Run(t, new(SanityTypesTestSuite)) +func (suite *SanityTypesTestSuite) TestPresence() { + var runner ysanity.Runner + var runnerRead ysanity.Runner + var readEntity types.Entity + + // Setting Presence + runner = ysanity.Runner{} + types.SetPresenceFlag(&runner.Outer.Inner) + suite.Equal(true, runner.Outer.Inner.YPresence) + + suite.CRUD.Create(&suite.Provider, &runner) + readEntity = suite.CRUD.Read(&suite.Provider, &ysanity.Runner{}) + runnerRead = *readEntity.(*ysanity.Runner) + suite.Equal(true, types.EntityEqual(&runnerRead, &runner)) + suite.Equal(runner.Outer.Inner.YPresence, runnerRead.Outer.Inner.YPresence) + + // CRUD Delete + suite.CRUD.Delete(&suite.Provider, &ysanity.Runner{}) + + // Setting Data + runner = ysanity.Runner{} + runner.Runner2.SomeLeaf = "test" + + suite.CRUD.Create(&suite.Provider, &runner) + readEntity = suite.CRUD.Read(&suite.Provider, &ysanity.Runner{}) + runnerRead = *readEntity.(*ysanity.Runner) + suite.Equal(true, types.EntityEqual(&runnerRead, &runner)) + suite.Equal(runner.Outer.Inner.YPresence, runnerRead.Outer.Inner.YPresence) } func (suite *SanityTypesTestSuite) TestEntityCollection() { @@ -469,3 +495,9 @@ func testParams(testName string, entity types.Entity) types.Entity { return ec } +func TestSanityTypesTestSuite(t *testing.T) { + if testing.Verbose() { + ydk.EnableLogging(ydk.Debug) + } + suite.Run(t, new(SanityTypesTestSuite)) +} diff --git a/sdk/go/core/ydk/path/path.go b/sdk/go/core/ydk/path/path.go index f01c12524..30248a254 100644 --- a/sdk/go/core/ydk/path/path.go +++ b/sdk/go/core/ydk/path/path.go @@ -841,6 +841,9 @@ func getEntityFromDataNode(node C.DataNode, entity types.Entity) { ydk.YLogError(fmt.Sprintf("Could not create child entity '%s'!", childName)) panic("Could not create child entity!") } + if types.IsPresenceContainer(childEntity){ + types.SetPresenceFlag(childEntity) + } types.SetParent(childEntity, entity) getEntityFromDataNode(childDataNode, childEntity) } diff --git a/sdk/go/core/ydk/types/types.go b/sdk/go/core/ydk/types/types.go index 69cd62adc..9cb7b9bea 100644 --- a/sdk/go/core/ydk/types/types.go +++ b/sdk/go/core/ydk/types/types.go @@ -408,6 +408,17 @@ func HasDataOrFilter(entity Entity) bool { if entity == nil { return false } + + isPresence, isSet := IsPresenceContainer(entity), false + if isPresence { + v := reflect.ValueOf(entity).Elem() + field := v.FieldByName("YPresence") + isSet = field.Interface().(bool) + } + if isSet { + return true + } + entityData := entity.GetEntityData() if (entityData.YFilter != yfilter.NotSet) { return true @@ -424,10 +435,9 @@ func HasDataOrFilter(entity Entity) bool { } } - v := reflect.ValueOf(entity).Elem() - // checking leafs leafs := GetYLeafs(entityData) + v := reflect.ValueOf(entity).Elem() for _, leaf := range leafs { field := v.FieldByName(leaf.GoName) @@ -562,6 +572,18 @@ func SetValue(entity Entity, valuePath string, value interface{}) { } } +// IsPresenceContainer returns if the given entity is a presence container +func IsPresenceContainer(entity Entity) bool { + v := reflect.ValueOf(entity).Elem() + field := v.FieldByName("YPresence") + return field.IsValid() +} + +func SetPresenceFlag(entity Entity) { + v := reflect.ValueOf(entity).Elem() + field := v.FieldByName("YPresence") + field.Set(reflect.ValueOf(true)) +} // Decimal64 represents a YANG built-in Decimal64 type type Decimal64 struct { diff --git a/ydkgen/printer/go/class_constructor_printer.py b/ydkgen/printer/go/class_constructor_printer.py index 7be835fd9..e95783e79 100644 --- a/ydkgen/printer/go/class_constructor_printer.py +++ b/ydkgen/printer/go/class_constructor_printer.py @@ -41,6 +41,8 @@ def print_function_header(self): def print_function_body(self): self.ctx.writeln('EntityData types.CommonEntityData') self.ctx.writeln('YFilter yfilter.YFilter') + if self.clazz.stmt.search_one('presence') is not None: + self.ctx.writeln('YPresence bool') self._print_inits() def _print_docstring(self):