/
gen_store_upperio.go
61 lines (51 loc) · 1.44 KB
/
gen_store_upperio.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
package main
import (
"fmt"
"github.com/gourd/goparser"
"github.com/gourd/gourd/compile"
"github.com/gourd/gourd/templates"
)
func init() {
t, err := templates.Asset("store/upperio.tpl")
if err != nil {
panic(err)
}
tpls.Append("gen store:upperio", string(t))
tpls.AddDeps("gen store:upperio", "gen:general")
tpls.AddPrep("gen store:upperio", func(in interface{}) (interface{}, error) {
var data map[string]interface{}
var f interface{}
var id *goparser.FieldSpec
var ok bool
if data, ok = in.(compile.Context); !ok {
return in, fmt.Errorf("Unable to prepare. Incorrect data provided: %#v", in)
}
if f, ok = data["Id"]; !ok {
return in, fmt.Errorf("Unable to prepare. No Id found in given data: %#v",
data)
}
if id, ok = f.(*goparser.FieldSpec); !ok {
return in, fmt.Errorf("Unable to prepare. Wrong Id type: %#v", f)
}
// override the field spec
data["Id"] = &UpperFieldSpec{
id,
}
return data, nil
})
}
// UpperFieldSpec represents information of an upper field.
// Includes its original field spec and database type suggestion
type UpperFieldSpec struct {
*goparser.FieldSpec
}
// IsString test if the field is string
func (s UpperFieldSpec) IsString() bool {
return s.Type == "string"
}
// IsInt test if the field is int
func (s *UpperFieldSpec) IsInt() bool {
return s.Type == "int" || s.Type == "uint" ||
s.Type == "int32" || s.Type == "uint32" ||
s.Type == "int64" || s.Type == "uint64"
}