Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

feat: allow subfield flattening with table:"_" #273

Merged
merged 1 commit into from
Mar 9, 2021
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
2 changes: 1 addition & 1 deletion coder-sdk/workspace_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type KubernetesProvider struct {
EnvproxyAccessURL string `json:"envproxy_access_url" validate:"required" table:"Access URL"`
DevurlHost string `json:"devurl_host" table:"Devurl Host"`
OrgWhitelist []string `json:"org_whitelist" table:"-"`
KubeProviderConfig `json:"config"`
KubeProviderConfig `json:"config" table:"_"`
}

// KubeProviderConfig defines Kubernetes-specific configuration options.
Expand Down
18 changes: 18 additions & 0 deletions pkg/tablewriter/tablewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ const structFieldTagKey = "table"
// StructValues tab delimits the values of a given struct.
//
// Tag a field `table:"-"` to hide it from output.
// Tag a field `table:"_"` to flatten its subfields.
func StructValues(data interface{}) string {
v := reflect.ValueOf(data)
s := &strings.Builder{}
for i := 0; i < v.NumField(); i++ {
if shouldHideField(v.Type().Field(i)) {
continue
}
if shouldFlatten(v.Type().Field(i)) {
fmt.Fprintf(s, "%v\t", StructValues(v.Field(i).Interface()))
continue
}
fmt.Fprintf(s, "%v\t", v.Field(i).Interface())
}
return s.String()
Expand All @@ -28,14 +33,22 @@ func StructValues(data interface{}) string {
// StructFieldNames tab delimits the field names of a given struct.
//
// Tag a field `table:"-"` to hide it from output.
// Tag a field `table:"_"` to flatten its subfields.
func StructFieldNames(data interface{}) string {
v := reflect.ValueOf(data)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
s := &strings.Builder{}
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
if shouldHideField(field) {
continue
}
if shouldFlatten(field) {
fmt.Fprintf(s, "%s\t", StructFieldNames(reflect.New(field.Type).Interface()))
continue
}
fmt.Fprintf(s, "%s\t", fieldName(field))
}
return s.String()
Expand All @@ -45,6 +58,7 @@ func StructFieldNames(data interface{}) string {
// tabular format. Headers abide by the `table` struct tag.
//
// `table:"-"` omits the field and no tag defaults to the Go identifier.
// `table:"_"` flattens a fields subfields.
func WriteTable(length int, each func(i int) interface{}) error {
if length < 1 {
return nil
Expand Down Expand Up @@ -73,6 +87,10 @@ func fieldName(f reflect.StructField) string {
return f.Name
}

func shouldFlatten(f reflect.StructField) bool {
return f.Tag.Get(structFieldTagKey) == "_"
}

func shouldHideField(f reflect.StructField) bool {
return f.Tag.Get(structFieldTagKey) == "-"
}