/
embedded_code.go
159 lines (132 loc) · 3.39 KB
/
embedded_code.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package ycode
import (
"vals"
"github.com/spf13/viper"
"bytes"
"io/ioutil"
"strings"
"fmt"
"reflect"
"log"
)
// Source structs provide access to the api for rendering a mapping source
// to a custom structure.
type Source struct {
val *vals.Value
}
// A lookup of Sql instances by their name.
type CodeLookup map[string]*Sql
// Pairs up a human name with the sql code. The name can be space or '_' separated.
type Sql struct {
Name string
Sql string
Vars []string
}
func NewSource(src *vals.Value) *Source {
return &Source{
val: src,
}
}
func (code CodeLookup) LoadSqlFields(scriptStruct interface{}) {
v := reflect.ValueOf(scriptStruct).Elem()
n := v.NumField()
for i := 0; i < n; i++ {
name := v.Type().Field(i).Name
if val, ok := code[name]; ok {
v.FieldByName(name).Set(reflect.ValueOf(val))
}
}
}
// Helper function for logging errors as fatal should they exist.
func maybePanic(err error) {
if err != nil {
log.Fatal(err)
}
}
// Brute force capitalizing words where the first letter is capitalized
// and the rest of the word is lower-cased.
func capitalized(s string) string {
a := strings.ToUpper(s[0:1])
b := strings.ToLower(s[1:])
return fmt.Sprintf("%s%s", a, b)
}
// Breaks apart name by either _ or space.
func split(name string) []string {
parts := strings.FieldsFunc(name, func(c rune) bool {
switch c {
case ' ', '_':
return true
default:
return false
}
})
return parts
}
// Splits the word over ' ' and '_'; capitalizes each word then joins them.
func capitalizeWords(word string) string {
parts := split(word)
for i,e := range parts {
parts[i] = capitalized(e)
}
return strings.Join(parts, "")
}
// Provides the name of the Sql instance in Pascal format.
func (s *Sql) PascalName() string {
return capitalizeWords(s.Name)
}
// Turns the given Value as a CodeLookup map to ease mapping it to a struct.
func (val *Source) YamlToSqlScripts() CodeLookup {
n := val.Len()
scripts := make(map[string]*Sql, n)
for i := 0; i < n; i++ {
v := val.In(i)
name := v.At("name").AsString()
sql := v.At("sql").AsString()
vars := v.At("vars").AsStringSlice()
s := &Sql{
Name: name,
Sql: sql,
Vars: vars,
}
scripts[s.PascalName()] = s
}
return scripts
}
// Fills the destination (should be a pointer) with the Sql code found in the
// yaml file mapping the name properties to the Pascal cased struct fields.
func (v *Source) FromYaml(dest interface{}) error {
v.YamlToSqlScripts().LoadSqlFields(dest)
return v.Error()
}
// Loads the given yaml file as a Value.
func LoadYaml(file string) *Source {
y, err := ioutil.ReadFile(file)
maybePanic(err)
viper.SetConfigType("yaml")
viper.ReadConfig(bytes.NewBuffer(y))
conf := viper.AllSettings()
return NewSource(vals.New(conf))
}
// Delegates to underlying value but return the Source.
func (s *Source) At(key string) *Source {
return NewSource(s.val.At(key))
}
// Delegates to underlying value but return the Source.
func (s *Source) AsString() string {
return s.val.AsString()
}
func (s *Source) AsStringSlice() []string {
return s.val.AsStringSlice()
}
// Delegates to underlying value but return the Source.
func (s *Source) In(n int) *Source {
return NewSource(s.val.In(n))
}
// Delegates to underlying value but return the Source.
func (s *Source) Error() error {
return s.val.Error()
}
// Delegates to underlying value but return the Source.
func (s *Source) Len() int {
return s.val.Len()
}