-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Structured data source driver (#5165)
* proto: add structured data source types Signed-off-by: Adolfo García Veytia (puerco) <[email protected]> * make gen Signed-off-by: Adolfo García Veytia (puerco) <[email protected]> * Add structured data source This commit adds the first version of the structured data source. Signed-off-by: Adolfo García Veytia (puerco) <[email protected]> * go mod tidy Signed-off-by: Adolfo García Veytia (puerco) <[email protected]> * Wire struct data source to DS machinery This commit connects the new structured data source to the data source machinery. Signed-off-by: Adolfo García Veytia (puerco) <[email protected]> * Add structured DS tests Signed-off-by: Adolfo García Veytia (puerco) <[email protected]> --------- Signed-off-by: Adolfo García Veytia (puerco) <[email protected]>
- Loading branch information
Showing
17 changed files
with
2,344 additions
and
1,290 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,77 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package structured | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/pelletier/go-toml/v2" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var ( | ||
// ErrNoInput triggers if a decoder is called without input | ||
ErrNoInput = errors.New("unable to decode, no input defined") | ||
) | ||
|
||
// This file contains various decoders that can be used to decode structured data | ||
|
||
// jsonDecoder decodes JSON data | ||
type jsonDecoder struct{} | ||
|
||
func (*jsonDecoder) Parse(r io.Reader) (any, error) { | ||
if r == nil { | ||
return nil, ErrNoInput | ||
} | ||
var res any | ||
dec := json.NewDecoder(r) | ||
if err := dec.Decode(&res); err != nil { | ||
return nil, fmt.Errorf("decoding json data: %w", err) | ||
} | ||
return res, nil | ||
} | ||
|
||
func (*jsonDecoder) Extensions() []string { | ||
return []string{"json"} | ||
} | ||
|
||
// yamlDecoder opens yaml | ||
type yamlDecoder struct{} | ||
|
||
func (*yamlDecoder) Parse(r io.Reader) (any, error) { | ||
if r == nil { | ||
return nil, ErrNoInput | ||
} | ||
var res any | ||
dec := yaml.NewDecoder(r) | ||
if err := dec.Decode(&res); err != nil { | ||
return nil, fmt.Errorf("decoding yaml data: %w", err) | ||
} | ||
return res, nil | ||
} | ||
|
||
func (*yamlDecoder) Extensions() []string { | ||
return []string{"yaml", "yml"} | ||
} | ||
|
||
type tomlDecoder struct{} | ||
|
||
func (*tomlDecoder) Parse(r io.Reader) (any, error) { | ||
if r == nil { | ||
return nil, ErrNoInput | ||
} | ||
var res any | ||
dec := toml.NewDecoder(r) | ||
if err := dec.Decode(&res); err != nil { | ||
return nil, fmt.Errorf("decoding toml data: %w", err) | ||
} | ||
return res, nil | ||
} | ||
|
||
func (*tomlDecoder) Extensions() []string { | ||
return []string{"toml"} | ||
} |
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,110 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package structured | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestJsonDecoder(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range []struct { | ||
name string | ||
data []byte | ||
mustErr bool | ||
expect any | ||
}{ | ||
{name: "normal", data: []byte(`{"a":1, "b":"abc"}`), mustErr: false}, | ||
{name: "invalid_json", data: []byte(`a 1`), mustErr: true}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
var b bytes.Buffer | ||
_, err := b.Write(tc.data) | ||
require.NoError(t, err) | ||
|
||
dec := jsonDecoder{} | ||
res, err := dec.Parse(&b) | ||
if tc.mustErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.NotNil(t, res) | ||
}) | ||
dec := jsonDecoder{} | ||
_, err := dec.Parse(nil) | ||
require.Error(t, err) | ||
|
||
} | ||
} | ||
|
||
func TestYamlDecoder(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range []struct { | ||
name string | ||
data []byte | ||
mustErr bool | ||
expect any | ||
}{ | ||
{name: "normal", data: []byte("---\na: 1\nb:\n - \"Hey\"\n - \"Bye\"\n"), mustErr: false}, | ||
{name: "invalid_yaml", data: []byte(" a 1\na: 2\n"), mustErr: true}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
var b bytes.Buffer | ||
_, err := b.Write(tc.data) | ||
require.NoError(t, err) | ||
|
||
dec := yamlDecoder{} | ||
res, err := dec.Parse(&b) | ||
if tc.mustErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.NotNil(t, res) | ||
}) | ||
dec := yamlDecoder{} | ||
_, err := dec.Parse(nil) | ||
require.Error(t, err) | ||
|
||
} | ||
} | ||
|
||
func TestTomlDecoder(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range []struct { | ||
name string | ||
data []byte | ||
mustErr bool | ||
expect any | ||
}{ | ||
{name: "normal", data: []byte("title = \"TOML Example\"\n\n[owner]\nname = \"Tom Preston-Werner\""), mustErr: false}, | ||
{name: "invalid_toml", data: []byte(" a 1\na: 2\n"), mustErr: true}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
var b bytes.Buffer | ||
_, err := b.Write(tc.data) | ||
require.NoError(t, err) | ||
|
||
dec := tomlDecoder{} | ||
res, err := dec.Parse(&b) | ||
if tc.mustErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.NotNil(t, res) | ||
}) | ||
dec := tomlDecoder{} | ||
_, err := dec.Parse(nil) | ||
require.Error(t, err) | ||
|
||
} | ||
} |
Oops, something went wrong.