From 37d3629d8bb867de6b9cf396ca1222a2136e6e2b Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Sat, 28 May 2016 07:54:52 +0900 Subject: 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 --- tools/pyang_plugins/bgpyang2golang.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'tools') 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] -- cgit v1.2.3