Skip to content

Commit

Permalink
feat(template): transfer []byte type in load function
Browse files Browse the repository at this point in the history
when primary and foreign key is []byte, transfer []byte to string for
map key.
  • Loading branch information
xux1217 committed Oct 24, 2024
1 parent 540d7a2 commit e13bab1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
17 changes: 17 additions & 0 deletions boil/load_map_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package boil

import (
"github.com/volatiletech/null/v8"
)

func GenLoadMapKey(key interface{}) interface{} {
switch t := key.(type) {
case []byte:
return string(t)
case null.Bytes:
return string(t.Bytes)
default:
return key
}

}
12 changes: 6 additions & 6 deletions templates/main/07_relationship_to_one_eager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E
}
}

args := make(map[interface{}]struct{})
args := make(map[interface{}]interface{})
if singular {
if object.R == nil {
object.R = &{{$ltable.DownSingular}}R{}
}
{{if $usesPrimitives -}}
args[object.{{$col}}] = struct{}{}
args[boil.GenLoadMapKey(object.{{$col}})] = object.{{$col}}
{{else -}}
if !queries.IsNil(object.{{$col}}) {
args[object.{{$col}}] = struct{}{}
args[boil.GenLoadMapKey(object.{{$col}})] = object.{{$col}}
}
{{end}}
} else {
Expand All @@ -56,10 +56,10 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E
}

{{if $usesPrimitives -}}
args[obj.{{$col}}] = struct{}{}
args[boil.GenLoadMapKey(obj.{{$col}})] = obj.{{$col}}
{{else -}}
if !queries.IsNil(obj.{{$col}}) {
args[obj.{{$col}}] = struct{}{}
args[boil.GenLoadMapKey(obj.{{$col}})] = obj.{{$col}}
}
{{end}}
}
Expand All @@ -71,7 +71,7 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E

argsSlice := make([]interface{}, len(args))
i := 0
for arg := range args {
for _, arg := range args {
argsSlice[i] = arg
i++
}
Expand Down
8 changes: 4 additions & 4 deletions templates/main/08_relationship_one_to_one_eager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi
}
}

args := make(map[interface{}]struct{})
args := make(map[interface{}]interface{})
if singular {
if object.R == nil {
object.R = &{{$ltable.DownSingular}}R{}
}
args[object.{{$col}}] = struct{}{}
args[boil.GenLoadMapKey(object.{{$col}})] = object.{{$col}}
} else {
for _, obj := range slice {
if obj.R == nil {
obj.R = &{{$ltable.DownSingular}}R{}
}

args[obj.{{$col}}] = struct{}{}
args[boil.GenLoadMapKey(obj.{{$col}})] = obj.{{$col}}
}
}

Expand All @@ -59,7 +59,7 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi

argsSlice := make([]interface{}, len(args))
i := 0
for arg := range args {
for _, arg := range args {
argsSlice[i] = arg
i++
}
Expand Down
8 changes: 4 additions & 4 deletions templates/main/09_relationship_to_many_eager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi
}
}

args := make(map[interface{}]struct{})
args := make(map[interface{}]interface{})
if singular {
if object.R == nil {
object.R = &{{$ltable.DownSingular}}R{}
}
args[object.{{$col}}] = struct{}{}
args[boil.GenLoadMapKey(object.{{$col}})] = object.{{$col}}
} else {
for _, obj := range slice {
if obj.R == nil {
obj.R = &{{$ltable.DownSingular}}R{}
}
args[obj.{{$col}}] = struct{}{}
args[boil.GenLoadMapKey(obj.{{$col}})] = obj.{{$col}}
}
}

Expand All @@ -59,7 +59,7 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi

argsSlice := make([]interface{}, len(args))
i := 0
for arg := range args {
for _, arg := range args {
argsSlice[i] = arg
i++
}
Expand Down

0 comments on commit e13bab1

Please sign in to comment.