/
gopkg.go
194 lines (170 loc) · 4.4 KB
/
gopkg.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package gopkg
import (
"fmt"
"reflect"
"slices"
"strings"
"k8s.io/gengo/parser"
"k8s.io/gengo/types"
"github.com/andreasgerstmayr/gen-api-docs/pkg/parser/format"
)
func fieldName(m types.Member) string {
v := reflect.StructTag(m.Tags).Get("json")
v = strings.TrimSuffix(v, ",inline")
v = strings.TrimSuffix(v, ",omitempty")
return v
}
func fieldEmbedded(m types.Member) bool {
return strings.Contains(reflect.StructTag(m.Tags).Get("json"), ",inline")
}
func formatComment(commentLines []string) []string {
if len(commentLines) == 1 && commentLines[0] == "" {
return []string{}
}
out := []string{}
for _, line := range commentLines {
if !strings.HasPrefix(line, "+") {
out = append(out, line)
}
}
// trim empty lines at end
for i := len(out) - 1; i > 0; i-- {
if out[i] == "" {
out = out[:i]
} else {
break
}
}
return out
}
// deref resolves pointers and aliases
func deref(t *types.Type) *types.Type {
switch t.Kind {
case types.Pointer:
return deref(t.Elem)
case types.Alias:
return deref(t.Underlying)
default:
return t
}
}
func getDefaultValue(name string, type_ *types.Type) string {
t := deref(type_)
ts := t.String()
if name == "lastTransitionTime" {
print()
}
switch {
case format.HideCoreTypes && slices.Contains(format.CoreTypes, name):
return "{}"
case ts == "k8s.io/apimachinery/pkg/api/resource.Quantity":
return "0Gi"
case ts == "k8s.io/apimachinery/pkg/apis/meta/v1.Duration":
return "0h"
case ts == "k8s.io/apimachinery/pkg/apis/meta/v1.Time":
return "\"2006-01-02T15:04:05Z\""
case ts == "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1.JSON":
return "{}"
case ts == "map[string]string":
return "{}"
case ts == "string":
return "\"\""
case ts == "bool":
return "false"
case ts == "int" || ts == "int64":
return "0"
default:
return ""
}
}
func render(type_ *types.Type, prop *format.Prop, parentCommentLines []string) {
type_ = deref(type_)
switch type_.Kind {
case types.Struct:
for _, member := range type_.Members {
if fieldEmbedded(member) {
render(member.Type, prop, member.CommentLines)
continue
}
name := fieldName(member)
if name == "" {
continue
}
if name == "status" {
print()
}
comment := formatComment(member.CommentLines)
if len(comment) == 0 {
comment = formatComment(member.Type.CommentLines)
}
p := &format.Prop{
Key: name,
Comment: comment,
ScalarValue: getDefaultValue(name, member.Type),
}
prop.Properties = append(prop.Properties, p)
if p.ScalarValue == "" {
render(member.Type, p, member.CommentLines)
}
}
case types.Map:
p := &format.Prop{
Key: "\"key\"",
Comment: formatComment(type_.CommentLines),
ScalarValue: getDefaultValue("", type_.Elem),
}
if len(parentCommentLines) > 0 && strings.Contains(parentCommentLines[0], "Requests describes the minimum amount of compute resources required.") {
prop.Properties = []*format.Prop{
{Key: "cpu", ScalarValue: "\"500m\""},
{Key: "memory", ScalarValue: "\"1Gi\""},
}
} else if len(parentCommentLines) > 0 && strings.Contains(parentCommentLines[0], "Limits describes the maximum amount of compute resources allowed.") {
prop.Properties = []*format.Prop{
{Key: "cpu", ScalarValue: "\"750m\""},
{Key: "memory", ScalarValue: "\"2Gi\""},
}
} else {
prop.Properties = []*format.Prop{p}
if p.ScalarValue == "" {
render(type_.Elem, p, parentCommentLines)
}
}
case types.Slice:
if prop.Key == "permissions" {
print()
}
prop.ListItem = &format.Prop{
Comment: formatComment(type_.Elem.CommentLines),
ScalarValue: getDefaultValue("", type_.Elem),
}
if prop.ListItem.ScalarValue == "" {
render(type_.Elem, prop.ListItem, parentCommentLines)
}
}
}
func parsePackage(type_ *types.Type) format.Prop {
prop := format.Prop{}
render(type_, &prop, []string{})
return prop
}
func Parse(pkgName string, typeName string) (format.Prop, error) {
p := parser.New()
err := p.AddDirRecursive(pkgName)
if err != nil {
return format.Prop{}, err
}
pkgs, err := p.FindTypes()
if err != nil {
return format.Prop{}, err
}
for name, pkg := range pkgs {
if name == pkgName {
type_, ok := pkg.Types[typeName]
if !ok {
return format.Prop{}, fmt.Errorf("cannot find type %s", typeName)
}
return parsePackage(type_), nil
}
}
return format.Prop{}, fmt.Errorf("cannot find package %s", pkgName)
}