Thanks to visit codestin.com
Credit goes to github.com

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

Commit f44bc76

Browse files
committed
Use dedicated Column interface for table formatting nested structs
1 parent deb23b4 commit f44bc76

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

coder-sdk/env.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ type EnvironmentStat struct {
5454
DiskUsed int64 `json:"disk_used"`
5555
}
5656

57-
func (e EnvironmentStat) String() string { return string(e.ContainerStatus) }
57+
// Column defines how EnvironmentStat should format in as a table column.
58+
func (e EnvironmentStat) Column() string { return string(e.ContainerStatus) }
5859

5960
// EnvironmentStatus refers to the states of an environment.
6061
type EnvironmentStatus string

coder-sdk/tags.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ type ImageTag struct {
1818
CreatedAt time.Time `json:"created_at" table:"-"`
1919
}
2020

21-
func (i ImageTag) String() string {
21+
// Column defines how ImageTag should format in as a table column.
22+
func (i ImageTag) Column() string {
2223
return i.Tag
2324
}
2425

@@ -29,9 +30,8 @@ type OSRelease struct {
2930
HomeURL string `json:"home_url"`
3031
}
3132

32-
func (o OSRelease) String() string {
33-
return o.PrettyName
34-
}
33+
// Column defines how OSRelease should format in as a table column.
34+
func (o OSRelease) Column() string { return o.PrettyName }
3535

3636
// CreateImageTagReq defines the request parameters for creating a new image tag.
3737
type CreateImageTagReq struct {

pkg/tablewriter/tablewriter.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import (
1010

1111
const structFieldTagKey = "table"
1212

13+
// Column defines an interface for formatting a type as a column.
14+
type Column interface {
15+
Column() string
16+
}
17+
1318
// StructValues tab delimits the values of a given struct.
1419
//
1520
// Tag a field `table:"-"` to hide it from output.
@@ -20,7 +25,12 @@ func StructValues(data interface{}) string {
2025
if shouldHideField(v.Type().Field(i)) {
2126
continue
2227
}
23-
fmt.Fprintf(s, "%v\t", v.Field(i).Interface())
28+
value := v.Field(i).Interface()
29+
if column, ok := value.(Column); ok {
30+
fmt.Fprintf(s, "%s\t", column.Column())
31+
} else {
32+
fmt.Fprintf(s, "%v\t", value)
33+
}
2434
}
2535
return s.String()
2636
}

0 commit comments

Comments
 (0)