diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2024-01-25 09:00:15 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-25 14:00:15 +0000 |
| commit | 0dfabf140720077e7d319a873f6941431a16d8bf (patch) | |
| tree | e6d17e93e3ba038830b1985173ede0b43fc30c5f /data | |
| parent | d75551b56796933f983dbe182e6345cf5af55e8f (diff) | |
Remove deprecated data.Source type (#1973)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'data')
| -rw-r--r-- | data/datasource.go | 49 | ||||
| -rw-r--r-- | data/datasource_test.go | 65 |
2 files changed, 44 insertions, 70 deletions
diff --git a/data/datasource.go b/data/datasource.go index 5c663c55..86e3101c 100644 --- a/data/datasource.go +++ b/data/datasource.go @@ -26,7 +26,7 @@ type Data struct { Ctx context.Context // TODO: remove this before 4.0 - Sources map[string]*Source + Sources map[string]config.DataSource cache map[string]*fileContent @@ -58,21 +58,14 @@ func FromConfig(ctx context.Context, cfg *config.Config) *Data { // when datasources are refactored ctx = datafs.ContextWithStdin(ctx, cfg.Stdin) - sources := map[string]*Source{} + sources := map[string]config.DataSource{} for alias, d := range cfg.DataSources { - sources[alias] = &Source{ - Alias: alias, - URL: d.URL, - Header: d.Header, - } + sources[alias] = d } for alias, d := range cfg.Context { - sources[alias] = &Source{ - Alias: alias, - URL: d.URL, - Header: d.Header, - } + sources[alias] = d } + return &Data{ Ctx: ctx, Sources: sources, @@ -84,16 +77,14 @@ func FromConfig(ctx context.Context, cfg *config.Config) *Data { // // Deprecated: will be replaced in future type Source struct { - Alias string - URL *url.URL - Header http.Header // used for http[s]: URLs, nil otherwise - mediaType string + URL *url.URL + Header http.Header // used for http[s]: URLs, nil otherwise } // String is the method to format the flag's value, part of the flag.Value interface. // The String method's output will be used in diagnostics. func (s *Source) String() string { - return fmt.Sprintf("%s=%s (%s)", s.Alias, s.URL.String(), s.mediaType) + return s.URL.String() } // DefineDatasource - @@ -108,13 +99,12 @@ func (d *Data) DefineDatasource(alias, value string) (string, error) { if err != nil { return "", err } - s := &Source{ - Alias: alias, + s := config.DataSource{ URL: srcURL, Header: d.ExtraHeaders[alias], } if d.Sources == nil { - d.Sources = make(map[string]*Source) + d.Sources = make(map[string]config.DataSource) } d.Sources[alias] = s return "", nil @@ -126,24 +116,21 @@ func (d *Data) DatasourceExists(alias string) bool { return ok } -func (d *Data) lookupSource(alias string) (*Source, error) { +func (d *Data) lookupSource(alias string) (*config.DataSource, error) { source, ok := d.Sources[alias] if !ok { srcURL, err := url.Parse(alias) if err != nil || !srcURL.IsAbs() { return nil, fmt.Errorf("undefined datasource '%s': %w", alias, err) } - source = &Source{ - Alias: alias, + source = config.DataSource{ URL: srcURL, Header: d.ExtraHeaders[alias], } d.Sources[alias] = source } - if source.Alias == "" { - source.Alias = alias - } - return source, nil + + return &source, nil } func (d *Data) readDataSource(ctx context.Context, alias string, args ...string) (*fileContent, error) { @@ -151,7 +138,7 @@ func (d *Data) readDataSource(ctx context.Context, alias string, args ...string) if err != nil { return nil, err } - fc, err := d.readSource(ctx, source, args...) + fc, err := d.readSource(ctx, alias, source, args...) if err != nil { return nil, fmt.Errorf("couldn't read datasource '%s': %w", alias, err) } @@ -186,17 +173,17 @@ func (d *Data) DatasourceReachable(alias string, args ...string) bool { if !ok { return false } - _, err := d.readSource(d.Ctx, source, args...) + _, err := d.readSource(d.Ctx, alias, &source, args...) return err == nil } // readSource returns the (possibly cached) data from the given source, // as referenced by the given args -func (d *Data) readSource(ctx context.Context, source *Source, args ...string) (*fileContent, error) { +func (d *Data) readSource(ctx context.Context, alias string, source *config.DataSource, args ...string) (*fileContent, error) { if d.cache == nil { d.cache = make(map[string]*fileContent) } - cacheKey := source.Alias + cacheKey := alias for _, v := range args { cacheKey += v } diff --git a/data/datasource_test.go b/data/datasource_test.go index da3d3094..616506ae 100644 --- a/data/datasource_test.go +++ b/data/datasource_test.go @@ -52,7 +52,7 @@ func TestNewData(t *testing.T) { } func TestDatasource(t *testing.T) { - setup := func(ext, mime string, contents []byte) *Data { + setup := func(ext string, contents []byte) *Data { fname := "foo." + ext var uPath string if runtime.GOOS == osWindows { @@ -66,18 +66,16 @@ func TestDatasource(t *testing.T) { }) ctx := datafs.ContextWithFSProvider(context.Background(), datafs.WrappedFSProvider(fsys, "file", "")) - sources := map[string]*Source{ + sources := map[string]config.DataSource{ "foo": { - Alias: "foo", - URL: &url.URL{Scheme: "file", Path: uPath}, - mediaType: mime, + URL: &url.URL{Scheme: "file", Path: uPath}, }, } return &Data{Sources: sources, Ctx: ctx} } test := func(ext, mime string, contents []byte, expected interface{}) { - data := setup(ext, mime, contents) + data := setup(ext, contents) actual, err := data.Datasource("foo", "?type="+mime) require.NoError(t, err) @@ -98,7 +96,7 @@ func TestDatasource(t *testing.T) { test("yaml", yamlMimetype, []byte("---\n- 1\n- two\n- true\n"), []interface{}{1, "two", true}) - d := setup("", textMimetype, nil) + d := setup("", nil) actual, err := d.Datasource("foo") require.NoError(t, err) assert.Equal(t, "", actual) @@ -121,15 +119,12 @@ func TestDatasourceReachable(t *testing.T) { }) ctx := datafs.ContextWithFSProvider(context.Background(), datafs.WrappedFSProvider(fsys, "file", "")) - sources := map[string]*Source{ + sources := map[string]config.DataSource{ "foo": { - Alias: "foo", - URL: &url.URL{Scheme: "file", Path: uPath}, - mediaType: jsonMimetype, + URL: &url.URL{Scheme: "file", Path: uPath}, }, "bar": { - Alias: "bar", - URL: &url.URL{Scheme: "file", Path: "/bogus"}, + URL: &url.URL{Scheme: "file", Path: "/bogus"}, }, } data := &Data{Sources: sources, Ctx: ctx} @@ -139,8 +134,8 @@ func TestDatasourceReachable(t *testing.T) { } func TestDatasourceExists(t *testing.T) { - sources := map[string]*Source{ - "foo": {Alias: "foo"}, + sources := map[string]config.DataSource{ + "foo": {}, } data := &Data{Sources: sources} assert.True(t, data.DatasourceExists("foo")) @@ -164,11 +159,9 @@ func TestInclude(t *testing.T) { }) ctx := datafs.ContextWithFSProvider(context.Background(), datafs.WrappedFSProvider(fsys, "file", "")) - sources := map[string]*Source{ + sources := map[string]config.DataSource{ "foo": { - Alias: "foo", - URL: &url.URL{Scheme: "file", Path: uPath}, - mediaType: textMimetype, + URL: &url.URL{Scheme: "file", Path: uPath}, }, } data := &Data{Sources: sources, Ctx: ctx} @@ -194,14 +187,12 @@ func TestDefineDatasource(t *testing.T) { _, err = d.DefineDatasource("data", "foo.json") s := d.Sources["data"] require.NoError(t, err) - assert.Equal(t, "data", s.Alias) assert.EqualValues(t, &url.URL{Path: "foo.json"}, s.URL) d = &Data{} _, err = d.DefineDatasource("data", "/otherdir/foo.json") s = d.Sources["data"] require.NoError(t, err) - assert.Equal(t, "data", s.Alias) assert.Equal(t, "file", s.URL.Scheme) assert.True(t, s.URL.IsAbs()) assert.Equal(t, "/otherdir/foo.json", s.URL.Path) @@ -210,27 +201,26 @@ func TestDefineDatasource(t *testing.T) { _, err = d.DefineDatasource("data", "sftp://example.com/blahblah/foo.json") s = d.Sources["data"] require.NoError(t, err) - assert.Equal(t, "data", s.Alias) assert.Equal(t, "sftp", s.URL.Scheme) assert.True(t, s.URL.IsAbs()) assert.Equal(t, "/blahblah/foo.json", s.URL.Path) d = &Data{ - Sources: map[string]*Source{ - "data": {Alias: "data"}, + Sources: map[string]config.DataSource{ + "data": {}, }, } _, err = d.DefineDatasource("data", "/otherdir/foo.json") s = d.Sources["data"] require.NoError(t, err) - assert.Equal(t, "data", s.Alias) assert.Nil(t, s.URL) d = &Data{} _, err = d.DefineDatasource("data", "/otherdir/foo?type=application/x-env") - s = d.Sources["data"] require.NoError(t, err) - assert.Equal(t, "data", s.Alias) + s = d.Sources["data"] + require.NotNil(t, s) + assert.Equal(t, "/otherdir/foo", s.URL.Path) } func TestFromConfig(t *testing.T) { @@ -240,7 +230,7 @@ func TestFromConfig(t *testing.T) { actual := FromConfig(ctx, cfg) expected := &Data{ Ctx: actual.Ctx, - Sources: map[string]*Source{}, + Sources: map[string]config.DataSource{}, } assert.EqualValues(t, expected, actual) @@ -254,10 +244,9 @@ func TestFromConfig(t *testing.T) { actual = FromConfig(ctx, cfg) expected = &Data{ Ctx: actual.Ctx, - Sources: map[string]*Source{ + Sources: map[string]config.DataSource{ "foo": { - Alias: "foo", - URL: mustParseURL("http://example.com"), + URL: mustParseURL("http://example.com"), }, }, } @@ -286,14 +275,12 @@ func TestFromConfig(t *testing.T) { actual = FromConfig(ctx, cfg) expected = &Data{ Ctx: actual.Ctx, - Sources: map[string]*Source{ + Sources: map[string]config.DataSource{ "foo": { - Alias: "foo", - URL: mustParseURL("http://foo.com"), + URL: mustParseURL("http://foo.com"), }, "bar": { - Alias: "bar", - URL: mustParseURL("http://bar.com"), + URL: mustParseURL("http://bar.com"), Header: http.Header{ "Foo": []string{"bar"}, }, @@ -309,9 +296,9 @@ func TestFromConfig(t *testing.T) { } func TestListDatasources(t *testing.T) { - sources := map[string]*Source{ - "foo": {Alias: "foo"}, - "bar": {Alias: "bar"}, + sources := map[string]config.DataSource{ + "foo": {}, + "bar": {}, } data := &Data{Sources: sources} |
