Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
robshakir committed Nov 13, 2018
1 parent 3ecb82e commit 1ac073e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
44 changes: 24 additions & 20 deletions pkg/yang/entry.go
Expand Up @@ -71,8 +71,8 @@ type Entry struct {
Name string // our name, same as the key in our parent Dirs
Description string `json:",omitempty"` // description from node, if any
Default string `json:",omitempty"` // default from node, if any
Units string `json:",omitempty"`
Errors []error `json:"-"` // list of errors encountered on this node
Units string `json:",omitempty"` // units associated with the type, if any
Errors []error `json:"-"` // list of errors encountered on this node
Kind EntryKind // kind of Entry
Config TriState // config state of this entry, if known
Prefix *Value `json:",omitempty"` // prefix to use from this point down
Expand Down Expand Up @@ -394,19 +394,31 @@ const (
DeviationAdd
// DeviationReplace corresponds to the replace argument to the deviate stmt.
DeviationReplace
// DeviationDelete corresponds to thee delete argument to the deviation stmt.
// DeviationDelete corresponds to the delete argument to the deviate stmt.
DeviationDelete
)

func (d deviationType) String() string {
m := map[deviationType]string{
var (
// fromDeviation maps from an enumerated deviation type to the YANG keyword.
fromDeviation = map[deviationType]string{
DeviationNotSupported: "not-supported",
DeviationAdd: "add",
DeviationReplace: "replace",
DeviationDelete: "delete",
DeviationUnset: "unknown",
}
return m[d]

// toDeviation maps from the YANG keyword to an enumerated deviation typee.
toDeviation = map[string]deviationType{
"not-supported": DeviationNotSupported,
"add": DeviationAdd,
"replace": DeviationReplace,
"delete": DeviationDelete,
}
)

func (d deviationType) String() string {
return fromDeviation[d]
}

// DeviatedEntry stores a wrapped Entry that corresponds to a deviation.
Expand Down Expand Up @@ -777,26 +789,18 @@ func ToEntry(n Node) (e *Entry) {
if a := fv.Interface().([]*Deviate); a != nil {
for _, d := range a {
de := ToEntry(d)
var arg deviationType
switch d.Statement().Argument {
case "not-supported":
arg = DeviationNotSupported
case "add":
arg = DeviationAdd
case "replace":
arg = DeviationReplace
case "delete":
arg = DeviationDelete

dt, ok := toDeviation[d.Statement().Argument]
if !ok {
e.addError(fmt.Errorf("%s: unknown deviation type in %s:%s", Source(n), n.Kind(), n.NName()))
continue
}

if e.Deviate == nil {
e.Deviate = map[deviationType][]*Entry{}
}

if _, ok := e.Deviate[arg]; !ok {
e.Deviate[arg] = []*Entry{}
}
e.Deviate[arg] = append(e.Deviate[arg], de)
e.Deviate[dt] = append(e.Deviate[dt], de)
}
}
case "mandatory":
Expand Down
42 changes: 41 additions & 1 deletion pkg/yang/entry_test.go
Expand Up @@ -1716,6 +1716,47 @@ func TestDeviation(t *testing.T) {
},
}},
},
}, {
desc: "complex deviation of multiple leaves",
inFiles: map[string]string{
"foo": `
module foo {
prefix "f";
namespace "urn:f";
container a { leaf b { type string; } }
typedef abc { type boolean; }
typedef abt { type uint32; }
deviation /a/b {
// typedef is not valid here.
//typedef abc {
// type boolean;
//}
deviate replace { type abc; }
}
deviation /a/b {
// typedef is not valid here.
//typedef abt {
// type uint16;
//}
deviate replace { type abt; }
}
}`,
},
wants: map[string][]deviationTest{
"foo": []deviationTest{{
path: "/a/b",
entry: &Entry{
Type: &YangType{
Name: "abt",
Kind: Yuint32,
},
},
}},
},
}}

for _, tt := range tests {
Expand All @@ -1725,7 +1766,6 @@ func TestDeviation(t *testing.T) {

for name, mod := range tt.inFiles {
if err := ms.Parse(mod, name); err != nil {
fmt.Printf("LIZARD %s\n", err)
if diff := errdiff.Substring(err, tt.wantParseErrSubstring); diff != "" {
t.Fatalf("error parsing module %s, %s", name, diff)
}
Expand Down

0 comments on commit 1ac073e

Please sign in to comment.