Skip to content

Commit 636cab3

Browse files
committed
update
1 parent 10276ef commit 636cab3

29 files changed

+1322
-1435
lines changed

fs_type.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
8181
return "int16"
8282

8383
case "float", "double precision", "float8", "pg_catalog.float8":
84-
return "float64"
84+
return "float"
8585

8686
case "real", "float4", "pg_catalog.float4":
8787
return "float32"
@@ -98,6 +98,9 @@ func postgresType(req *plugin.CodeGenRequest, col *plugin.Column) string {
9898
case "jsonb":
9999
return "string"
100100

101+
case "tsvector":
102+
return "string"
103+
101104
case "bytea", "blob", "pg_catalog.bytea":
102105
return "[]byte"
103106

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ go 1.19
44

55
require (
66
github.com/golang/protobuf v1.5.0
7-
github.com/iancoleman/strcase v0.2.0
87
github.com/jinzhu/inflection v1.0.0
98
github.com/pkg/errors v0.9.1
109
github.com/samber/lo v1.38.1
10+
github.com/stoewer/go-strcase v1.3.0
1111
golang.org/x/text v0.9.0
1212
google.golang.org/protobuf v1.28.1
1313
)

go.sum

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
25
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
36
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
47
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
58
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
6-
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
7-
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
89
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
910
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
1011
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1112
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
13+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
14+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1215
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
1316
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
17+
github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs=
18+
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
19+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
20+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
21+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
22+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
23+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
24+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
25+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
1426
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
1527
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
1628
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
@@ -19,3 +31,7 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
1931
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
2032
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
2133
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
34+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
35+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
36+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
37+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/camelsnakekebab/util.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package camelsnakekebab
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
7+
"golang.org/x/text/cases"
8+
"golang.org/x/text/language"
9+
)
10+
11+
func SplitWords(str string) []string {
12+
// A pattern that matches all known word separators
13+
wordSeparatorPattern := regexp.MustCompile(`\s+|_|-(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z_-])(?=[A-Z])|(?<=[A-Za-z0-9])(?=[^A-Za-z0-9])`)
14+
15+
// Split the input string using the pattern as separator
16+
result := wordSeparatorPattern.Split(str, -1)
17+
18+
// Remove empty strings from the array
19+
var words []string
20+
for _, w := range result {
21+
if strings.TrimSpace(w) != "" {
22+
words = append(words, w)
23+
}
24+
}
25+
26+
return words
27+
}
28+
29+
func convertCase(convertFirst func(string) string, convertRest func(string) string, sep string, str string) string {
30+
words := SplitWords(str)
31+
if len(words) == 0 {
32+
return ""
33+
}
34+
result := make([]string, len(words))
35+
result[0] = convertFirst(words[0])
36+
for i := 1; i < len(words); i++ {
37+
result[i] = convertRest(words[i])
38+
}
39+
return strings.Join(result, sep)
40+
}
41+
42+
func titleCase(s string) string {
43+
// Create a TitleConverter with the current locale's title case rules
44+
tc := cases.Title(language.English)
45+
46+
// Convert the input string to title case
47+
return tc.String(s)
48+
}
49+
50+
func LowerCamelCase(str string) string {
51+
return convertCase(strings.ToLower, titleCase, "", str)
52+
}
53+
54+
func UpperCamelCase(str string) string {
55+
return convertCase(titleCase, titleCase, "", str)
56+
}
57+
58+
func LowerSnakeCase(str string) string {
59+
return convertCase(strings.ToLower, strings.ToLower, "_", str)
60+
}
61+
62+
func UpperSnakeCase(str string) string {
63+
return convertCase(titleCase, titleCase, "_", str)
64+
}
65+
66+
func ConstantCase(str string) string {
67+
return convertCase(strings.ToUpper, strings.ToUpper, "_", str)
68+
}
69+
70+
func KebabCase(str string) string {
71+
return convertCase(strings.ToLower, strings.ToLower, "-", str)
72+
}

internal/sdk/utils.go

+5-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"strings"
55
"unicode"
66

7-
"github.com/iancoleman/strcase"
7+
"github.com/stoewer/go-strcase"
88
"golang.org/x/text/cases"
99
"golang.org/x/text/language"
1010
)
@@ -39,34 +39,19 @@ func ToSnakeCase(s string) string {
3939
if s == "ID" {
4040
return "id"
4141
}
42-
return strcase.ToSnake(s)
42+
return strcase.SnakeCase(s)
4343
}
4444

4545
func ToPascalCase(s string) string {
46-
return toCamelInitCase(s, true)
46+
return strcase.UpperCamelCase(s)
4747
}
4848

4949
func ToLowerCamelCase(s string) string {
50-
return strcase.ToLowerCamel(s)
50+
return strcase.LowerCamelCase(s)
5151
}
5252

5353
func ToCamelCase(s string) string {
54-
return strcase.ToLowerCamel(s)
55-
}
56-
func toCamelInitCase(name string, initUpper bool) string {
57-
out := ""
58-
for i, p := range strings.Split(name, "_") {
59-
if !initUpper && i == 0 {
60-
out += p
61-
continue
62-
}
63-
if p == "id" {
64-
out += "ID"
65-
} else {
66-
out += titleCase(p)
67-
}
68-
}
69-
return out
54+
return strcase.UpperCamelCase(s)
7055
}
7156

7257
// Go string literals cannot contain backtick. If a string contains

query.go

+28-2
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,48 @@ func (v QueryValue) NpgsqlParams() string {
124124
// Construct the PostgreSQL params string
125125
var sqlParamsString string
126126
for idx, columnName := range columnNames {
127-
columnNameFull := sdk.ToSnakeCase(columnName)
127+
columnNameFull := sdk.ToLowerCamelCase(columnName)
128128
if isInStruct[idx] {
129129
columnNameFull = "arg." + sdk.ToPascalCase(columnNameFull)
130130
}
131-
sqlParamsString += fmt.Sprintf("\"@%s\", Sql.%s %s", sdk.ToSnakeCase(columnName), type2readerFunc(columnTypes[idx]), columnNameFull)
131+
sqlParamsString += fmt.Sprintf("\"@%s\", Sql.%s %s", sdk.ToSnakeCase(columnName), type2readerFuncParam(columnTypes[idx]), columnNameFull)
132132
if idx < len(columnNames)-1 {
133133
sqlParamsString += "; "
134134
}
135135
}
136136
return sqlParamsString
137137
}
138138

139+
func type2readerFuncParam(t string) string {
140+
if t == "int32" {
141+
t = "int"
142+
}
143+
if t == "float64" || t == "float" {
144+
t = "double"
145+
}
146+
if t == "float32" {
147+
t = "float"
148+
}
149+
150+
if t == "DateTime" {
151+
t = "date"
152+
}
153+
154+
return sdk.ToLowerCamelCase(strings.Replace(t, " option", "OrNone", 1))
155+
}
156+
139157
func type2readerFunc(t string) string {
140158
if t == "int32" {
141159
t = "int"
142160
}
161+
if t == "float64" || t == "float" {
162+
t = "double"
163+
}
164+
165+
if t == "float32" {
166+
t = "float"
167+
}
168+
143169
return sdk.ToLowerCamelCase(strings.Replace(t, " option", "OrNone", 1))
144170
}
145171

result.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
9494
}
9595

9696
if len(query.Columns) == 1 {
97+
// {
98+
// "text": "SELECT COUNT(*)\nFROM chat_message\nWHERE user_id = $1\nAND created_at \u003e= NOW() - INTERVAL '10 minutes'",
99+
// "name": "GetChatMessagesCount",
100+
// "cmd": ":one",
101+
// "columns": [
102+
// {
103+
// "name": "count",
104+
// "not_null": true,
105+
// "length": -1,
106+
// "is_func_call": true,
107+
// "type": {
108+
// "name": "bigint"
109+
// }
110+
// }
111+
// ],
112+
// },
97113
c := query.Columns[0]
98114
name := columnName(c, 0)
99115
if c.IsFuncCall {
@@ -167,8 +183,6 @@ func argName(name string) string {
167183
for i, p := range strings.Split(name, "_") {
168184
if i == 0 {
169185
out += strings.ToLower(p)
170-
} else if p == "id" {
171-
out += "ID"
172186
} else {
173187
out += strings.Title(p)
174188
}

templates/query.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type {{.Ret.Type}} = { {{- range .Ret.Struct.Fields}}
3838
let {{.MethodName}} (db: NpgsqlConnection) ({{.Arg.Pair}}) =
3939
let reader = fun (read:RowReader) -> {
4040
{{- range $i, $e := .Ret.UniqueFields}}
41-
{{$e.Name}} = read.{{$e.Type | type2readerFunc}} "{{$e.Name | snakeCase }}"
41+
{{$e.Name}} = read.{{$e.Type | type2readerFunc }} "{{$e.Name }}"
4242
{{- end -}}
4343
}
4444

@@ -55,7 +55,7 @@ let {{.MethodName}} (db: NpgsqlConnection) ({{.Arg.Pair}}) =
5555
let {{.MethodName}} (db: NpgsqlConnection) ({{.Arg.Pair}}) =
5656
let reader = fun (read:RowReader) -> {
5757
{{- range $i, $e := .Ret.UniqueFields}}
58-
{{$e.Name}} = read.{{$e.Type | type2readerFunc }} "{{$e.Name | snakeCase }}"
58+
{{$e.Name}} = read.{{$e.Type | type2readerFunc }} "{{$e.Name}}"
5959
{{- end -}}
6060
}
6161
db

testdata/gen/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
clear:
2+
rm -rf .ionide/symbolCache.db

0 commit comments

Comments
 (0)