corenova / yang-js

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

Augment/Uses along with 'when' clause is not compiled #63

Closed sindhukothe closed 6 years ago

sindhukothe commented 7 years ago

I have the following piece of yang. Looks like the at the compilation stage, the ethernet-top is not augmented to oc-if:interface due to the when clause

augment "/oc-if:interfaces/oc-if:interface" { description "Adds addtional Ethernet-specific configuration to interfaces model";

uses ethernet-top {
  when "oc-if:state/oc-if:type = 'ift:ethernetCsmacd'" {
  description "Additional interface configuration parameters when
  the interface type is Ethernet";
  }
}

}

The src/lang/extensions.coffee has the following code that seems to be related: unless @when? @once 'compile:after', => @debug "augmenting '#{target.kind}:#{target.tag}'" from = @root.tag if @parent.kind is 'module' and target.root isnt @root target.extends @nodes.map (x) -> copy = x.clone() copy.tag = "#{from}:#{x.tag}" if from? return copy else target.on 'apply:after', (data) =>

sekur commented 7 years ago

This is by design, I've deferred schema application to be performed for when conditional during eval instead of during parse. This is because the when conditional cannot be applied without underlying data for which to verify the rule. I'm open to suggestion on how else we should treat this condition.

sindhukothe commented 7 years ago

Here is what I think: At the time of parsing, we are merely augmenting nodes and preparing a tree of nodes. There is no data to determine if this when clause is returning false or true. At the time of evaluation, there is data to evaluate the when clause and hence determine whether the child is valid or not. Hence, we should augment the node during parse irrespective of the when clause, but evaluate the when clause during eval. Basically, I am trying to display a tree of the node hierarchy after compiling the yangs, but these child nodes would never appear in that tree. Let me know what you think.

sekur commented 7 years ago

There's a particular use case that makes it difficult to have pre-populated node hierarchy - which occurs when you have circular/recursive groupings.

Take a look at yang-swagger project. It builds a tree of JSON schema nodes based on existence of nested data that have the same schema structure. If we were to pre-populate the nodes, it would result in an infinitely deep structure... I use when conditional to denote that it be evaluated with data to have the schema application occur during eval instead of during parse. If you can think of a better way to handle this scenario, I'm on board with making this a compile time facility.

sekur commented 6 years ago

Closing for now until a better alternative is available.