-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathstructures_deffinition.go
More file actions
156 lines (131 loc) · 4.03 KB
/
structures_deffinition.go
File metadata and controls
156 lines (131 loc) · 4.03 KB
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
package main
import (
"encoding/json"
"fmt"
"sort"
)
type NonPODUsedType byte
const (
POD NonPODUsedType = iota
NonPOD
PODInherited
)
// StructSection appears in json file on top of structs definition section.
type StructSection struct {
StructComments json.RawMessage `json:"struct_comments"`
Structs json.RawMessage `json:"structs"`
NonPODUsed json.RawMessage `json:"nonPOD_used"`
}
// StructDef represents a definition of an ImGui struct.
type StructDef struct {
Name CIdentifier `json:"name"`
CommentAbove string
Members []StructMemberDef `json:"members"`
NonPODUsed NonPODUsedType
}
func (s *StructDef) podName(ctx *Context) CIdentifier {
if s.NonPODUsed == NonPOD {
return s.Name + CIdentifier(ctx.preset.NonPODUsedSuffix)
}
return s.Name
}
// StructMemberDef represents a definition of an ImGui struct member.
type StructMemberDef struct {
Name CIdentifier `json:"name"`
TemplateType CIdentifier `json:"template_type"`
Type CIdentifier `json:"type"`
Size int `json:"size"`
Comment CommentDef
CommentData json.RawMessage `json:"comment"`
Bitfield string `json:"bitfield"`
}
// getStructDefs takes a json file bytes (struct_and_enums.json) and returns a slice of StructDef.
func getStructDefs(enumJsonBytes []byte) ([]StructDef, error) {
// extract only structs section from json
var structSectionJson StructSection
err := json.Unmarshal(enumJsonBytes, &structSectionJson)
if err != nil {
return nil, fmt.Errorf("cannot extract structs section from json: %w", err)
}
var (
structs []StructDef
structJson map[string]json.RawMessage
structCommentJson map[string]json.RawMessage = make(map[string]json.RawMessage)
nonPODJson map[string]any
)
err = json.Unmarshal(structSectionJson.Structs, &structJson)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal structs section: %w", err)
}
if structSectionJson.StructComments != nil && string(structSectionJson.StructComments) != "[]" {
err = json.Unmarshal(structSectionJson.StructComments, &structCommentJson)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal struct's comments section: %w", err)
}
}
if structSectionJson.NonPODUsed != nil && string(structSectionJson.NonPODUsed) != "[]" {
err = json.Unmarshal(structSectionJson.NonPODUsed, &nonPODJson)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal struct's NonPODUsed section: %w", err)
}
}
for k, v := range structJson {
var memberDefs []StructMemberDef
err := json.Unmarshal(v, &memberDefs)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal struct %s: %w", k, err)
}
for i, member := range memberDefs {
if member.CommentData != nil {
var comment CommentDef
err = json.Unmarshal(member.CommentData, &comment)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal struct comment %s: %w", member.CommentData, err)
}
memberDefs[i].Comment = comment
}
}
isNonPODUsedVal, ok := nonPODJson[k]
var nonPODUsed NonPODUsedType
if !ok {
nonPODUsed = POD
} else {
switch v := isNonPODUsedVal.(type) {
case string:
switch v {
case "inherited":
nonPODUsed = PODInherited
default:
nonPODUsed = NonPOD
}
case bool:
if v {
nonPODUsed = NonPOD
} else {
nonPODUsed = POD
}
default:
return nil, fmt.Errorf("unexpected type for NonPODUsed value %T for struct %s", v, k)
}
}
str := StructDef{
Name: CIdentifier(k),
Members: memberDefs,
NonPODUsed: nonPODUsed,
}
if commentData, ok := structCommentJson[k]; ok {
var structCommentValue CommentDef
err := json.Unmarshal(commentData, &structCommentValue)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal enum comment data %s: %w", k, err)
}
str.CommentAbove = structCommentValue.Above
}
structs = append(structs, str)
}
// sort lexicographically for determenistic generation
sort.Slice(structs, func(i, j int) bool {
return structs[i].Name < structs[j].Name
})
return structs, nil
}