summaryrefslogtreecommitdiffhomepage
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/pyang_plugins/bgpyang2golang.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/tools/pyang_plugins/bgpyang2golang.py b/tools/pyang_plugins/bgpyang2golang.py
index 8acbfa02..5cb09e6a 100644
--- a/tools/pyang_plugins/bgpyang2golang.py
+++ b/tools/pyang_plugins/bgpyang2golang.py
@@ -412,13 +412,20 @@ def emit_typedef(ctx, module):
print >> o, ' %s %s = "%s"' % (enum_name, type_name, sub.arg.lower())
print >> o, ')\n'
- print >> o, '\nfunc (v %s) ToInt() int {' % (type_name)
+ print >> o, 'func (v %s) ToInt() int {' % (type_name)
print >> o, 'for i, vv := range []string{%s} {' % (",".join('"%s"' % s.arg.lower() for s in t.substmts))
print >> o, 'if string(v) == vv {return i}'
print >> o, '}'
print >> o, 'return -1'
print >> o, '}\n'
+ print >> o, 'func (v %s) FromInt(i int) %s {' % (type_name, type_name)
+ print >> o, 'for j, vv := range []string{%s} {' % (",".join('"%s"' % s.arg.lower() for s in t.substmts))
+ print >> o, 'if i == j {return %s(vv)}' % (type_name)
+ print >> o, '}'
+ print >> o, 'return %s("")' % (type_name)
+ print >> o, '}\n'
+
print >> o, 'func (v %s) Validate() error {' % (type_name)
print >> o, 'if v.ToInt() < 0 {'
print >> o, 'return fmt.Errorf("invalid %s: %%s", v)' % (type_name)