forked from mpchadwick/dbanon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
101 lines (83 loc) · 1.68 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
96
97
98
99
100
101
package dbanon
import (
"errors"
"gopkg.in/yaml.v2"
"io/ioutil"
"strings"
)
type Config struct {
Tables []struct {
Name string `yaml:"name"`
Columns map[string]string `yaml:"columns"`
}
Eav []struct {
Name string `yaml:"name"`
Attributes map[string]string `yaml:"attributes"`
}
}
func NewConfig(requested string) (*Config, error) {
c := &Config{}
if requested == "" {
return c, errors.New("You must specify a config")
}
var source []byte
var err error
switch requested {
default:
source, err = ioutil.ReadFile(requested)
if err != nil {
return c, err
}
}
yaml.Unmarshal(source, &c)
return c, nil
}
func (c Config) String() ([]byte, error) {
return yaml.Marshal(c)
}
func (c Config) ProcessTable(t string) string {
for _, table := range c.Tables {
if table.Name == t {
return "table"
}
}
eavSuffixes := [5]string{"_datetime", "_decimal", "_int", "_text", "_varchar"}
for _, v := range eavSuffixes {
if strings.HasSuffix(t, v) {
parts := strings.Split(t, "_entity")
for _, e := range c.Eav {
if e.Name == parts[0] {
return "eav"
}
}
}
}
return ""
}
func (c Config) ProcessColumn(t string, col string) (bool, string) {
for _, table := range c.Tables {
if table.Name != t {
continue
}
for k, v := range table.Columns {
if k == col {
return true, v
}
}
}
return false, ""
}
func (c Config) ProcessEav(t string, attributeId string) (bool, string) {
parts := strings.Split(t, "_entity")
entity := parts[0]
for _, e := range c.Eav {
if e.Name == entity {
for k, v := range e.Attributes {
if k == attributeId {
return true, v
}
}
}
}
return false, ""
}