/
struct.go
166 lines (146 loc) · 3.96 KB
/
struct.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package cgo
import (
"go/ast"
"go/token"
"go/types"
"regexp"
"strings"
)
var (
constructorName = regexp.MustCompile(`^New([A-Z]\w+)`)
)
// Struct is a helpful facade over types.Named which is intended to only contain a struct
type Struct struct {
*Named
}
func NewStruct(named *types.Named) *Struct {
if _, ok := named.Underlying().(*types.Struct); !ok {
panic("only structs belong in structs")
}
return &Struct{NewNamed(named)}
}
// Struct returns the underlying struct
func (s Struct) Struct() *types.Struct {
return s.Underlying().(*types.Struct)
}
// ToAst returns the go/ast representation of the CGo wrapper of the Array type
func (s Struct) ToAst() []ast.Decl {
decls := []ast.Decl{s.NewAst(), s.StringAst()}
decls = append(decls, s.FieldAccessorsAst()...)
decls = append(decls, s.MethodAsts()...)
return decls
}
func (s Struct) FieldAccessorsAst() []ast.Decl {
var accessors []ast.Decl
for i := 0; i < s.Struct().NumFields(); i++ {
field := s.Struct().Field(i)
if ShouldGenerateField(field) {
accessors = append(accessors, s.Getter(field), s.Setter(field))
}
}
return accessors
}
func (s Struct) Getter(field *types.Var) ast.Decl {
functionName := s.FieldName(field) + "_get"
selfIdent := NewIdent("self")
localVarIdent := NewIdent("value")
fieldIdent := NewIdent(field.Name())
castExpression := CastUnsafePtrOfTypeUuid(DeRef(s.CTypeName()), selfIdent)
assignment := &ast.AssignStmt{
Lhs: []ast.Expr{localVarIdent},
Tok: token.DEFINE,
Rhs: []ast.Expr{
&ast.SelectorExpr{
X: castExpression,
Sel: fieldIdent,
},
},
}
funcDecl := &ast.FuncDecl{
Doc: &ast.CommentGroup{
List: ExportComments(functionName),
},
Name: NewIdent(functionName),
Type: &ast.FuncType{
Params: InstanceMethodParams(),
Results: &ast.FieldList{
List: []*ast.Field{{Type: TypeToArgumentTypeExpr(field.Type())}},
},
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
assignment,
Return(CastOut(field.Type(), localVarIdent)),
},
},
}
return funcDecl
}
func (s Struct) Setter(field *types.Var) ast.Decl {
functionName := s.FieldName(field) + "_set"
selfIdent := NewIdent("self")
localVarIdent := NewIdent("value")
transformedLocalVarIdent := NewIdent("val")
fieldIdent := NewIdent(field.Name())
castExpression := CastUnsafePtrOfTypeUuid(DeRef(s.CTypeName()), selfIdent)
typedField := UnsafePtrOrBasic(field, field.Type())
typedField.Names = []*ast.Ident{localVarIdent}
params := InstanceMethodParams(typedField)
firstAssignmentCastRhs := CastExpr(field.Type(), localVarIdent)
secondAssignment := ast.Expr(transformedLocalVarIdent)
if isStringPointer(field.Type()) {
strPtrCast := CastExpr(field.Type(), localVarIdent).(*ast.UnaryExpr)
firstAssignmentCastRhs = strPtrCast.X
secondAssignment = Ref(transformedLocalVarIdent)
}
funcDecl := &ast.FuncDecl{
Doc: &ast.CommentGroup{
List: ExportComments(functionName),
},
Name: NewIdent(functionName),
Type: &ast.FuncType{
Params: params,
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.AssignStmt{
Lhs: []ast.Expr{transformedLocalVarIdent},
Tok: token.DEFINE,
Rhs: []ast.Expr{firstAssignmentCastRhs},
},
&ast.AssignStmt{
Lhs: []ast.Expr{
&ast.SelectorExpr{
X: castExpression,
Sel: fieldIdent,
},
},
Tok: token.ASSIGN,
Rhs: []ast.Expr{secondAssignment},
},
},
},
}
return funcDecl
}
func (s Struct) FieldName(field *types.Var) string {
return s.CName() + "_" + field.Name()
}
func (s Struct) IsConstructor(f *Func) bool {
matches := constructorName.FindStringSubmatch(f.Name())
if len(matches) > 1 && strings.HasPrefix(matches[1], s.Obj().Name()) {
return true
}
return false
}
func (s Struct) ConstructorName(f *Func) string {
return strings.Replace(f.Name(), s.Obj().Name(), "", 1)
}
func isStringPointer(t types.Type) bool {
if ptr, ok := t.(*types.Pointer); ok {
if basic, okB := ptr.Elem().(*types.Basic); okB && basic.Kind() == types.String {
return true
}
}
return false
}