Skip to content

Commit 062a291

Browse files
enhancement: added customized useful-commands file and fixed issue in api.proto
1 parent b23e947 commit 062a291

File tree

6 files changed

+93
-10
lines changed

6 files changed

+93
-10
lines changed

core/internal/languages/golang/frameworks/common-files/copier.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package commonfiles
22

33
import (
4+
"fmt"
45
"github.com/gertd/go-pluralize"
56
corenode "github.com/intelops/compage/core/internal/core/node"
67
"github.com/intelops/compage/core/internal/languages/executor"
8+
commonUtils "github.com/intelops/compage/core/internal/languages/utils"
79
"github.com/intelops/compage/core/internal/utils"
810
"golang.org/x/text/cases"
911
"golang.org/x/text/language"
@@ -35,7 +37,16 @@ type Copier struct {
3537
PluralizeClient *pluralize.Client
3638
}
3739

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 {
3950
SmallResourceNameSingular string
4051
SmallResourceNamePlural string
4152
CapsResourceNameSingular string
@@ -57,9 +68,9 @@ func NewCopier(userName, repositoryName, nodeName, nodeDirectoryName, templatesR
5768
"UserName": userName,
5869
}
5970
// set all grpcResources for main.go.tmpl
60-
var grpcResourcesData []resourceData
71+
var grpcResourcesData []grpcResourceData
6172
for _, r := range grpcResources {
62-
grpcResourcesData = append(grpcResourcesData, resourceData{
73+
grpcResourcesData = append(grpcResourcesData, grpcResourceData{
6374
SmallResourceNameSingular: strings.ToLower(r.Name),
6475
SmallResourceNamePlural: pluralizeClient.Plural(strings.ToLower(r.Name)),
6576
CapsResourceNameSingular: r.Name,
@@ -74,13 +85,15 @@ func NewCopier(userName, repositoryName, nodeName, nodeDirectoryName, templatesR
7485
data["HasGrpcClients"] = HasGrpcClients
7586

7687
// set all grpcResources for main.go.tmpl
77-
var restResourcesData []resourceData
88+
var restResourcesData []restResourceData
7889
for _, r := range restResources {
79-
restResourcesData = append(restResourcesData, resourceData{
90+
restResourcesData = append(restResourcesData, restResourceData{
8091
SmallResourceNameSingular: strings.ToLower(r.Name),
8192
SmallResourceNamePlural: pluralizeClient.Plural(strings.ToLower(r.Name)),
8293
CapsResourceNameSingular: r.Name,
8394
CapsResourceNamePlural: pluralizeClient.Plural(r.Name),
95+
ResourcePostBody: getResourcePostBody(r),
96+
ResourcePutBody: getResourcePutBody(r),
8497
})
8598
}
8699
data["RestResources"] = restResourcesData
@@ -93,7 +106,7 @@ func NewCopier(userName, repositoryName, nodeName, nodeDirectoryName, templatesR
93106
if hasRestClients {
94107
var d []clientData
95108
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)})
97110
}
98111
data["RestClients"] = d
99112
}
@@ -125,6 +138,28 @@ func NewCopier(userName, repositoryName, nodeName, nodeDirectoryName, templatesR
125138
}
126139
}
127140

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+
128163
// CreateCommonFiles creates/copies relevant files to generated project
129164
func (c Copier) CreateCommonFiles() error {
130165
var filePaths []string

core/internal/languages/golang/frameworks/go-gin-server/copier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func (c Copier) copyRestClientResourceFiles(restClient *corenode.RestClient) err
246246
/// add resource specific data to map in c needed for templates.
247247
c.Data["RestClientPort"] = restClient.Port
248248
c.Data["RestClientServiceName"] = restClient.SourceNodeName
249-
c.Data["RestClientSourceNodeID"] = strings.Replace(cases.Title(language.Und, cases.NoLower).String(restClient.SourceNodeID), "-", "_", -1)
249+
c.Data["RestClientSourceNodeID"] = strings.Replace(cases.Title(language.Und, cases.NoLower).String(restClient.SourceNodeID), "-", "", -1)
250250

251251
// copy restClient files to generated project.
252252
targetResourceClientFileName := c.NodeDirectoryName + RestClientPath + "/" + restClient.SourceNodeName + "-" + ClientFile

core/internal/languages/golang/frameworks/go-grpc-server/copier.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,17 @@ func (c Copier) CopyGrpcClientResourceFiles(grpcClient *corenode.GrpcClient) err
288288
func (c Copier) addResourceSpecificTemplateData(resource *corenode.Resource) error {
289289
// make every field public by making its first character capital.
290290
fields := map[string]string{}
291+
protoFields := map[string]string{}
291292
// this slice is needed for grpc resource message generation
292293
var fieldNames []string
293294
for key, value := range resource.Fields {
294295
key = cases.Title(language.Und, cases.NoLower).String(key)
295-
fields[key] = commonUtils.GetProtoBufDataType(value)
296+
fields[key] = value
297+
protoFields[key] = commonUtils.GetProtoBufDataType(value)
296298
fieldNames = append(fieldNames, key)
297299
}
298300
c.Data["Fields"] = fields
301+
c.Data["ProtoFields"] = protoFields
299302
c.Data["FieldNames"] = fieldNames
300303
// db fields
301304
if c.IsSQLDB {

core/internal/languages/utils/common.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,46 @@ func GetMySQLDataType(value string) string {
130130
return "VARCHAR(100)"
131131
}
132132
}
133+
134+
func GetDefaultValueForDataType(value string) interface{} {
135+
switch value {
136+
case "int":
137+
fallthrough
138+
case "int16":
139+
fallthrough
140+
case "int32":
141+
fallthrough
142+
case "int64":
143+
fallthrough
144+
case "uint8":
145+
fallthrough
146+
case "uint16":
147+
fallthrough
148+
case "uint32":
149+
fallthrough
150+
case "uint64":
151+
fallthrough
152+
case "uint":
153+
fallthrough
154+
case "rune":
155+
fallthrough
156+
case "byte":
157+
fallthrough
158+
case "uintptr":
159+
return 1
160+
case "bool":
161+
return true
162+
case "float32":
163+
fallthrough
164+
case "float64":
165+
return 1.0
166+
case "complex64":
167+
fallthrough
168+
case "complex128":
169+
return 1.0
170+
case "string":
171+
return "sample string"
172+
default:
173+
return "defaultValue"
174+
}
175+
}

core/test/generator_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ func TestRestServerGenerator(t *testing.T) {
381381
"resources": [
382382
{
383383
"fields": {
384-
"Name": "string"
384+
"Name": "string",
385+
"Address": "string",
386+
"Age": "int32"
385387
},
386388
"name": "User"
387389
}

0 commit comments

Comments
 (0)