Skip to content

Commit b09e768

Browse files
authored
Dev008 (#135)
* fix pgsql cast
1 parent c4dd193 commit b09e768

File tree

13 files changed

+194
-81
lines changed

13 files changed

+194
-81
lines changed

_example/go-gen/common_orm.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_example/go-gen/model_orm.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ require (
1010
github.com/lib/pq v1.10.9
1111
github.com/mailru/easyjson v0.9.0
1212
github.com/mattn/go-sqlite3 v1.14.30
13-
github.com/miekg/dns v1.1.67
13+
github.com/miekg/dns v1.1.68
1414
github.com/oschwald/geoip2-golang v1.13.0
1515
github.com/prometheus/client_golang v1.23.0
1616
github.com/prometheus/client_model v0.6.2
17-
go.osspkg.com/algorithms v1.5.1
17+
go.osspkg.com/algorithms v1.5.2
1818
go.osspkg.com/casecheck v0.3.0
1919
go.osspkg.com/config v0.1.3
2020
go.osspkg.com/console v0.3.3
@@ -75,10 +75,10 @@ require (
7575
github.com/prometheus/common v0.65.0 // indirect
7676
github.com/prometheus/procfs v0.16.1 // indirect
7777
go.etcd.io/bbolt v1.4.0 // indirect
78-
golang.org/x/net v0.41.0 // indirect
79-
golang.org/x/sync v0.15.0 // indirect
80-
golang.org/x/sys v0.33.0 // indirect
81-
golang.org/x/text v0.26.0 // indirect
82-
golang.org/x/tools v0.34.0 // indirect
78+
golang.org/x/net v0.42.0 // indirect
79+
golang.org/x/sync v0.16.0 // indirect
80+
golang.org/x/sys v0.34.0 // indirect
81+
golang.org/x/text v0.27.0 // indirect
82+
golang.org/x/tools v0.35.0 // indirect
8383
gopkg.in/yaml.v3 v3.0.1 // indirect
8484
)

go.sum

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4
8282
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
8383
github.com/mattn/go-sqlite3 v1.14.30 h1:bVreufq3EAIG1Quvws73du3/QgdeZ3myglJlrzSYYCY=
8484
github.com/mattn/go-sqlite3 v1.14.30/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
85-
github.com/miekg/dns v1.1.67 h1:kg0EHj0G4bfT5/oOys6HhZw4vmMlnoZ+gDu8tJ/AlI0=
86-
github.com/miekg/dns v1.1.67/go.mod h1:fujopn7TB3Pu3JM69XaawiU0wqjpL9/8xGop5UrTPps=
85+
github.com/miekg/dns v1.1.68 h1:jsSRkNozw7G/mnmXULynzMNIsgY2dHC8LO6U6Ij2JEA=
86+
github.com/miekg/dns v1.1.68/go.mod h1:fujopn7TB3Pu3JM69XaawiU0wqjpL9/8xGop5UrTPps=
8787
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
8888
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
8989
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -118,8 +118,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
118118
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
119119
go.etcd.io/bbolt v1.4.0 h1:TU77id3TnN/zKr7CO/uk+fBCwF2jGcMuw2B/FMAzYIk=
120120
go.etcd.io/bbolt v1.4.0/go.mod h1:AsD+OCi/qPN1giOX1aiLAha3o1U8rAz65bvN4j0sRuk=
121-
go.osspkg.com/algorithms v1.5.1 h1:WQ4taaMJZKvTOLXm5w2tt6Cte/kN4NG0GJww2oWCG+I=
122-
go.osspkg.com/algorithms v1.5.1/go.mod h1:Dm8yePWdcvzIdPGDQTuusNpCin8kMGMLaGOAQ62GREc=
121+
go.osspkg.com/algorithms v1.5.2 h1:hEBJzPkO34vFPeSXEI7gKzVWxNQKRGcHRe/c4Q3dHWw=
122+
go.osspkg.com/algorithms v1.5.2/go.mod h1:Dm8yePWdcvzIdPGDQTuusNpCin8kMGMLaGOAQ62GREc=
123123
go.osspkg.com/casecheck v0.3.0 h1:x15blEszElbrHrEH5H02JIIhGIg/lGZzIt1kQlD3pwM=
124124
go.osspkg.com/casecheck v0.3.0/go.mod h1:TRFXDMFJEOtnlp3ET2Hix3osbxwPWhvaiT/HfD3+gBA=
125125
go.osspkg.com/config v0.1.3 h1:LAgTTYFzoIf4VNBIpFtjG81rR0/3SG29r+1D8I/YBzs=
@@ -160,19 +160,19 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
160160
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
161161
golang.org/x/mod v0.26.0 h1:EGMPT//Ezu+ylkCijjPc+f4Aih7sZvaAr+O3EHBxvZg=
162162
golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ=
163-
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
164-
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
163+
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
164+
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
165165
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=
166166
golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU=
167-
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
168-
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
167+
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
168+
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
169169
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
170-
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
171-
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
172-
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
173-
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
174-
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
175-
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
170+
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
171+
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
172+
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
173+
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
174+
golang.org/x/tools v0.35.0 h1:mBffYraMEf7aa0sB+NuKnuCy8qI/9Bughn8dC2Gu5r0=
175+
golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw=
176176
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
177177
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
178178
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

internal/gen/ormbuilder/code/code.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ const tmplCRUD = `
208208
{{if not .RO}}
209209
const sql{{.Table|title}}Create{{.Name}} = @{{ queries .Dialect "INSERT INTO " (.Table|esc .Dialect) " (" .Cols ") " }}
210210
{{ queries .Dialect "VALUES (" .ColsLen ") " }}
211-
{{if .PK.Col}}{{end}}
212211
@
213212
214213
func (v *Repo{{.FileName|title}}) Create{{.Name}}(ctx context.Context, ms []*{{.Name}}, opts ...CreateOption) error {
@@ -228,7 +227,7 @@ func (v *Repo{{.FileName|title}}) Create{{.Name}}(ctx context.Context, ms []*{{.
228227
o(buf)
229228
}
230229
231-
{{if .PK.Col}}buf.WriteString(@ {{ queries .Dialect "RETURNING (" .PK.Col ")" }}@){{end}}
230+
{{if .PK.Col}}buf.WriteString(@{{ queries .Dialect " RETURNING (" .PK.Col ") " }}@){{end}}
232231
buf.WriteString(@;@)
233232
234233
return v.orm.Tag(v.wtag).Tx(ctx, "{{.Table|lower}}_create", func(tx orm.Tx) {
@@ -289,7 +288,7 @@ func (v *Repo{{.FileName|title}}) Delete{{.Name}}(ctx context.Context, pk []{{.P
289288
}
290289
{{end}}
291290
292-
const sql{{$G.Table|title}}Read{{$G.Name}}All = @{{ queries $G.Dialect "SELECT " $G.Cols }}
291+
const sql{{$G.Table|title}}Read{{$G.Name}}All = @{{ queries $G.Dialect "SELECT " (joinEscaped $G.PK.Col $G.Cols) }}
293292
{{ queries $G.Dialect " FROM " ($G.Table|esc $G.Dialect) }};
294293
@
295294
@@ -300,7 +299,7 @@ error) {
300299
q.SQL(sql{{$G.Table|title}}Read{{$G.Name}}All)
301300
q.Bind(func(bind orm.Scanner) error {
302301
m := {{$G.Name}}{}
303-
if e := bind.Scan({{ fields "&m." $G.Fields }}); e!= nil{
302+
if e := bind.Scan({{ fields "&m." (joinString $G.PK.Field $G.Fields) }}); e!= nil{
304303
return e
305304
}
306305
result = append(result, m)
@@ -314,7 +313,7 @@ error) {
314313
}
315314
316315
{{range $i, $x := .RCols}}
317-
const sql{{$G.Table|title}}Read{{$G.Name}}By{{$x.Field}} = @{{ queries $G.Dialect "SELECT " $G.Cols }}
316+
const sql{{$G.Table|title}}Read{{$G.Name}}By{{$x.Field}} = @{{ queries $G.Dialect "SELECT " (joinEscaped $G.PK.Col $G.Cols) }}
318317
{{ queries $G.Dialect " FROM " ($G.Table|esc $G.Dialect) }}
319318
{{ queries $G.Dialect " WHERE " $x.Col " = ANY(" 1 ")"}}
320319
@
@@ -341,7 +340,7 @@ ctx context.Context, args []{{$x.Type}}, opts ...ReadOption,
341340
q.SQL(buf.String(), args)
342341
q.Bind(func(bind orm.Scanner) error {
343342
m := {{$G.Name}}{}
344-
if e := bind.Scan({{ fields "&m." $G.Fields }}); e!= nil{
343+
if e := bind.Scan({{ fields "&m." (joinString $G.PK.Field $G.Fields) }}); e!= nil{
345344
return e
346345
}
347346
result = append(result, m)

internal/gen/ormbuilder/code/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func ConflictIgnore() CreateOption {
3737
3838
func ConflictUpdate(fields []string, ups []string) CreateOption {
3939
return func(b *strings.Builder) {
40-
fmt.Fprintf(b, @ ON CONFLICT("%s") DO NOTHING SET @, strings.Join(fields, @","@))
40+
fmt.Fprintf(b, @ ON CONFLICT("%s") DO UPDATE SET @, strings.Join(fields, @","@))
4141
4242
j := len(ups) - 1
4343
for i, up := range ups {

internal/gen/ormbuilder/tmpl/template.go

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ func Build(w io.Writer, value string, model any) error {
2020
tmpl, err := template.
2121
New("0").
2222
Funcs(template.FuncMap{
23-
"esc": esc,
24-
"lower": strings.ToLower,
25-
"title": title,
26-
"queries": queries,
27-
"fields": fields,
28-
"pls": pls,
29-
"incr": incr,
23+
"esc": esc,
24+
"lower": strings.ToLower,
25+
"title": title,
26+
"queries": queries,
27+
"fields": fields,
28+
"pls": pls,
29+
"incr": incr,
30+
"joinEscaped": joinEscaped,
31+
"joinString": joinString,
3032
}).
3133
Parse(value)
3234

@@ -37,6 +39,38 @@ func Build(w io.Writer, value string, model any) error {
3739
return tmpl.Execute(w, model)
3840
}
3941

42+
func joinEscaped(args ...any) []Escaped {
43+
out := make([]Escaped, 0, len(args))
44+
45+
for _, arg := range args {
46+
switch v := arg.(type) {
47+
case Escaped:
48+
out = append(out, v)
49+
50+
case []Escaped:
51+
out = append(out, v...)
52+
}
53+
}
54+
55+
return out
56+
}
57+
58+
func joinString(args ...any) []string {
59+
out := make([]string, 0, len(args))
60+
61+
for _, arg := range args {
62+
switch v := arg.(type) {
63+
case string:
64+
out = append(out, v)
65+
66+
case []string:
67+
out = append(out, v...)
68+
}
69+
}
70+
71+
return out
72+
}
73+
4074
func esc(dialect string, arg string) string {
4175
return dialects.EscapeCol(dialects.Dialect(dialect), arg)
4276
}

orm/types.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,41 @@ func (jb *jsonb) Value() (driver.Value, error) {
6565

6666
func applyPGSqlCastTypes(args []any) {
6767
count := len(args)
68-
6968
for i := 0; i < count; i++ {
70-
if reflect.ValueOf(args[i]).Kind() == reflect.Slice {
71-
args[i] = pq.Array(args[i])
72-
continue
73-
}
7469

7570
switch args[i].(type) {
76-
case TypeScanValuer:
77-
// ---
7871
case []byte, string, *[]byte, *string:
79-
// ---
72+
continue
8073
case int, int8, int16, int32, int64, *int, *int8, *int16, *int32, *int64:
81-
// ---
74+
continue
8275
case uint, uint8, uint16, uint32, uint64, *uint, *uint8, *uint16, *uint32, *uint64:
83-
// ---
76+
continue
8477
case float32, float64, *float32, *float64:
85-
// ---
78+
continue
8679
case bool, *bool:
87-
// ---
80+
continue
8881
case complex64, complex128, *complex64, *complex128:
89-
// ---
82+
continue
9083
case time.Time, *time.Time:
91-
// ---
84+
continue
85+
case TypeScanValuer:
86+
continue
9287
case TypeJSONb:
9388
args[i] = &jsonb{Any: args[i]}
9489
default:
9590
}
91+
92+
ref := reflect.ValueOf(args[i])
93+
94+
if ref.Kind() == reflect.Slice || ref.Kind() == reflect.Array {
95+
args[i] = pq.Array(args[i])
96+
continue
97+
}
98+
99+
if ref.Kind() == reflect.Ptr &&
100+
(ref.Elem().Kind() == reflect.Slice || ref.Elem().Kind() == reflect.Array) {
101+
args[i] = pq.Array(args[i])
102+
continue
103+
}
96104
}
97105
}

xdns/client.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package xdns
77

88
import (
9-
"fmt"
109
"time"
1110

1211
"github.com/miekg/dns"
@@ -71,13 +70,11 @@ func (v *Client) Exchange(question dns.Question) ([]dns.RR, error) {
7170
for _, address := range ns {
7271
resp, _, err := v.cli.Exchange(msg, address)
7372
if err != nil {
74-
errs = errors.Wrap(errs, errors.Wrapf(err, "name: %s, dns: %s", question.String(), address))
73+
errs = errors.Wrap(errs, errors.Wrapf(err, "name: %s, dns: %s", question.Name, address))
7574
continue
7675
}
7776

7877
if len(resp.Answer) == 0 {
79-
errs = errors.Wrap(errs,
80-
errors.Wrapf(fmt.Errorf("empty answer"), "name: %s, dns: %s", question.String(), address))
8178
continue
8279
}
8380

xdns/config.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55

66
package xdns
77

8-
import "time"
9-
108
type (
119
ConfigGroup struct {
1210
DNS Config `yaml:"dns"`
1311
}
1412
Config struct {
15-
Addr string `yaml:"addr"`
16-
Timeout time.Duration `yaml:"timeout"`
13+
Addr string `yaml:"addr"`
14+
QTypes []string `yaml:"qtypes,omitempty"`
1715
}
1816
)
1917

2018
func (v *ConfigGroup) Default() {
2119
v.DNS.Addr = "0.0.0.0:53"
22-
v.DNS.Timeout = 5 * time.Second
20+
v.DNS.QTypes = []string{"A"}
2321
}

0 commit comments

Comments
 (0)