corenova / yang-js

YANG parser and composer
Apache License 2.0
56 stars 18 forks source link

Schema extension does not work as described in the TUTORIAL.md #128

Closed fredgan closed 3 years ago

fredgan commented 3 years ago

Hi Peter @sekur,

I find that schema extension does not work as described in the TUTORIAL.md. example:

var Yang = require('yang-js')
var schema = Yang.parse('container foo { leaf a; }');
var model = schema.eval({ foo: { a: 'bar' } });
// try assigning a new arbitrary property
model.foo.b = 'hello';
console.log(model.foo.b);
// returns: undefined (since not part of schema)

the actual result:

$ node schema-extension.js
hello

As it showed above, the result didn't return undefined.

Besides, another example

var Yang = require('yang-js')
var schema = Yang.parse('container foo { leaf a; }');
var model = schema.eval({ foo: { a: 'bar' } });
// extend the previous container foo expression with an additional leaf
schema.extends('leaf b;')
model.foo.b = 'hello';
console.log(model.foo.b)
// returns: 'hello' (since now part of schema!)

the results throw an error.

$ node schema-extension2.js
/workspace/user/node_modules/yang-js/lib/yang.js:288
          throw this.error("cannot merge invalid element into Yang", elem);
          ^

Error [ExpressionError]: [container(foo)] cannot merge invalid element into Yang
    at Function.error (/workspace/user/node_modules/yang-js/lib/element.js:62:17)
    at Function.error (/workspace/user/node_modules/yang-js/lib/expression.js:140:21)
    at Function.merge (/workspace/user/node_modules/yang-js/lib/yang.js:288:22)
    at /workspace/user/node_modules/yang-js/lib/element.js:131:23
    at Array.forEach (<anonymous>)
    at Function.extends (/workspace/user/node_modules/yang-js/lib/element.js:130:15)
    at Object.<anonymous> (/workspace/user/yang-js/bin/exam-test/schema-extension2.js:5:15)
    at Module._compile (node:internal/modules/cjs/loader:1083:30)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
    at Module.load (node:internal/modules/cjs/loader:948:32) {
  uri: 'container(foo)',
  src: <ref *1> [Function: evaluate] Yang {
    leaf: [
      [Function: evaluate] Yang {
        parent: [Circular *1],
        [Symbol(element:index)]: 0,
        [Symbol(element:cache)]: []
      }
    ],
    [Symbol(element:cache)]: [
      [Function: evaluate] Yang {
        parent: [Circular *1],
        [Symbol(element:index)]: 0,
        [Symbol(element:cache)]: []
      }
    ]
  },
  ctx: 'leaf b;'
}

Is it the expected behavior, or a user error?

sekur commented 3 years ago

Sorry, it's outdated docs... I've modified a while back so that you can still assign arbitrary properties to a schema evaluated object. Those assigned properties won't be schema validated (since the arbitrary properties will not have a schema expression for them) but it won't prevent the assignment.

Also, I had at some point removed support for passing in a schema string as part of .extends(). You now need to be more explicit:

schema.extends(Yang.parse('leaf b;'))

I can't recall why I removed support for directly extending schema with a string statement... let me see if I can restore that functionality.

sekur commented 3 years ago

published as 0.24.46 along with update to TUTORIAL.md