forked from zph/moresql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
95 lines (85 loc) · 2.12 KB
/
config.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
package moresql
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"regexp"
"strings"
log "github.com/Sirupsen/logrus"
)
func LoadConfigString(s string) (Config, error) {
config := Config{}
var configDelayed ConfigDelayed
err := json.Unmarshal([]byte(s), &configDelayed)
if err != nil {
log.Fatalln(err)
}
for k, v := range configDelayed {
db := DB{}
collections := Collections{}
db.Collections = collections
for k, v := range v.Collections {
coll := Collection{Name: v.Name, PgTable: v.PgTable}
var fields Fields
fields, err = JsonToFields(string(v.Fields))
if err != nil {
log.Warnf("JSON Config decoding error: ", err)
return nil, fmt.Errorf("Unable to decode %s", err)
}
coll.Fields = fields
db.Collections[k] = coll
}
config[k] = db
}
return config, nil
}
func LoadConfig(path string) Config {
b, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
config, err := LoadConfigString(string(b))
if err != nil {
panic(err)
}
return config
}
func mongoToPostgresTypeConversion(mongoType string) string {
// Coerce "id" bsonId types into text since Postgres doesn't have type for BSONID
switch strings.ToLower(mongoType) {
case "id":
return "text"
}
return mongoType
}
func normalizeDotNotationToPostgresNaming(key string) string {
re := regexp.MustCompile("\\.")
return re.ReplaceAllString(key, "_")
}
func JsonToFields(s string) (Fields, error) {
var init FieldsWrapper
var err error
result := Fields{}
err = json.Unmarshal([]byte(s), &init)
for k, v := range init {
field := Field{}
str := ""
if err := json.Unmarshal(v, &field); err == nil {
result[k] = field
} else if err := json.Unmarshal(v, &str); err == nil {
// Convert shorthand to longhand Field
f := Field{
Mongo{k, str},
Postgres{normalizeDotNotationToPostgresNaming(k), mongoToPostgresTypeConversion(str)},
}
result[k] = f
} else {
errLong := json.Unmarshal(v, &field)
errShort := json.Unmarshal(v, &str)
err = errors.New(fmt.Sprintf("Could not decode Field. Long decoding %+v. Short decoding %+v", errLong, errShort))
return nil, err
}
}
return result, err
}