From 3d857d6c44bc16122bb8a5bd4cf81ada22846860 Mon Sep 17 00:00:00 2001 From: Asparuh Krastev Date: Thu, 26 Oct 2023 13:49:29 +0300 Subject: [PATCH 1/4] Generate const documentation. Fixes #340. --- bind/gen_varconst.go | 10 ++++++++++ bind/package.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/bind/gen_varconst.go b/bind/gen_varconst.go index e0906a5..f794af8 100644 --- a/bind/gen_varconst.go +++ b/bind/gen_varconst.go @@ -125,6 +125,16 @@ func (g *pyGen) genConstValue(c *Const) { val = "False" } g.pywrap.Printf("%s = %s\n", c.GoName(), val) + if c.doc != "" { + lns := strings.Split(c.doc, "\n") + g.pywrap.Printf(`"""`) + g.pywrap.Printf("\n") + for _, l := range lns { + g.pywrap.Printf("%s\n", l) + } + g.pywrap.Printf(`"""`) + g.pywrap.Printf("\n") + } } func (g *pyGen) genEnum(e *Enum) { diff --git a/bind/package.go b/bind/package.go index 8a87df5..4406b27 100644 --- a/bind/package.go +++ b/bind/package.go @@ -112,6 +112,7 @@ func (p *Package) getDoc(parent string, o types.Object) string { n := o.Name() switch tp := o.(type) { case *types.Const: + // Check for untyped consts for _, c := range p.doc.Consts { for _, cn := range c.Names { if n == cn { @@ -119,6 +120,20 @@ func (p *Package) getDoc(parent string, o types.Object) string { } } } + // Check for typed consts + scopeName := p.pkg.Scope().Lookup(n) + constType := scopeName.Type() + for _, t := range p.doc.Types { + if p.pkg.Path()+"."+t.Name == constType.String() { + for _, c := range t.Consts { + for _, cn := range c.Names { + if n == cn { + return c.Doc + } + } + } + } + } case *types.Var: if tp.IsField() && parent != "" { From 71d9da3f871e668fd6134d7fa4008a7a500864f4 Mon Sep 17 00:00:00 2001 From: Asparuh Krastev Date: Thu, 26 Oct 2023 14:11:41 +0300 Subject: [PATCH 2/4] Simplify --- bind/package.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bind/package.go b/bind/package.go index 4406b27..ae92651 100644 --- a/bind/package.go +++ b/bind/package.go @@ -122,9 +122,12 @@ func (p *Package) getDoc(parent string, o types.Object) string { } // Check for typed consts scopeName := p.pkg.Scope().Lookup(n) - constType := scopeName.Type() + constType := scopeName.Type().(*types.Named) + if constType == nil { + return "" + } for _, t := range p.doc.Types { - if p.pkg.Path()+"."+t.Name == constType.String() { + if t.Name == constType.Obj().Name() { for _, c := range t.Consts { for _, cn := range c.Names { if n == cn { From 3bc817dcb185cce365cfa057ccb04a80694edad0 Mon Sep 17 00:00:00 2001 From: Asparuh Krastev Date: Thu, 26 Oct 2023 14:17:34 +0300 Subject: [PATCH 3/4] Revert --- bind/package.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bind/package.go b/bind/package.go index ae92651..41d37e2 100644 --- a/bind/package.go +++ b/bind/package.go @@ -122,12 +122,12 @@ func (p *Package) getDoc(parent string, o types.Object) string { } // Check for typed consts scopeName := p.pkg.Scope().Lookup(n) - constType := scopeName.Type().(*types.Named) + constType := scopeName.Type() if constType == nil { return "" } for _, t := range p.doc.Types { - if t.Name == constType.Obj().Name() { + if p.pkg.Path()+"."+t.Name == constType.String() { for _, c := range t.Consts { for _, cn := range c.Names { if n == cn { From 113c72fdd4897da333db0650fbd05900515016cc Mon Sep 17 00:00:00 2001 From: Asparuh Krastev Date: Thu, 26 Oct 2023 14:20:13 +0300 Subject: [PATCH 4/4] Checks --- bind/package.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bind/package.go b/bind/package.go index 41d37e2..2136abb 100644 --- a/bind/package.go +++ b/bind/package.go @@ -122,6 +122,9 @@ func (p *Package) getDoc(parent string, o types.Object) string { } // Check for typed consts scopeName := p.pkg.Scope().Lookup(n) + if scopeName == nil { + return "" + } constType := scopeName.Type() if constType == nil { return ""