-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
95 lines (76 loc) · 2.37 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
// Copyright 2019 Job Stoit. All rights reserved.
package main
import (
"log"
"regexp"
"strconv"
"strings"
yaml "gopkg.in/yaml.v2"
)
// this config struct is for mapping the object to the nessasary state
type config struct {
Driver string `yaml:"driver"`
Pkg string `yaml:"pkg"`
Tables map[string]map[string]string `yaml:"tables,flow"`
Enums map[string][]string `yaml:"enums,flow"`
Seeders map[string][]string `yaml:"seeds,flow"`
}
// ReadConfig reads the data of the given yaml file into a model
func ReadConfig(data []byte) (m Model) {
c := config{}
catch(yaml.Unmarshal(data, &c), `Yaml configuration is unreadable`)
removeSpacesReg := regexp.MustCompile(`\s`)
defaultReg := regexp.MustCompile(`,\s?default\((\w+)\)`)
constrainReg := regexp.MustCompile(`,\s?constraint\(([\w,\s;]+)\)`)
m.Pkg = c.Pkg
m.Driver = c.Driver
for i, tab := range c.Tables {
t := Table(i)
for e, context := range tab {
c := Column{}
c.Table = &t
c.Name = e
if match := defaultReg.FindStringSubmatch(context); len(match) == 2 {
c.Default = match[1]
}
if match := constrainReg.FindStringSubmatch(context); len(match) == 2 {
c.Constraints = strings.Split(match[1], `;`)
}
context = removeSpacesReg.ReplaceAllString(context, ``)
c.rawType, c.Size = getRawType(context)
c.Primary = strings.Contains(context, `,primary`)
c.Nullable = strings.Contains(context, `,nullable`)
c.Unique = strings.Contains(context, `,unique`)
m.Types = append(m.Types, &c)
}
m.Tables = append(m.Tables, &t)
}
for i, enu := range c.Enums {
t := Table(i)
m.Tables = append(m.Tables, &t)
m.Types = append(m.Types, &Enum{&t, enu})
}
for _, typ := range m.Types {
if col, ok := typ.(*Column); ok {
m.GetType(col)
}
}
return
}
var typeDataReg = regexp.MustCompile(`^[\w\_\.]+(\(\d{0,3}\))?`)
func getRawType(context string) (rawType string, size int) {
typeData := typeDataReg.FindStringSubmatch(context)
if len(typeData) == 0 {
log.Fatal(`Type not defined: ` + context)
}
rawType = typeData[0]
if len(typeData) >= 2 && typeData[1] != `` {
ssize := strings.Trim(typeData[1], `(`)
ssize = strings.Trim(ssize, `)`)
isize, err := strconv.Atoi(ssize)
catch(err, `Datatype in an invalid format: %s\n`, rawType)
size = isize
rawType = strings.Trim(rawType, typeData[1])
}
return
}