Compare commits

...

4 Commits

Author SHA1 Message Date
Bobo
d549d305ae feat: entgo. 2025-02-08 00:00:50 +08:00
Bobo
23dcad60a3 feat: time utils. 2025-02-07 22:42:19 +08:00
Bobo
fa7ae4f876 feat: time trans. 2025-01-11 13:22:23 +08:00
tx7do
9f6a4eba80 feat: ent support update json field. 2024-12-14 14:41:44 +08:00
11 changed files with 38 additions and 14 deletions

View File

@@ -588,7 +588,7 @@ func filterJsonbField(s *sql.Selector, jsonbField, field string) string {
p := sql.P() p := sql.P()
switch s.Builder.Dialect() { switch s.Builder.Dialect() {
case dialect.Postgres: case dialect.Postgres:
p.Ident(s.C(field)).WriteString(" -> ").WriteString(jsonbField) p.Ident(s.C(field)).WriteString(" ->> ").WriteString("'" + jsonbField + "'")
//b.Arg(strings.ToLower(value)) //b.Arg(strings.ToLower(value))
break break

View File

@@ -34,7 +34,7 @@ func BuildSetNullUpdater(fields []string) func(u *sql.UpdateBuilder) {
} }
// ExtractJsonFieldKeyValues 提取json字段的键值对 // ExtractJsonFieldKeyValues 提取json字段的键值对
func ExtractJsonFieldKeyValues(msg proto.Message, paths []string) []string { func ExtractJsonFieldKeyValues(msg proto.Message, paths []string, needToSnakeCase bool) []string {
var keyValues []string var keyValues []string
rft := msg.ProtoReflect() rft := msg.ProtoReflect()
for _, path := range paths { for _, path := range paths {
@@ -46,7 +46,14 @@ func ExtractJsonFieldKeyValues(msg proto.Message, paths []string) []string {
continue continue
} }
keyValues = append(keyValues, fmt.Sprintf("'%s'", stringcase.ToSnakeCase(path))) var k string
if needToSnakeCase {
k = stringcase.ToSnakeCase(path)
} else {
k = path
}
keyValues = append(keyValues, fmt.Sprintf("'%s'", k))
v := rft.Get(fd) v := rft.Get(fd)
switch v.Interface().(type) { switch v.Interface().(type) {
@@ -77,8 +84,8 @@ func SetJsonNullFieldUpdateBuilder(fieldName string, msg proto.Message, paths []
} }
// SetJsonFieldValueUpdateBuilder 设置json字段的值 // SetJsonFieldValueUpdateBuilder 设置json字段的值
func SetJsonFieldValueUpdateBuilder(fieldName string, msg proto.Message, paths []string) func(u *sql.UpdateBuilder) { func SetJsonFieldValueUpdateBuilder(fieldName string, msg proto.Message, paths []string, needToSnakeCase bool) func(u *sql.UpdateBuilder) {
keyValues := ExtractJsonFieldKeyValues(msg, paths) keyValues := ExtractJsonFieldKeyValues(msg, paths, needToSnakeCase)
if len(keyValues) == 0 { if len(keyValues) == 0 {
return nil return nil
} }

View File

@@ -3,7 +3,7 @@ git tag v1.1.13
git tag bank_card/v1.1.3 git tag bank_card/v1.1.3
git tag geoip/v1.1.3 git tag geoip/v1.1.3
git tag entgo/v1.1.17 git tag entgo/v1.1.19
git tag gorm/v1.1.3 git tag gorm/v1.1.3
git push origin --tags git push origin --tags

View File

@@ -1,4 +1,4 @@
package util package timeutil
import "time" import "time"

View File

@@ -1,4 +1,4 @@
package util package timeutil
import ( import (
"math" "math"

View File

@@ -1,4 +1,4 @@
package util package timeutil
import ( import (
"testing" "testing"

View File

@@ -1,4 +1,4 @@
package util package timeutil
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package util package timeutil
import "time" import "time"

View File

@@ -1,4 +1,4 @@
package util package timeutil
import ( import (
"fmt" "fmt"

View File

@@ -1,8 +1,9 @@
package util package timeutil
import ( import (
"time" "time"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/timestamppb"
"github.com/tx7do/go-utils/trans" "github.com/tx7do/go-utils/trans"
@@ -132,3 +133,19 @@ func TimeToTimestamppb(tm *time.Time) *timestamppb.Timestamp {
} }
return nil return nil
} }
func FloatToDurationpb(duration *float64, timePrecision time.Duration) *durationpb.Duration {
if duration == nil {
return nil
}
return durationpb.New(time.Duration(*duration) * timePrecision)
}
func DurationpbToFloat(duration *durationpb.Duration, timePrecision time.Duration) *float64 {
if duration == nil {
return nil
}
seconds := duration.AsDuration().Seconds()
secondsWithPrecision := seconds / timePrecision.Seconds()
return &secondsWithPrecision
}

View File

@@ -1,4 +1,4 @@
package util package timeutil
import ( import (
"fmt" "fmt"