corenova / yang-js

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

[question] how can I eval two different data on the same schema? #79

Closed quantang closed 5 years ago

quantang commented 5 years ago

Hi @saintkepha ,

Do you have any idea how I can eval different data without merging them?

As my scenario may get different JSON data from different sources, I use yang-js to eval the data against my schema first, then I retrieve the cooked data from YANG model for further processing.

Currently, my code can work well if I only deal with one JSON data. If there is another JSON data and I use YANG instance to eval it, it will be merged into the existing YANG model, right?

Please correct me if I use it incorrectly.

Cheers, Quan

sekur commented 5 years ago

I don’t think schema.eval would merge into existing data. Every eval should produce a new set of cooked model (as in the passed in data being independently infused with property instances). The only caveat is how it deals with data for imported module schemas. It currently pulls from Model.Store which is a singleton instance. I think we can likely change that to be pegged to the Model instance.

A simple test would be to eval two different JS object and operate on them independently. A change in one cooked model should not impact the other. On Mon, Oct 15, 2018 at 6:25 PM Quan Tang notifications@github.com wrote:

Hi @saintkepha https://github.com/saintkepha ,

Do you have any idea how I can eval different data without merging them?

As my scenario may get different JSON data from different sources, I use yang-js to eval the data against my schema first, then I retrieve the cooked data from YANG model for further processing.

Currently, my code can work well if I only deal with one JSON data. If there is another JSON data and I use YANG instance to eval it, it will be merged into the existing YANG model, right?

Please correct me if I use it incorrectly.

Cheers, Quan

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/corenova/yang-js/issues/79, or mute the thread https://github.com/notifications/unsubscribe-auth/AA6KbB9n2uEQYjhzY2g7vANmO3Aqo48Oks5ulTWAgaJpZM4XdaJg .

-- Peter Lee Chief Executive Officer Corenova Technologies, Inc. +1-310-400-6450 linkedin.com/in/peter-k-lee

quantang commented 5 years ago

Hi @saintkepha ,

Here is my sample code and the output:

Code:

const Yang = require('yang-js');

const data1 = {
    "ietf-network:networks": {
        "network": [{
            "network-id": "local.area.atmf",
        }]
    }
};

const data2 = {
    "ietf-network:networks": {
        "network": [{
            "network-id": "wireless.awc",
        }]
    }
};

const Topo = Yang.import('at-network-topology');

const model1 = Topo.eval(data1);
const networks1 = model1.access('ietf-network');
console.info('network from data1:');
console.info(JSON.stringify(networks1));

const model2 = Topo.eval(data2);
const networks2 = model2.access('ietf-network');
console.info('network from data2:');
console.info(JSON.stringify(networks2));

The output only has the data of the first one:

network from data1:
{"ietf-network:networks":{"network":[{"network-id":"local.area.atmf"}]}}
network from data2:
{"ietf-network:networks":{"network":[{"network-id":"local.area.atmf"}]}}

Please correct me if I am wrong. Cheers.

sekur commented 5 years ago

Yes, that is the issue I was indicating in my prior note.

The only caveat is how it deals with data for imported module schemas. It currently pulls from Model.Store which is a singleton instance.

The way yang-js currently handles data for imported modules is that it uses a singleton object and it results in bleedover in this scenario. Let me change the Model class instance to hold the models for imported modules instead of the Model class itself and it should allow this use case to operate as expected.

quantang commented 5 years ago

Ok, I see your point. It is not a high priority issue for us, as I can work it around and eventually merge them into a single one. Cheers.