-
Notifications
You must be signed in to change notification settings - Fork 4
/
clone.go
38 lines (34 loc) · 1.01 KB
/
clone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package gomodel
import (
"fmt"
"strings"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/lib/types"
"projectforge.dev/projectforge/app/project/export/golang"
"projectforge.dev/projectforge/app/project/export/model"
"projectforge.dev/projectforge/app/util"
)
func modelClone(m *model.Model) *golang.Block {
ret := golang.NewBlock("Clone", "func")
ret.W("func (%s *%s) Clone() *%s {", m.FirstLetter(), m.Proper(), m.Proper())
calls := lo.Map(m.Columns, func(col *model.Column, _ int) string {
decl := col.Proper()
switch col.Type.Key() {
case types.KeyMap, types.KeyValueMap, types.KeyReference:
decl += ".Clone()"
}
return fmt.Sprintf("%s.%s,", m.FirstLetter(), decl)
})
lines := util.JoinLines(calls, " ", 120)
if len(lines) == 1 && len(lines[0]) < 100 {
ret.W("\treturn &%s{%s}", m.Proper(), strings.TrimSuffix(lines[0], ","))
} else {
ret.W("\treturn &%s{", m.Proper())
lo.ForEach(lines, func(l string, _ int) {
ret.W("\t\t%s", l)
})
ret.W("\t}")
}
ret.W("}")
return ret
}