/
apis.go
112 lines (102 loc) · 2.72 KB
/
apis.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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package ack
import (
"path/filepath"
"strings"
ttpl "text/template"
"github.com/aws/aws-controllers-k8s/pkg/generate"
"github.com/aws/aws-controllers-k8s/pkg/generate/templateset"
ackmodel "github.com/aws/aws-controllers-k8s/pkg/model"
"github.com/iancoleman/strcase"
)
var (
apisTemplatePaths = []string{
"apis/doc.go.tpl",
"apis/enums.go.tpl",
"apis/groupversion_info.go.tpl",
"apis/types.go.tpl",
}
apisIncludePaths = []string{
"boilerplate.go.tpl",
"apis/enum_def.go.tpl",
"apis/type_def.go.tpl",
}
apisCopyPaths = []string{}
apisFuncMap = ttpl.FuncMap{
"Join": strings.Join,
}
)
// APIs returns a pointer to a TemplateSet containing all the templates for
// generating ACK service controller's apis/ contents
func APIs(
g *generate.Generator,
templateBasePath string,
) (*templateset.TemplateSet, error) {
enumDefs, err := g.GetEnumDefs()
if err != nil {
return nil, err
}
typeDefs, typeImports, err := g.GetTypeDefs()
if err != nil {
return nil, err
}
crds, err := g.GetCRDs()
if err != nil {
return nil, err
}
ts := templateset.New(
templateBasePath,
apisIncludePaths,
apisCopyPaths,
apisFuncMap,
)
metaVars := g.MetaVars()
apiVars := &templateAPIVars{
metaVars,
enumDefs,
typeDefs,
typeImports,
}
for _, path := range apisTemplatePaths {
outPath := strings.TrimSuffix(filepath.Base(path), ".tpl")
if err = ts.Add(outPath, path, apiVars); err != nil {
return nil, err
}
}
for _, crd := range crds {
crdFileName := strcase.ToSnake(crd.Kind) + ".go"
crdVars := &templateCRDVars{
metaVars,
crd,
}
if err = ts.Add(crdFileName, "apis/crd.go.tpl", crdVars); err != nil {
return nil, err
}
}
return ts, nil
}
// templateAPIVars contains template variables for templates that output Go
// code in the /services/$SERVICE/apis/$API_VERSION directory
type templateAPIVars struct {
templateset.MetaVars
EnumDefs []*ackmodel.EnumDef
TypeDefs []*ackmodel.TypeDef
Imports map[string]string
}
// templateCRDVars contains template variables for the template that outputs Go
// code for a single top-level resource's API definition
type templateCRDVars struct {
templateset.MetaVars
CRD *ackmodel.CRD
}