1
1
package commonfiles
2
2
3
3
import (
4
+ "fmt"
4
5
"github.com/gertd/go-pluralize"
5
6
corenode "github.com/intelops/compage/core/internal/core/node"
6
7
"github.com/intelops/compage/core/internal/languages/executor"
8
+ commonUtils "github.com/intelops/compage/core/internal/languages/utils"
7
9
"github.com/intelops/compage/core/internal/utils"
8
10
"golang.org/x/text/cases"
9
11
"golang.org/x/text/language"
@@ -35,7 +37,16 @@ type Copier struct {
35
37
PluralizeClient * pluralize.Client
36
38
}
37
39
38
- type resourceData struct {
40
+ type restResourceData struct {
41
+ SmallResourceNameSingular string
42
+ SmallResourceNamePlural string
43
+ CapsResourceNameSingular string
44
+ CapsResourceNamePlural string
45
+ ResourcePostBody string
46
+ ResourcePutBody string
47
+ }
48
+
49
+ type grpcResourceData struct {
39
50
SmallResourceNameSingular string
40
51
SmallResourceNamePlural string
41
52
CapsResourceNameSingular string
@@ -57,9 +68,9 @@ func NewCopier(userName, repositoryName, nodeName, nodeDirectoryName, templatesR
57
68
"UserName" : userName ,
58
69
}
59
70
// set all grpcResources for main.go.tmpl
60
- var grpcResourcesData []resourceData
71
+ var grpcResourcesData []grpcResourceData
61
72
for _ , r := range grpcResources {
62
- grpcResourcesData = append (grpcResourcesData , resourceData {
73
+ grpcResourcesData = append (grpcResourcesData , grpcResourceData {
63
74
SmallResourceNameSingular : strings .ToLower (r .Name ),
64
75
SmallResourceNamePlural : pluralizeClient .Plural (strings .ToLower (r .Name )),
65
76
CapsResourceNameSingular : r .Name ,
@@ -74,13 +85,15 @@ func NewCopier(userName, repositoryName, nodeName, nodeDirectoryName, templatesR
74
85
data ["HasGrpcClients" ] = HasGrpcClients
75
86
76
87
// set all grpcResources for main.go.tmpl
77
- var restResourcesData []resourceData
88
+ var restResourcesData []restResourceData
78
89
for _ , r := range restResources {
79
- restResourcesData = append (restResourcesData , resourceData {
90
+ restResourcesData = append (restResourcesData , restResourceData {
80
91
SmallResourceNameSingular : strings .ToLower (r .Name ),
81
92
SmallResourceNamePlural : pluralizeClient .Plural (strings .ToLower (r .Name )),
82
93
CapsResourceNameSingular : r .Name ,
83
94
CapsResourceNamePlural : pluralizeClient .Plural (r .Name ),
95
+ ResourcePostBody : getResourcePostBody (r ),
96
+ ResourcePutBody : getResourcePutBody (r ),
84
97
})
85
98
}
86
99
data ["RestResources" ] = restResourcesData
@@ -93,7 +106,7 @@ func NewCopier(userName, repositoryName, nodeName, nodeDirectoryName, templatesR
93
106
if hasRestClients {
94
107
var d []clientData
95
108
for _ , restClient := range restClients {
96
- d = append (d , clientData {SourceNodeID : strings .Replace (cases .Title (language .Und , cases .NoLower ).String (restClient .SourceNodeID ), "-" , "_ " , - 1 )})
109
+ d = append (d , clientData {SourceNodeID : strings .Replace (cases .Title (language .Und , cases .NoLower ).String (restClient .SourceNodeID ), "-" , "" , - 1 )})
97
110
}
98
111
data ["RestClients" ] = d
99
112
}
@@ -125,6 +138,28 @@ func NewCopier(userName, repositoryName, nodeName, nodeDirectoryName, templatesR
125
138
}
126
139
}
127
140
141
+ func getResourcePostBody (r * corenode.Resource ) string {
142
+ postBody := "{"
143
+ for key , value := range r .Fields {
144
+ sprintf := fmt .Sprintf ("\" %s\" : \" %v\" ," , key , commonUtils .GetDefaultValueForDataType (value ))
145
+ postBody += sprintf
146
+ }
147
+ postBody = strings .TrimSuffix (postBody , "," )
148
+ postBody += "}"
149
+ return postBody
150
+ }
151
+
152
+ func getResourcePutBody (r * corenode.Resource ) string {
153
+ putBody := fmt .Sprintf ("{\" %s\" : %v," , "Id" , 123 )
154
+ for key , value := range r .Fields {
155
+ sprintf := fmt .Sprintf ("\" %s\" : \" %v\" ," , key , commonUtils .GetDefaultValueForDataType (value ))
156
+ putBody += sprintf
157
+ }
158
+ putBody = strings .TrimSuffix (putBody , "," )
159
+ putBody += "}"
160
+ return putBody
161
+ }
162
+
128
163
// CreateCommonFiles creates/copies relevant files to generated project
129
164
func (c Copier ) CreateCommonFiles () error {
130
165
var filePaths []string
0 commit comments