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

Commit 6e89477

Browse files
authored
feat: allow subfield flattening with table:"_" (#273)
1 parent fa3f53b commit 6e89477

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

coder-sdk/workspace_providers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type KubernetesProvider struct {
1919
EnvproxyAccessURL string `json:"envproxy_access_url" validate:"required" table:"Access URL"`
2020
DevurlHost string `json:"devurl_host" table:"Devurl Host"`
2121
OrgWhitelist []string `json:"org_whitelist" table:"-"`
22-
KubeProviderConfig `json:"config"`
22+
KubeProviderConfig `json:"config" table:"_"`
2323
}
2424

2525
// KubeProviderConfig defines Kubernetes-specific configuration options.

pkg/tablewriter/tablewriter.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ const structFieldTagKey = "table"
1313
// StructValues tab delimits the values of a given struct.
1414
//
1515
// Tag a field `table:"-"` to hide it from output.
16+
// Tag a field `table:"_"` to flatten its subfields.
1617
func StructValues(data interface{}) string {
1718
v := reflect.ValueOf(data)
1819
s := &strings.Builder{}
1920
for i := 0; i < v.NumField(); i++ {
2021
if shouldHideField(v.Type().Field(i)) {
2122
continue
2223
}
24+
if shouldFlatten(v.Type().Field(i)) {
25+
fmt.Fprintf(s, "%v\t", StructValues(v.Field(i).Interface()))
26+
continue
27+
}
2328
fmt.Fprintf(s, "%v\t", v.Field(i).Interface())
2429
}
2530
return s.String()
@@ -28,14 +33,22 @@ func StructValues(data interface{}) string {
2833
// StructFieldNames tab delimits the field names of a given struct.
2934
//
3035
// Tag a field `table:"-"` to hide it from output.
36+
// Tag a field `table:"_"` to flatten its subfields.
3137
func StructFieldNames(data interface{}) string {
3238
v := reflect.ValueOf(data)
39+
if v.Kind() == reflect.Ptr {
40+
v = v.Elem()
41+
}
3342
s := &strings.Builder{}
3443
for i := 0; i < v.NumField(); i++ {
3544
field := v.Type().Field(i)
3645
if shouldHideField(field) {
3746
continue
3847
}
48+
if shouldFlatten(field) {
49+
fmt.Fprintf(s, "%s\t", StructFieldNames(reflect.New(field.Type).Interface()))
50+
continue
51+
}
3952
fmt.Fprintf(s, "%s\t", fieldName(field))
4053
}
4154
return s.String()
@@ -45,6 +58,7 @@ func StructFieldNames(data interface{}) string {
4558
// tabular format. Headers abide by the `table` struct tag.
4659
//
4760
// `table:"-"` omits the field and no tag defaults to the Go identifier.
61+
// `table:"_"` flattens a fields subfields.
4862
func WriteTable(length int, each func(i int) interface{}) error {
4963
if length < 1 {
5064
return nil
@@ -73,6 +87,10 @@ func fieldName(f reflect.StructField) string {
7387
return f.Name
7488
}
7589

90+
func shouldFlatten(f reflect.StructField) bool {
91+
return f.Tag.Get(structFieldTagKey) == "_"
92+
}
93+
7694
func shouldHideField(f reflect.StructField) bool {
7795
return f.Tag.Get(structFieldTagKey) == "-"
7896
}

0 commit comments

Comments
 (0)