Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion config/config_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ func (a *ConfigAttribute) IsZero(cfg *Config) bool {
func (a *ConfigAttribute) GetString(cfg *Config) string {
rv := reflect.ValueOf(cfg)
field := rv.Elem().Field(a.num)
return fmt.Sprintf("%v", field.Interface())
switch a.Kind {
case reflect.Slice:
parts := field.Interface().([]string)
return strings.Join(parts, ",")
default:
return fmt.Sprintf("%v", field.Interface())
}
}

func (a *ConfigAttribute) HasAuthAttribute() bool {
Expand Down
105 changes: 105 additions & 0 deletions config/config_attributes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -59,3 +60,107 @@ func TestConfigFile_Configure_ListParsing(t *testing.T) {
})
}
}

func TestConfigAttribute_GetString(t *testing.T) {
findAttr := func(name string) *ConfigAttribute {
for i := range ConfigAttributes {
if ConfigAttributes[i].Name == name {
return &ConfigAttributes[i]
}
}
t.Fatalf("attribute %q not found", name)
return nil
}

testCases := []struct {
name string
attrName string
wantKind reflect.Kind
cfg *Config
want string
}{
{
name: "string type returns string value",
attrName: "host",
wantKind: reflect.String,
cfg: &Config{Host: "https://example.databricks.com"},
want: "https://example.databricks.com",
},
{
name: "string type returns empty string when not set",
attrName: "host",
wantKind: reflect.String,
cfg: &Config{},
want: "",
},
{
name: "bool type returns true",
attrName: "skip_verify",
wantKind: reflect.Bool,
cfg: &Config{InsecureSkipVerify: true},
want: "true",
},
{
name: "bool type returns false",
attrName: "skip_verify",
wantKind: reflect.Bool,
cfg: &Config{InsecureSkipVerify: false},
want: "false",
},
{
name: "int type returns numeric string",
attrName: "http_timeout_seconds",
wantKind: reflect.Int,
cfg: &Config{HTTPTimeoutSeconds: 120},
want: "120",
},
{
name: "int type returns zero",
attrName: "http_timeout_seconds",
wantKind: reflect.Int,
cfg: &Config{HTTPTimeoutSeconds: 0},
want: "0",
},
{
name: "slice type returns comma-joined string",
attrName: "scopes",
wantKind: reflect.Slice,
cfg: &Config{Scopes: []string{"alpha", "beta", "gamma"}},
want: "alpha,beta,gamma",
},
{
name: "slice type returns single item",
attrName: "scopes",
wantKind: reflect.Slice,
cfg: &Config{Scopes: []string{"all-apis"}},
want: "all-apis",
},
{
name: "slice type returns empty string for nil slice",
attrName: "scopes",
wantKind: reflect.Slice,
cfg: &Config{Scopes: nil},
want: "",
},
{
name: "slice type returns empty string for empty slice",
attrName: "scopes",
wantKind: reflect.Slice,
cfg: &Config{Scopes: []string{}},
want: "",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
attr := findAttr(tc.attrName)
if attr.Kind != tc.wantKind {
t.Fatalf("expected %v kind, got %v", tc.wantKind, attr.Kind)
}
got := attr.GetString(tc.cfg)
if got != tc.want {
t.Errorf("GetString() = %q, want %q", got, tc.want)
}
})
}
}
Loading