Skip to content

Commit 528f508

Browse files
committed
Append Dict to Config.Dict
1 parent 38866ac commit 528f508

File tree

5 files changed

+88
-41
lines changed

5 files changed

+88
-41
lines changed

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/goccy/go-yaml"
15+
"github.com/k1LoW/tbls/dict"
1516
"github.com/k1LoW/tbls/schema"
1617
"github.com/minio/minio/pkg/wildcard"
1718
"github.com/pkg/errors"
@@ -38,6 +39,7 @@ type Config struct {
3839
LintExclude []string `yaml:"lintExclude"`
3940
Relations []AdditionalRelation `yaml:"relations"`
4041
Comments []AdditionalComment `yaml:"comments"`
42+
Dict dict.Dict `yaml:"dict"`
4143
}
4244

4345
// Format is document format setting

config/config_test.go

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ func TestLoadDefault(t *testing.T) {
2020
}
2121
want := ""
2222
if config.DSN != want {
23-
t.Errorf("actual %v\nwant %v", config.DSN, want)
23+
t.Errorf("got %v\nwant %v", config.DSN, want)
2424
}
2525
want2 := "dbdoc"
2626
if config.DocPath != want2 {
27-
t.Errorf("actual %v\nwant %v", config.DocPath, want2)
27+
t.Errorf("got %v\nwant %v", config.DocPath, want2)
2828
}
2929
want3 := "png"
3030
if config.ER.Format != want3 {
31-
t.Errorf("actual %v\nwant %v", config.ER.Format, want3)
31+
t.Errorf("got %v\nwant %v", config.ER.Format, want3)
3232
}
3333
want4 := 1
3434
if *config.ER.Distance != want4 {
35-
t.Errorf("actual %v\nwant %v", config.ER.Distance, want4)
35+
t.Errorf("got %v\nwant %v", config.ER.Distance, want4)
3636
}
3737
}
3838

3939
func TestLoadConfigFile(t *testing.T) {
4040
_ = os.Setenv("TBLS_TEST_PG_PASS", "pgpass")
4141
_ = os.Setenv("TBLS_TEST_PG_DOC_PATH", "sample/pg")
42-
configFilepath := filepath.Join(testdataDir(), "env_testdb_tbls.yml")
42+
configFilepath := filepath.Join(testdataDir(), "config_test_tbls_2.yml")
4343
config, err := NewConfig()
4444
if err != nil {
4545
t.Fatal(err)
@@ -48,19 +48,23 @@ func TestLoadConfigFile(t *testing.T) {
4848
if err != nil {
4949
t.Fatal(err)
5050
}
51-
expected := "pg://root:pgpass@localhost:55432/testdb?sslmode=disable"
52-
if config.DSN != expected {
53-
t.Errorf("actual %v\nwant %v", config.DSN, expected)
51+
52+
if want := "pg://root:pgpass@localhost:55432/testdb?sslmode=disable"; config.DSN != want {
53+
t.Errorf("got %v\nwant %v", config.DSN, want)
54+
}
55+
56+
if want := "sample/pg"; config.DocPath != want {
57+
t.Errorf("got %v\nwant %v", config.DocPath, want)
5458
}
55-
expected2 := "sample/pg"
56-
if config.DocPath != expected2 {
57-
t.Errorf("actual %v\nwant %v", config.DocPath, expected2)
59+
60+
if want := "INDEX"; config.Dict.Lookup("Indexes") != want {
61+
t.Errorf("got %v\nwant %v", config.Dict.Lookup("Indexes"), want)
5862
}
5963
}
6064

6165
var tests = []struct {
62-
value string
63-
expected string
66+
value string
67+
want string
6468
}{
6569
{"${TBLS_ONE}/${TBLS_TWO}", "one/two"},
6670
{"${TBLS_ONE}/${TBLS_TWO}/${TBLS_NONE}", "one/two/"},
@@ -72,12 +76,12 @@ func TestParseWithEnvirion(t *testing.T) {
7276
_ = os.Setenv("TBLS_ONE", "one")
7377
_ = os.Setenv("TBLS_TWO", "two")
7478
for _, tt := range tests {
75-
actual, err := parseWithEnviron(tt.value)
79+
got, err := parseWithEnviron(tt.value)
7680
if err != nil {
7781
t.Fatal(err)
7882
}
79-
if actual != tt.expected {
80-
t.Errorf("actual %v\nwant %v", actual, tt.expected)
83+
if got != tt.want {
84+
t.Errorf("got %v\nwant %v", got, tt.want)
8185
}
8286
}
8387
}
@@ -132,17 +136,17 @@ func TestMergeAditionalData(t *testing.T) {
132136
if err != nil {
133137
t.Error(err)
134138
}
135-
expected := 1
136-
actual := len(s.Relations)
137-
if actual != expected {
138-
t.Errorf("actual %v\nwant %v", actual, expected)
139+
want := 1
140+
got := len(s.Relations)
141+
if got != want {
142+
t.Errorf("got %v\nwant %v", got, want)
139143
}
140144
posts, _ := s.FindTableByName("posts")
141145
title, _ := posts.FindColumnByName("title")
142-
expected2 := "post title"
143-
actual2 := title.Comment
144-
if actual2 != expected2 {
145-
t.Errorf("actual %v\nwant %v", actual2, expected2)
146+
want2 := "post title"
147+
got2 := title.Comment
148+
if got2 != want2 {
149+
t.Errorf("got %v\nwant %v", got2, want2)
146150
}
147151
}
148152

@@ -227,10 +231,10 @@ func TestFilterTables(t *testing.T) {
227231
if err != nil {
228232
t.Error(err)
229233
}
230-
expected := 2
231-
actual := len(s.Tables)
232-
if actual != expected {
233-
t.Errorf("actual %v\nwant %v", actual, expected)
234+
want := 2
235+
got := len(s.Tables)
236+
if got != want {
237+
t.Errorf("got %v\nwant %v", got, want)
234238
}
235239
}
236240

@@ -297,22 +301,22 @@ func TestModifySchema(t *testing.T) {
297301
if err != nil {
298302
t.Error(err)
299303
}
300-
expected := 1
301-
actual := len(s.Relations)
302-
if actual != expected {
303-
t.Errorf("actual %v\nwant %v", actual, expected)
304+
want := 1
305+
got := len(s.Relations)
306+
if got != want {
307+
t.Errorf("got %v\nwant %v", got, want)
304308
}
305309
posts, _ := s.FindTableByName("posts")
306310
title, _ := posts.FindColumnByName("title")
307-
expected2 := "post title"
308-
actual2 := title.Comment
309-
if actual2 != expected2 {
310-
t.Errorf("actual %v\nwant %v", actual2, expected2)
311+
want2 := "post title"
312+
got2 := title.Comment
313+
if got2 != want2 {
314+
t.Errorf("got %v\nwant %v", got2, want2)
311315
}
312-
expected3 := 2
313-
actual3 := len(s.Tables)
314-
if actual3 != expected3 {
315-
t.Errorf("actual %v\nwant %v", actual, expected)
316+
want3 := 2
317+
got3 := len(s.Tables)
318+
if got3 != want3 {
319+
t.Errorf("got %v\nwant %v", got, want)
316320
}
317321
}
318322

dict/dict.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package dict
22

3-
import "sync"
3+
import (
4+
"encoding/json"
5+
"sync"
6+
7+
"github.com/goccy/go-yaml"
8+
)
49

510
type Dict struct {
611
s sync.Map
@@ -50,3 +55,35 @@ func (d *Dict) Dump() map[string]string {
5055
})
5156
return dpd
5257
}
58+
59+
func (d *Dict) MarchalJSON() ([]byte, error) {
60+
return json.Marshal(d.Dump())
61+
}
62+
63+
func (d *Dict) UnmarshalJSON(data []byte) error {
64+
m := map[string]string{}
65+
err := json.Unmarshal(data, &m)
66+
if err != nil {
67+
return err
68+
}
69+
for k, v := range m {
70+
d.s.Store(k, v)
71+
}
72+
return nil
73+
}
74+
75+
func (d *Dict) MarchalYAML() ([]byte, error) {
76+
return yaml.Marshal(d.Dump())
77+
}
78+
79+
func (d *Dict) UnmarshalYAML(data []byte) error {
80+
m := map[string]string{}
81+
err := yaml.Unmarshal(data, &m)
82+
if err != nil {
83+
return err
84+
}
85+
for k, v := range m {
86+
d.s.Store(k, v)
87+
}
88+
return nil
89+
}

testdata/config_test.yml.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ comments:
4646
columnComments:
4747
b: column comment required.
4848
b2: column comment required.
49+
dict: {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
---
22
dsn: pg://root:${TBLS_TEST_PG_PASS}@localhost:55432/testdb?sslmode=disable
33
docPath: ${TBLS_TEST_PG_DOC_PATH}
4+
dict:
5+
Indexes: INDEX
6+
Constraints: FK, UNIQUE, etc

0 commit comments

Comments
 (0)