Skip to content

Commit

Permalink
CiscoDevNet#763: Adding support for presence type in golan
Browse files Browse the repository at this point in the history
  • Loading branch information
ylil93 committed May 7, 2018
1 parent 4810b01 commit b7e1575
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
36 changes: 34 additions & 2 deletions sdk/go/core/tests/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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))
}
3 changes: 3 additions & 0 deletions sdk/go/core/ydk/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
26 changes: 24 additions & 2 deletions sdk/go/core/ydk/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions ydkgen/printer/go/class_constructor_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit b7e1575

Please sign in to comment.