This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
/
apigen.go
157 lines (136 loc) · 5.07 KB
/
apigen.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
package main
import (
"fmt"
"path/filepath"
"sort"
"strings"
"github.com/13k/geyser"
"github.com/iancoleman/strcase"
)
const (
pkgPathNetHTTP = "net/http"
pkgPathTesting = "testing"
pkgPathGeyser = "github.com/13k/geyser"
pkgPathGeyserDota2 = "github.com/13k/geyser/dota2"
pkgPathTestifyAssert = "github.com/stretchr/testify/assert"
pkgPathTestifyRequire = "github.com/stretchr/testify/require"
fnamefInterface = "%s.go"
fnamefResults = "%s_results.go"
fnamefTests = "%s_test.go"
srcPkgTestifyAssert = "assert"
srcPkgTestifyRequire = "require"
srcPkgGeyser = "geyser"
srcPkgGeyserDota2 = "dota2"
srcfPkgTest = "%s_test"
srcSchemaInterfacesCtor = "MustNewSchemaInterfaces"
srcSchemaInterface = "SchemaInterface"
srcSchemaInterfaceKey = "SchemaInterfaceKey"
srcSchemaMethodsCtor = "MustNewSchemaMethods"
srcSchemaMethod = "SchemaMethod"
srcSchemaMethodKey = "SchemaMethodKey"
srcSchemaMethodParamsCtor = "NewSchemaMethodParams"
srcSchemaMethodParam = "SchemaMethodParam"
srcClient = "Client"
srcRequest = "Request"
srcfStructCtorName = "New%s"
srcfSchemaVarName = "Schema%s"
srcIntefaceMethodNotFoundError = "InterfaceMethodNotFoundError"
commentDisclaimer = "Code generated by github.com/13k/geyser/apigen. DO NOT EDIT."
commentfDisclaimerInterface = "API interface: %s."
commentfSchemaVar = "%s stores the SchemaInterfaces for interface %s."
commentfStruct = "%s represents interface %s."
commentfSupportedAppIDs = "Supported AppIDs: %s."
commentfSupportedVersions = "Supported versions: %s."
commentStructUndoc = "This is an undocumented interface."
commentfStructCtor = "%s creates a new %s interface."
commentfStructGetter = "%s creates a new %s interface."
commentfStructMethodHeader = "%s creates a Request for interface method %s."
commentStructMethodUndoc = "This is an undocumented method."
commentfParamsVersioned = "Parameters (v%d)"
commentParams = "Parameters"
commentfStructResult = "%s holds the result of the method %s/%s."
errfUnknownInterfaceFilename = "Unknown filename for interface %q"
errfUnknownHTTPMethod = "Unknown HTTP method %q of interface method %q/%q"
testfInvalidResultType = "invalid result type %T"
testfInvalidErrorType = "invalid error type %T"
)
type APIGen struct {
baseName string
interfaces geyser.SchemaInterfacesGroup
groupedMethods map[string]geyser.SchemaMethodsGroup
methodsNames []string
appIDs []uint32
requiredAppID bool
undoc bool
pkgPath string
pkgName string
testPkgName string
schemaVarName string
structName string
structCtorName string
interfaceFile *GeneratedFile
resultsFile *GeneratedFile
testsFile *GeneratedFile
}
func NewAPIGen(interfaces geyser.SchemaInterfacesGroup, pkgPath, pkgName, outputDir, basefile string) (*APIGen, error) {
baseName := interfaces.Name()
appIDs := interfaces.AppIDs()
groupedMethods, err := interfaces.GroupMethods()
if err != nil {
return nil, err
}
testPkgName := fmt.Sprintf(srcfPkgTest, pkgName)
structName := strings.TrimPrefix(baseName, "I")
structCtorName := fmt.Sprintf(srcfStructCtorName, structName)
schemaVarName := fmt.Sprintf(srcfSchemaVarName, structName)
interfaceFname := fmt.Sprintf(fnamefInterface, basefile)
resultsFname := fmt.Sprintf(fnamefResults, basefile)
testsFname := fmt.Sprintf(fnamefTests, basefile)
interfaceFile := NewGeneratedFile(filepath.Join(outputDir, interfaceFname))
resultsFile := NewGeneratedFile(filepath.Join(outputDir, resultsFname))
testsFile := NewGeneratedFile(filepath.Join(outputDir, testsFname))
methodsNames := make([]string, 0, len(groupedMethods))
for name := range groupedMethods {
methodsNames = append(methodsNames, name)
}
sort.Slice(appIDs, func(i, j int) bool {
return appIDs[i] < appIDs[j]
})
sort.Strings(methodsNames)
undoc := true
// if at least 1 interface in the group is documented, consider the struct documented
for _, si := range interfaces {
if !si.Undocumented {
undoc = false
break
}
}
g := &APIGen{
baseName: baseName,
interfaces: interfaces,
groupedMethods: groupedMethods,
methodsNames: methodsNames,
appIDs: appIDs,
requiredAppID: len(appIDs) > 0,
undoc: undoc,
pkgPath: pkgPath,
pkgName: pkgName,
testPkgName: testPkgName,
structName: structName,
structCtorName: structCtorName,
schemaVarName: schemaVarName,
interfaceFile: interfaceFile,
resultsFile: resultsFile,
testsFile: testsFile,
}
return g, nil
}
func (g *APIGen) methodFuncName(methodName string) string {
if methodName[0] >= 'a' && methodName[0] <= 'z' {
return strcase.ToCamel(methodName)
}
return methodName
}
func (g *APIGen) methodResultStructName(methodName string) string {
return g.structName + g.methodFuncName(methodName)
}