-
Notifications
You must be signed in to change notification settings - Fork 51
/
structures_deffinition.go
104 lines (86 loc) · 2.9 KB
/
structures_deffinition.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
package main
import (
"encoding/json"
"fmt"
"sort"
)
// 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"`
}
// StructDef represents a definition of an ImGui struct.
type StructDef struct {
Name CIdentifier `json:"name"`
CommentAbove string
Members []StructMemberDef `json:"members"`
}
// 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)
)
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)
}
}
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
}
}
str := StructDef{
Name: CIdentifier(k),
Members: memberDefs,
}
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
}