-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb9b214
commit 621bdbd
Showing
8 changed files
with
262 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/cristalhq/aconfig/aconfighcl | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/cristalhq/aconfig v0.10.0 | ||
github.com/hashicorp/hcl v1.0.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
github.com/cristalhq/aconfig v0.10.0 h1:5dyXjRkUBDuueSHFYxlLdxpZrnzGHwlffhL16NiT+XQ= | ||
github.com/cristalhq/aconfig v0.10.0/go.mod h1:0ZBp7dUf0F2Jr7YbLjw8OVlAD0eeV2bU3NwmVgeUReo= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= | ||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package aconfighcl | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/hashicorp/hcl" | ||
) | ||
|
||
// Decoder of HCL files for aconfig. | ||
type Decoder struct{} | ||
|
||
// New HCL decoder for aconfig. | ||
func New() *Decoder { return &Decoder{} } | ||
|
||
// DecodeFile implements aconfig.FileDecoder. | ||
func (d *Decoder) DecodeFile(filename string) (map[string]interface{}, error) { | ||
b, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
f, err := hcl.ParseBytes(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var raw map[string]interface{} | ||
if err := hcl.DecodeObject(&raw, f); err != nil { | ||
return nil, err | ||
} | ||
|
||
res := map[string]interface{}{} | ||
|
||
for key, value := range raw { | ||
flatten("", key, value, res) | ||
} | ||
return res, nil | ||
} | ||
|
||
// copied and adapted from aconfig/utils.go | ||
// | ||
func flatten(prefix, key string, curr interface{}, res map[string]interface{}) { | ||
switch curr := curr.(type) { | ||
case []map[string]interface{}: | ||
for _, v := range curr { | ||
flatten(prefix+key, "", v, res) | ||
} | ||
case []map[interface{}]interface{}: | ||
for k, v := range curr { | ||
flatten(prefix+key+".", fmt.Sprint(k), v, res) | ||
} | ||
|
||
case map[string]interface{}: | ||
for k, v := range curr { | ||
flatten(prefix+key+".", k, v, res) | ||
} | ||
|
||
case map[interface{}]interface{}: | ||
for k, v := range curr { | ||
if k, ok := k.(string); ok { | ||
flatten(prefix+key+".", k, v, res) | ||
} | ||
} | ||
case []interface{}: | ||
b := &strings.Builder{} | ||
for i, v := range curr { | ||
if i > 0 { | ||
b.WriteByte(',') | ||
} | ||
b.WriteString(fmt.Sprint(v)) | ||
} | ||
res[prefix+key] = b.String() | ||
case bool: | ||
res[prefix+key] = fmt.Sprint(curr) | ||
case string: | ||
res[prefix+key] = curr | ||
case float64: | ||
res[prefix+key] = fmt.Sprint(curr) | ||
case int, int8, int16, int32: | ||
res[prefix+key] = fmt.Sprint(curr) | ||
default: | ||
panic(fmt.Sprintf("%s::%s got %T %v", prefix, key, curr, curr)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package aconfighcl_test | ||
|
||
import ( | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/cristalhq/aconfig" | ||
"github.com/cristalhq/aconfig/aconfighcl" | ||
) | ||
|
||
func TestHCL(t *testing.T) { | ||
filepath := createTestFile(t) | ||
|
||
var cfg structConfig | ||
loader := aconfig.LoaderFor(&cfg, aconfig.Config{ | ||
SkipDefaults: true, | ||
SkipEnv: true, | ||
SkipFlags: true, | ||
FileDecoders: map[string]aconfig.FileDecoder{ | ||
".hcl": aconfighcl.New(), | ||
}, | ||
Files: []string{filepath}, | ||
}) | ||
|
||
if err := loader.Load(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
i := int32(42) | ||
j := int64(420) | ||
want := structConfig{ | ||
A: "b", | ||
C: 10, | ||
E: 123.456, | ||
B: []byte("abc"), | ||
I: &i, | ||
J: &j, | ||
Y: structY{ | ||
X: "y", | ||
Z: []string{"1", "2", "3"}, | ||
A: structD{ | ||
I: true, | ||
}, | ||
}, | ||
AA: structA{ | ||
X: "y", | ||
BB: structB{ | ||
CC: structC{ | ||
MM: "n", | ||
BB: []byte("boo"), | ||
}, | ||
DD: []string{"x", "y", "z"}, | ||
}, | ||
}, | ||
StructM: StructM{ | ||
M: "n", | ||
}, | ||
} | ||
|
||
if got := cfg; !reflect.DeepEqual(want, got) { | ||
t.Fatalf("want %v, got %v", want, got) | ||
} | ||
} | ||
|
||
func createTestFile(t *testing.T) string { | ||
t.Helper() | ||
dir := t.TempDir() | ||
t.Cleanup(func() { | ||
os.RemoveAll(dir) | ||
}) | ||
|
||
filepath := dir + "/testfile.hcl" | ||
|
||
f, err := os.Create(filepath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
_, err = f.WriteString(testfileContent) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return filepath | ||
} | ||
|
||
type structConfig struct { | ||
A string `hcl:"a"` | ||
C int `hcl:"c"` | ||
E float64 `hcl:"e"` | ||
B []byte `hcl:"b"` | ||
I *int32 `hcl:"i"` | ||
J *int64 `hcl:"j"` | ||
Y structY `hcl:"y"` | ||
|
||
AA structA `hcl:"A"` | ||
StructM | ||
} | ||
|
||
type structY struct { | ||
X string `hcl:"x"` | ||
Z []string `hcl:"z"` | ||
A structD `hcl:"A"` | ||
} | ||
|
||
type structA struct { | ||
X string `hcl:"x"` | ||
BB structB `hcl:"B"` | ||
} | ||
|
||
type structB struct { | ||
CC structC `hcl:"C"` | ||
DD []string `hcl:"D"` | ||
} | ||
|
||
type structC struct { | ||
MM string `hcl:"m"` | ||
BB []byte `hcl:"b"` | ||
} | ||
|
||
type structD struct { | ||
I bool `hcl:"i"` | ||
} | ||
|
||
type StructM struct { | ||
M string `hcl:"M"` | ||
} | ||
|
||
const testfileContent = ` | ||
"a" = "b" | ||
"c" = 10 | ||
"e" = 123.456 | ||
"b" = "abc" | ||
"i" = 42 | ||
"j" = 420 | ||
"y" = { | ||
"x" = "y" | ||
"z" = ["1", "2", "3"] | ||
"A" = { | ||
"i" = true | ||
} | ||
} | ||
"A" = { | ||
"x" = "y" | ||
"B" = { | ||
"C" = { | ||
"m" = "n" | ||
"b" = "boo" | ||
} | ||
"D" = ["x", "y", "z"] | ||
} | ||
} | ||
"M" = "n" | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters