Skip to content

Commit 21c877e

Browse files
enhancement: added protobuf datatypes and refactored SQLDB code
1 parent 7ed2cdf commit 21c877e

File tree

3 files changed

+140
-177
lines changed

3 files changed

+140
-177
lines changed

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

Lines changed: 4 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/intelops/compage/core/internal/core/node"
77
"github.com/intelops/compage/core/internal/languages"
88
"github.com/intelops/compage/core/internal/languages/executor"
9+
commonUtils "github.com/intelops/compage/core/internal/languages/utils"
10+
911
"github.com/intelops/compage/core/internal/utils"
1012
"golang.org/x/text/cases"
1113
"golang.org/x/text/language"
@@ -419,95 +421,9 @@ func (c Copier) CreateRootLevelFiles() error {
419421

420422
func (c Copier) getDBDataType(value string) (string, error) {
421423
if c.SQLDB == Sqlite {
422-
return getSqliteDataType(value), nil
424+
return commonUtils.GetSqliteDataType(value), nil
423425
} else if c.SQLDB == MySQL {
424-
return getMySQLDataType(value), nil
426+
return commonUtils.GetMySQLDataType(value), nil
425427
}
426428
return "", errors.New("database not supported")
427429
}
428-
429-
func getSqliteDataType(value string) string {
430-
switch value {
431-
case "int":
432-
fallthrough
433-
case "int16":
434-
fallthrough
435-
case "int32":
436-
fallthrough
437-
case "int64":
438-
fallthrough
439-
case "uint8":
440-
fallthrough
441-
case "uint16":
442-
fallthrough
443-
case "uint32":
444-
fallthrough
445-
case "uint64":
446-
fallthrough
447-
case "uint":
448-
fallthrough
449-
case "rune":
450-
fallthrough
451-
case "byte":
452-
fallthrough
453-
case "uintptr":
454-
fallthrough
455-
case "bool":
456-
return "INTEGER"
457-
case "float32":
458-
fallthrough
459-
case "float64 ":
460-
fallthrough
461-
case "complex64":
462-
fallthrough
463-
case "complex128":
464-
return "REAL"
465-
case "string":
466-
return "TEXT"
467-
default:
468-
return "TEXT"
469-
}
470-
}
471-
472-
func getMySQLDataType(value string) string {
473-
switch value {
474-
case "int":
475-
fallthrough
476-
case "int16":
477-
fallthrough
478-
case "int32":
479-
fallthrough
480-
case "int64":
481-
fallthrough
482-
case "uint8":
483-
fallthrough
484-
case "uint16":
485-
fallthrough
486-
case "uint32":
487-
fallthrough
488-
case "uint64":
489-
fallthrough
490-
case "uint":
491-
fallthrough
492-
case "rune":
493-
fallthrough
494-
case "byte":
495-
fallthrough
496-
case "uintptr":
497-
return "INT"
498-
case "bool":
499-
return "BOOL"
500-
case "float32":
501-
fallthrough
502-
case "float64 ":
503-
return "FLOAT"
504-
case "complex64":
505-
fallthrough
506-
case "complex128":
507-
return "DOUBLE"
508-
case "string":
509-
return "VARCHAR(100)"
510-
default:
511-
return "VARCHAR(100)"
512-
}
513-
}

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

Lines changed: 4 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/intelops/compage/core/internal/core/node"
77
"github.com/intelops/compage/core/internal/languages"
88
"github.com/intelops/compage/core/internal/languages/executor"
9+
commonUtils "github.com/intelops/compage/core/internal/languages/utils"
910
"github.com/intelops/compage/core/internal/utils"
1011
"golang.org/x/text/cases"
1112
"golang.org/x/text/language"
@@ -285,7 +286,7 @@ func (c Copier) addResourceSpecificTemplateData(resource node.Resource) error {
285286
var fieldNames []string
286287
for key, value := range resource.Fields {
287288
key = cases.Title(language.Und, cases.NoLower).String(key)
288-
fields[key] = value
289+
fields[key] = commonUtils.GetProtoBufDataType(value)
289290
fieldNames = append(fieldNames, key)
290291
}
291292
c.Data["Fields"] = fields
@@ -453,9 +454,9 @@ func (c Copier) CreateRootLevelFiles() error {
453454

454455
func (c Copier) getDBDataType(value string) (string, error) {
455456
if c.SQLDB == Sqlite {
456-
return getSqliteDataType(value), nil
457+
return commonUtils.GetSqliteDataType(value), nil
457458
} else if c.SQLDB == MySQL {
458-
return getMySQLDataType(value), nil
459+
return commonUtils.GetMySQLDataType(value), nil
459460
}
460461
return "", errors.New("database not supported")
461462
}
@@ -472,89 +473,3 @@ func (c Copier) copySelfGrpcClientResourceFiles() error {
472473
// apply template
473474
return executor.Execute(filePaths, c.Data)
474475
}
475-
476-
func getSqliteDataType(value string) string {
477-
switch value {
478-
case "int":
479-
fallthrough
480-
case "int16":
481-
fallthrough
482-
case "int32":
483-
fallthrough
484-
case "int64":
485-
fallthrough
486-
case "uint8":
487-
fallthrough
488-
case "uint16":
489-
fallthrough
490-
case "uint32":
491-
fallthrough
492-
case "uint64":
493-
fallthrough
494-
case "uint":
495-
fallthrough
496-
case "rune":
497-
fallthrough
498-
case "byte":
499-
fallthrough
500-
case "uintptr":
501-
fallthrough
502-
case "bool":
503-
return "INTEGER"
504-
case "float32":
505-
fallthrough
506-
case "float64 ":
507-
fallthrough
508-
case "complex64":
509-
fallthrough
510-
case "complex128":
511-
return "REAL"
512-
case "string":
513-
return "TEXT"
514-
default:
515-
return "TEXT"
516-
}
517-
}
518-
519-
func getMySQLDataType(value string) string {
520-
switch value {
521-
case "int":
522-
fallthrough
523-
case "int16":
524-
fallthrough
525-
case "int32":
526-
fallthrough
527-
case "int64":
528-
fallthrough
529-
case "uint8":
530-
fallthrough
531-
case "uint16":
532-
fallthrough
533-
case "uint32":
534-
fallthrough
535-
case "uint64":
536-
fallthrough
537-
case "uint":
538-
fallthrough
539-
case "rune":
540-
fallthrough
541-
case "byte":
542-
fallthrough
543-
case "uintptr":
544-
return "INT"
545-
case "bool":
546-
return "BOOL"
547-
case "float32":
548-
fallthrough
549-
case "float64 ":
550-
return "FLOAT"
551-
case "complex64":
552-
fallthrough
553-
case "complex128":
554-
return "DOUBLE"
555-
case "string":
556-
return "VARCHAR(100)"
557-
default:
558-
return "VARCHAR(100)"
559-
}
560-
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package utils
2+
3+
func GetProtoBufDataType(value string) string {
4+
switch value {
5+
case "rune":
6+
fallthrough
7+
case "byte":
8+
fallthrough
9+
case "uintptr":
10+
fallthrough
11+
case "int":
12+
fallthrough
13+
case "int16":
14+
fallthrough
15+
case "int32":
16+
return "int32"
17+
case "int64":
18+
return "int64"
19+
case "uint":
20+
fallthrough
21+
case "uint8":
22+
fallthrough
23+
case "uint16":
24+
fallthrough
25+
case "uint32":
26+
return "uint32"
27+
case "uint64":
28+
return "int64"
29+
case "bool":
30+
return "bool"
31+
case "float32":
32+
return "float"
33+
case "complex64":
34+
fallthrough
35+
case "complex128":
36+
fallthrough
37+
case "float64 ":
38+
return "double"
39+
case "[]byte":
40+
return "bytes"
41+
case "string":
42+
return "string"
43+
default:
44+
return "string"
45+
}
46+
}
47+
48+
func GetSqliteDataType(value string) string {
49+
switch value {
50+
case "int":
51+
fallthrough
52+
case "int16":
53+
fallthrough
54+
case "int32":
55+
fallthrough
56+
case "int64":
57+
fallthrough
58+
case "uint8":
59+
fallthrough
60+
case "uint16":
61+
fallthrough
62+
case "uint32":
63+
fallthrough
64+
case "uint64":
65+
fallthrough
66+
case "uint":
67+
fallthrough
68+
case "rune":
69+
fallthrough
70+
case "byte":
71+
fallthrough
72+
case "uintptr":
73+
fallthrough
74+
case "bool":
75+
return "INTEGER"
76+
case "float32":
77+
fallthrough
78+
case "float64 ":
79+
fallthrough
80+
case "complex64":
81+
fallthrough
82+
case "complex128":
83+
return "REAL"
84+
case "string":
85+
return "TEXT"
86+
default:
87+
return "TEXT"
88+
}
89+
}
90+
91+
func GetMySQLDataType(value string) string {
92+
switch value {
93+
case "int":
94+
fallthrough
95+
case "int16":
96+
fallthrough
97+
case "int32":
98+
fallthrough
99+
case "int64":
100+
fallthrough
101+
case "uint8":
102+
fallthrough
103+
case "uint16":
104+
fallthrough
105+
case "uint32":
106+
fallthrough
107+
case "uint64":
108+
fallthrough
109+
case "uint":
110+
fallthrough
111+
case "rune":
112+
fallthrough
113+
case "byte":
114+
fallthrough
115+
case "uintptr":
116+
return "INT"
117+
case "bool":
118+
return "BOOL"
119+
case "float32":
120+
fallthrough
121+
case "float64 ":
122+
return "FLOAT"
123+
case "complex64":
124+
fallthrough
125+
case "complex128":
126+
return "DOUBLE"
127+
case "string":
128+
return "VARCHAR(100)"
129+
default:
130+
return "VARCHAR(100)"
131+
}
132+
}

0 commit comments

Comments
 (0)