-
Notifications
You must be signed in to change notification settings - Fork 6
/
list_select.go
106 lines (94 loc) · 2.43 KB
/
list_select.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package generator
import (
"fmt"
"github.com/goradd/goradd/codegen/generator"
"github.com/goradd/goradd/pkg/orm/db"
"github.com/goradd/goradd/pkg/orm/query"
)
func init() {
generator.RegisterControlGenerator(SelectList{}, "github.com/goradd/goradd/pkg/page/control/SelectList")
}
// This structure describes the SelectList to the connector dialog and code generator
type SelectList struct {
}
func (d SelectList) Imports() []string {
return []string{"strconv"}
}
func (d SelectList) SupportsColumn(ref interface{}) bool {
if col,ok := ref.(*db.Column); ok && col.ForeignKey != nil {
return true
}
return false
}
func (d SelectList) GenerateCreator(ref interface{}, desc *generator.ControlDescription) (s string) {
col := ref.(*db.Column)
s = fmt.Sprintf(
`%s.SelectListCreator{
ID: p.ID() + "-%s",
DataProvider: p,
ControlOptions: page.ControlOptions{
IsRequired: %#v,
DataConnector: %s{},
},
}`, desc.Package, desc.ControlID, !col.IsNullable, desc.Connector)
return
}
func (d SelectList) GenerateRefresh(ref interface{}, desc *generator.ControlDescription) string {
return `ctrl.SetValue(val)`
}
func (d SelectList) GenerateUpdate(ref interface{}, desc *generator.ControlDescription) string {
col := ref.(*db.Column)
var s string
if col.IsNullable {
s = `var val interface{}`
} else {
switch col.ColumnType {
case query.ColTypeInteger:
s = `var val int`
case query.ColTypeUnsigned:
s = `var val uint`
case query.ColTypeInteger64:
s = `var val int64`
case query.ColTypeUnsigned64:
s = `var val uint64`
default:
s = `var val string`
}
}
s += `
sv := ctrl.StringValue()
`
var s2 string
switch col.ColumnType {
case query.ColTypeInteger:
s2 = `val,_ = strconv.Atoi(sv)`
case query.ColTypeUnsigned:
s2 = `v2,_ := strconv.ParseUint(sv, 10, 0); val = uint(v2)`
case query.ColTypeInteger64:
s2 = `val,_ = strconv.ParseInt(sv, 10, 64)`
case query.ColTypeUnsigned64:
s2 = `val,_ = strconv.ParseUint(sv, 10, 64)`
default:
s2 = `val = sv`
}
if col.IsNullable {
s += fmt.Sprintf(
`
if sv == "" {
val = nil
} else {
%s
}`, s2)
} else {
s += s2
}
return s
}
func (d SelectList) GenerateProvider(ref interface{}, desc *generator.ControlDescription) string {
col := ref.(*db.Column)
if col.ForeignKey.IsType {
return fmt.Sprintf(`return model.All%sI()`, col.ForeignKey.GoTypePlural)
} else {
return fmt.Sprintf(`return model.Query%s(ctx).LoadI()`, col.ForeignKey.GoTypePlural)
}
}