-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconstant.go
80 lines (66 loc) · 1.45 KB
/
constant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package generate
import (
"fmt"
"github.com/pekim/jennifer/jen"
)
type Constant struct {
Namespace *Namespace
Name string `xml:"name,attr"`
Blacklist bool `xml:"blacklist,attr"`
Value string `xml:"value,attr"`
Version string `xml:"version,attr"`
CType string `xml:"http://www.gtk.org/introspection/c/1.0 type,attr"`
Doc *Doc `xml:"doc"`
Type *Type `xml:"type"`
goTypeName string
}
func (c *Constant) init(ns *Namespace) {
c.Namespace = ns
switch c.Type.Name {
case "gboolean":
c.goTypeName = "bool"
case "utf8":
c.goTypeName = "string"
default:
if goTypeName, found := numberCTypeMap[c.Type.Name]; found {
c.goTypeName = goTypeName
}
}
}
func (c *Constant) version() string {
return c.Version
}
func (c *Constant) mergeAddenda(addenda *Constant) {
c.Blacklist = addenda.Blacklist
}
func (c *Constant) blacklisted() (bool, string) {
return c.Blacklist, c.Name
}
func (c *Constant) supported() (supported bool, reason string) {
if c.goTypeName != "" {
return true, ""
} else {
return false, fmt.Sprintf("type %s for %s", c.Type.Name, c.Name)
}
}
func (c *Constant) generate(g *jen.Group, version *Version) {
if !supportedByVersion(c, version) {
return
}
if c.Type.Name == "gboolean" {
g.
Const().
Id(c.Name).
Id(c.goTypeName).
Op("=").
Lit(c.Value == "true").
Commentf("C.%s", c.CType)
return
}
g.
Const().
Id(c.Name).
Id(c.goTypeName).
Op("=").
Qual("C", c.CType)
}