mirror of
https://github.com/hanzoai/ingress-parser.git
synced 2026-10-30 18:33:22 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers; omitzero is not applied, since it changes what the JSON says. Co-Authored-By: Claude Opus 5 <[email protected]>
406 lines
6.3 KiB
Go
406 lines
6.3 KiB
Go
package file
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDecode_TOML(t *testing.T) {
|
|
f, err := os.CreateTemp(t.TempDir(), "ingress-config-*.toml")
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
|
_ = f.Close()
|
|
})
|
|
|
|
_, err = f.WriteString(`
|
|
foo = "bar"
|
|
fii = "bir"
|
|
[yi]
|
|
`)
|
|
require.NoError(t, err)
|
|
|
|
element := &Yo{
|
|
Fuu: "test",
|
|
}
|
|
|
|
err = Decode(f.Name(), element)
|
|
require.NoError(t, err)
|
|
|
|
expected := &Yo{
|
|
Foo: "bar",
|
|
Fii: "bir",
|
|
Fuu: "test",
|
|
Yi: &Yi{
|
|
Foo: "foo",
|
|
Fii: "fii",
|
|
},
|
|
}
|
|
assert.Equal(t, expected, element)
|
|
}
|
|
|
|
func TestDecodeContent_TOML(t *testing.T) {
|
|
content := `
|
|
foo = "bar"
|
|
fii = "bir"
|
|
[yi]
|
|
`
|
|
|
|
element := &Yo{
|
|
Fuu: "test",
|
|
}
|
|
|
|
err := DecodeContent(content, ".toml", element)
|
|
require.NoError(t, err)
|
|
|
|
expected := &Yo{
|
|
Foo: "bar",
|
|
Fii: "bir",
|
|
Fuu: "test",
|
|
Yi: &Yi{
|
|
Foo: "foo",
|
|
Fii: "fii",
|
|
},
|
|
}
|
|
assert.Equal(t, expected, element)
|
|
}
|
|
|
|
func TestDecodeContent_TOML_rawSlice(t *testing.T) {
|
|
//nolint:dupword // false positive
|
|
content := `
|
|
[testData]
|
|
trustIP = [
|
|
"10.0.0.0/8",
|
|
"172.0.0.0/8",
|
|
"192.0.0.0/8"
|
|
]
|
|
koo = [1, 2, 3]
|
|
soo = [1, "a", 3]
|
|
boo = [1, 2.6, 3]
|
|
hoo = [true, false, false, true]
|
|
buckets = [42.01, 42.02]
|
|
|
|
[testData.Headers]
|
|
Foo = "Bar"
|
|
`
|
|
|
|
var element FooRaw
|
|
|
|
err := DecodeContent(content, ".toml", &element)
|
|
require.NoError(t, err)
|
|
|
|
expected := FooRaw{
|
|
TestData: map[string]any{
|
|
"Headers": map[string]any{"Foo": "Bar"},
|
|
"trustIP": []any{"10.0.0.0/8", "172.0.0.0/8", "192.0.0.0/8"},
|
|
"koo": []any{int64(1), int64(2), int64(3)},
|
|
"soo": []any{"1", "a", "3"},
|
|
"boo": []any{float64(1), 2.6, float64(3)},
|
|
"hoo": []any{true, false, false, true},
|
|
"buckets": []any{42.01, 42.02},
|
|
},
|
|
}
|
|
assert.EqualValues(t, expected, element)
|
|
}
|
|
|
|
func TestDecodeContent_TOML_rawValue(t *testing.T) {
|
|
content := `
|
|
name = "test"
|
|
[[meta.aaa]]
|
|
bbb = 1
|
|
`
|
|
|
|
type Foo struct {
|
|
Name string
|
|
Meta map[string]any
|
|
}
|
|
|
|
element := &Foo{}
|
|
|
|
err := DecodeContent(content, ".toml", element)
|
|
require.NoError(t, err)
|
|
|
|
expected := &Foo{
|
|
Name: "test",
|
|
Meta: map[string]any{"aaa": []any{map[string]any{"bbb": "1"}}},
|
|
}
|
|
assert.Equal(t, expected, element)
|
|
}
|
|
|
|
func TestDecode_YAML(t *testing.T) {
|
|
f, err := os.CreateTemp(t.TempDir(), "ingress-config-*.yaml")
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
|
_ = f.Close()
|
|
})
|
|
|
|
_, err = f.WriteString(`
|
|
foo: bar
|
|
fii: bir
|
|
yi: {}
|
|
`)
|
|
require.NoError(t, err)
|
|
|
|
element := &Yo{
|
|
Fuu: "test",
|
|
}
|
|
|
|
err = Decode(f.Name(), element)
|
|
require.NoError(t, err)
|
|
|
|
expected := &Yo{
|
|
Foo: "bar",
|
|
Fii: "bir",
|
|
Fuu: "test",
|
|
Yi: &Yi{
|
|
Foo: "foo",
|
|
Fii: "fii",
|
|
},
|
|
}
|
|
assert.Equal(t, expected, element)
|
|
}
|
|
|
|
func TestDecodeContent_YAML(t *testing.T) {
|
|
content := `
|
|
foo: bar
|
|
fii: bir
|
|
yi: {}
|
|
`
|
|
|
|
element := &Yo{
|
|
Fuu: "test",
|
|
}
|
|
|
|
err := DecodeContent(content, ".yaml", element)
|
|
require.NoError(t, err)
|
|
|
|
expected := &Yo{
|
|
Foo: "bar",
|
|
Fii: "bir",
|
|
Fuu: "test",
|
|
Yi: &Yi{
|
|
Foo: "foo",
|
|
Fii: "fii",
|
|
},
|
|
}
|
|
assert.Equal(t, expected, element)
|
|
}
|
|
|
|
func TestDecodeContent_YAML_rawSlice(t *testing.T) {
|
|
content := `
|
|
testData:
|
|
Headers:
|
|
Foo: Bar
|
|
trustIP:
|
|
- 10.0.0.0/8
|
|
- 172.0.0.0/8
|
|
- 192.0.0.0/8
|
|
koo:
|
|
- 1
|
|
- 2
|
|
- 3
|
|
soo:
|
|
- 1
|
|
- a
|
|
- 3
|
|
boo:
|
|
- 1
|
|
- 2.6
|
|
- 3
|
|
buckets:
|
|
- 42.01
|
|
- 42.02
|
|
hoo:
|
|
- true
|
|
- false
|
|
- false
|
|
- true
|
|
joo:
|
|
- name: a
|
|
value: 1
|
|
- name: c
|
|
value: 2
|
|
`
|
|
|
|
var element FooRaw
|
|
err := DecodeContent(content, ".yaml", &element)
|
|
require.NoError(t, err)
|
|
|
|
expected := FooRaw{
|
|
TestData: map[string]any{
|
|
"Headers": map[string]any{"Foo": "Bar"},
|
|
"trustIP": []any{"10.0.0.0/8", "172.0.0.0/8", "192.0.0.0/8"},
|
|
"koo": []any{int64(1), int64(2), int64(3)},
|
|
"soo": []any{"1", "a", "3"},
|
|
"boo": []any{float64(1), 2.6, float64(3)},
|
|
"hoo": []any{true, false, false, true},
|
|
"buckets": []any{42.01, 42.02},
|
|
"joo": []any{
|
|
map[string]any{"name": "a", "value": "1"},
|
|
map[string]any{"name": "c", "value": "2"},
|
|
},
|
|
},
|
|
}
|
|
assert.Equal(t, expected, element)
|
|
}
|
|
|
|
func TestDecodeContent_YAML_rawValue(t *testing.T) {
|
|
type Foo struct {
|
|
Name string
|
|
Meta map[string]any
|
|
}
|
|
|
|
testCases := []struct {
|
|
desc string
|
|
content string
|
|
expected any
|
|
}{
|
|
{
|
|
desc: "simple",
|
|
content: `
|
|
name: test
|
|
meta:
|
|
aaa:
|
|
- bbb: 1
|
|
`,
|
|
expected: &Foo{
|
|
Name: "test",
|
|
Meta: map[string]any{"aaa": []any{map[string]any{"bbb": "1"}}},
|
|
},
|
|
},
|
|
{
|
|
desc: "null",
|
|
content: `
|
|
name: test
|
|
meta:
|
|
aaa:
|
|
- bbb: 1
|
|
bbb: {"foo":"bar"}
|
|
null: {"toto":"tata"}
|
|
`,
|
|
expected: &Foo{
|
|
Name: "test",
|
|
Meta: map[string]any{
|
|
"aaa": []any{map[string]any{"bbb": "1"}},
|
|
"bbb": map[string]any{"foo": "bar"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range testCases {
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
element := &Foo{}
|
|
|
|
err := DecodeContent(test.content, ".yaml", element)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.expected, element)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDecodeContent_YAML_typedSliceRecursive(t *testing.T) {
|
|
content := `
|
|
foo:
|
|
- foo
|
|
- bar
|
|
bar:
|
|
foo:
|
|
- foo
|
|
- bar
|
|
baz:
|
|
foo:
|
|
- foo
|
|
- bar
|
|
boz:
|
|
foo:
|
|
- 42
|
|
- 42
|
|
`
|
|
|
|
element := map[string]any{}
|
|
|
|
err := DecodeContent(content, ".yaml", &element)
|
|
require.NoError(t, err)
|
|
|
|
expected := map[string]any{
|
|
"foo": []any{"foo", "bar"},
|
|
"bar": map[string]any{
|
|
"foo": []any{"foo", "bar"},
|
|
"baz": map[string]any{
|
|
"foo": []any{"foo", "bar"},
|
|
"boz": map[string]any{
|
|
"foo": []any{int64(42), int64(42)},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
assert.Equal(t, expected, element)
|
|
}
|
|
|
|
func TestDecodeContent_JSON(t *testing.T) {
|
|
content := `
|
|
{
|
|
"foo": "bar",
|
|
"fii": "bir",
|
|
"yi": {}
|
|
}
|
|
`
|
|
|
|
element := &Yo{
|
|
Fuu: "test",
|
|
}
|
|
|
|
err := DecodeContent(content, ".json", element)
|
|
require.NoError(t, err)
|
|
|
|
expected := &Yo{
|
|
Foo: "bar",
|
|
Fii: "bir",
|
|
Fuu: "test",
|
|
Yi: &Yi{
|
|
Foo: "foo",
|
|
Fii: "fii",
|
|
},
|
|
}
|
|
assert.Equal(t, expected, element)
|
|
}
|
|
|
|
func TestDecodeContent_JSON_rawValue(t *testing.T) {
|
|
content := `
|
|
{
|
|
"name": "test",
|
|
"meta": {
|
|
"aaa": [
|
|
{
|
|
"bbb": 1
|
|
}
|
|
]
|
|
}
|
|
}
|
|
`
|
|
|
|
type Foo struct {
|
|
Name string
|
|
Meta map[string]any
|
|
}
|
|
|
|
element := &Foo{}
|
|
|
|
err := DecodeContent(content, ".json", element)
|
|
require.NoError(t, err)
|
|
|
|
expected := &Foo{
|
|
Name: "test",
|
|
Meta: map[string]any{"aaa": []any{map[string]any{"bbb": "1"}}},
|
|
}
|
|
assert.Equal(t, expected, element)
|
|
}
|