Skip to content

Commit

Permalink
lib.yang.schema: implement semantics for enumeration type
Browse files Browse the repository at this point in the history
Implement semantics according to RFC6020, 9.6.  When validating a
configuration against a schema which contains

leaf foo {
  type enumeratoion {
    enum bar;
    enum baz;
  }
}

the resulting Lua table will contain the key "foo" whose value is the
string of the chosen enum in the configuration, e.g.

{
  foo = "baz"
}

It is debatable whether this should actually result in the numerical
value assigned to the name in the schema.
  • Loading branch information
alexandergall committed Feb 5, 2019
1 parent 771b55c commit 623aff0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/lib/yang/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,28 @@ function resolve(schema, features)
elseif node.id == 'identityref' then
node.bases = resolve_bases(node.bases, env)
node.default_prefix = schema.id
elseif node.id == 'enumeration' then
local values = {}
local max_value = -2147483648
for i, enum in ipairs(node.enums) do
assert(not node.enums[enum.name],
'duplicate name in enumeraion: '..enum.name)

This comment has been minimized.

Copy link
@dpino

dpino Feb 5, 2019

Nit: s/enumeraion/enumeration/

node.enums[enum.name] = enum
if enum.value then
assert(not values[enum.value],
'duplicate value in enumeration: '..enum.value)
values[enum.value] = true
max_value = math.max(enum.value, max_value)
elseif i == 1 then
max_value = 0
enum.value = max_value
elseif max_value < 2147483647 then
max_value = max_value + 1
enum.value = max_value
else
error('explicit value required in enum: '..enum.name)
end
end
end
node.primitive_type = node.id
end
Expand Down

2 comments on commit 623aff0

@dpino
Copy link

@dpino dpino commented on 623aff0 Feb 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Could you also add a test case in lib.yang.data? The test case you posted at snabbco#1415 should be enough.

@alexandergall
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I accidentally rebased this branch :( Will post a PR with the rebased branch.

Please sign in to comment.