diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-05-28 07:54:52 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-05-28 07:54:52 +0900 |
commit | 37d3629d8bb867de6b9cf396ca1222a2136e6e2b (patch) | |
tree | 86adb50d514c3449c0a62c64d1b22ebd8f63e7c9 /tools | |
parent | 1e360ff7f5afca5bdaab04acd3a5ce8398cd5003 (diff) |
config: handle entry's empty name
Equal method assumes that an each entry has unique name and uses it as
a key for map. However, currently, an entry is allowed to have an
empty name. To work around the problem, uses an index in the list as a
key for map.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/pyang_plugins/bgpyang2golang.py | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/tools/pyang_plugins/bgpyang2golang.py b/tools/pyang_plugins/bgpyang2golang.py index 5c3819a7..af440407 100644 --- a/tools/pyang_plugins/bgpyang2golang.py +++ b/tools/pyang_plugins/bgpyang2golang.py @@ -95,6 +95,8 @@ def emit_go(ctx): # emit generate_header(ctx) + generate_common_functions(ctx) + for mod in ctx.module_deps: if mod not in _module_excluded: emit_typedef(ctx, mod) @@ -310,10 +312,10 @@ def emit_class_def(ctx, yang_statement, struct_name, prefix): print >> o, '{' print >> o, 'lmap := make(map[string]*{0})'.format(type_name[2:]) print >> o, 'for i, l := range lhs.{0} {{'.format(val_name) - print >> o, 'lmap[string({0})] = &lhs.{1}[i]'.format(' + '.join('l.{0}'.format(convert_to_golang(v)) for v in elem.split(' ')), val_name) + print >> o, 'lmap[mapkey(i, string({0}))] = &lhs.{1}[i]'.format(' + '.join('l.{0}'.format(convert_to_golang(v)) for v in elem.split(' ')), val_name) print >> o, '}' - print >> o, 'for _, r := range rhs.{0} {{'.format(val_name) - print >> o, 'if l, y := lmap[string({0})]; !y {{'.format('+'.join('r.{0}'.format(convert_to_golang(v)) for v in elem.split(' '))) + print >> o, 'for i, r := range rhs.{0} {{'.format(val_name) + print >> o, 'if l, y := lmap[mapkey(i, string({0}))]; !y {{'.format('+'.join('r.{0}'.format(convert_to_golang(v)) for v in elem.split(' '))) print >> o, 'return false' print >> o, '} else if !r.Equal(l) {' print >> o, 'return false' @@ -690,6 +692,15 @@ def generate_header(ctx): print '' +def generate_common_functions(ctx): + print 'func mapkey(index int, name string) string {' + print 'if name != "" {' + print 'return name' + print '}' + print 'return fmt.Sprintf("%v", index)' + print '}' + + def translate_type(key): if key in _type_translation_map.keys(): return _type_translation_map[key] |