diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2016-01-08 14:49:27 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-01-09 05:17:57 -0800 |
commit | 3bed393b014210f6764778138798c31725839079 (patch) | |
tree | 469607542c5e8291b18de97cbd98737f6d2f66ab /tools | |
parent | a0b87f1ddf849cc44a39645fb296bfe2d3a5b287 (diff) |
config: add FromInt() method to config typedef
use for the translation between api structs
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/pyang_plugins/bgpyang2golang.py | 9 |
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) |