Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve human marshal for nil value #737

Merged
merged 5 commits into from
Mar 2, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ hair-count 0
is-happy false
eyes-color unknown
status stopped
name
name -
region fr-par
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ hair-count 9223372036854775808
is-happy true
eyes-color amber
status stopped
name
name -
region fr-par
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ hair-count 9223372036854775809
is-happy true
eyes-color blue
status stopped
name
name -
region fr-par
2 changes: 1 addition & 1 deletion internal/e2e/testdata/test-human-get-simple.stdout.golden
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ hair-count 9223372036854775809
is-happy true
eyes-color blue
status stopped
name
name -
region fr-par
6 changes: 3 additions & 3 deletions internal/e2e/testdata/test-human-list-simple.stdout.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ID ORGANIZATION ID CREATED AT UPDATED AT HEIGHT SHOE SIZE ALTITUDE IN METER ALTITUDE IN MILLIMETER FINGERS COUNT HAIR COUNT IS HAPPY EYES COLOR STATUS NAME REGION
0194fdc2-fa2f-fcc0-41d3-ff12045b73c8 11111111-1111-1111-1111-111111111111 few seconds ago few seconds ago 0 0 0 0 0 0 false unknown stopped fr-par
62a5eee8-2abd-f44a-2d0b-75fb180daf48 11111111-1111-1111-1111-111111111111 few seconds ago few seconds ago 0 0 0 0 0 0 false unknown stopped fr-par
39465185-0fd4-a178-892e-e285ece15114 11111111-1111-1111-1111-111111111111 few seconds ago few seconds ago 0 0 0 0 0 0 false unknown stopped fr-par
0194fdc2-fa2f-fcc0-41d3-ff12045b73c8 11111111-1111-1111-1111-111111111111 few seconds ago few seconds ago 0 0 0 0 0 0 false unknown stopped - fr-par
62a5eee8-2abd-f44a-2d0b-75fb180daf48 11111111-1111-1111-1111-111111111111 few seconds ago few seconds ago 0 0 0 0 0 0 false unknown stopped - fr-par
39465185-0fd4-a178-892e-e285ece15114 11111111-1111-1111-1111-111111111111 few seconds ago few seconds ago 0 0 0 0 0 0 false unknown stopped - fr-par
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ hair-count 9223372036854775809
is-happy true
eyes-color blue
status stopped
name
name -
region fr-par
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ hair-count 9223372036854775808
is-happy false
eyes-color amber
status stopped
name
name -
region fr-par
6 changes: 5 additions & 1 deletion internal/human/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ func Marshal(data interface{}, opt *MarshalOpt) (string, error) {
}

rValue := reflect.ValueOf(data)
if !rValue.IsValid() {
return defaultMarshalerFunc(nil, opt)
}

rType := rValue.Type()

switch {
// If data is nil
case isInterfaceNil(data):
return "", nil
return defaultMarshalerFunc(nil, opt)

// If data has a registered MarshalerFunc call it
case marshalerFuncs[rType] != nil:
Expand Down
11 changes: 11 additions & 0 deletions internal/human/marshal_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ func RegisterMarshalerFunc(i interface{}, f MarshalerFunc) {

// DefaultMarshalerFunc is used by default for all non-registered type
func defaultMarshalerFunc(i interface{}, opt *MarshalOpt) (string, error) {
if i == nil {
i = "-"
}

switch v := i.(type) {
case string:
if v == "" {
i = "-"
}
}

return fmt.Sprint(i), nil
}

Expand Down
14 changes: 13 additions & 1 deletion internal/human/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestMarshal(t *testing.T) {
strings.0 s1
strings.1 s2
time ` + humanDate + `
struct.string
struct.string -
struct.int 0
struct.bool false
struct.time a long while ago
Expand Down Expand Up @@ -154,4 +154,16 @@ func TestMarshal(t *testing.T) {
`,
err: nil,
}))

t.Run("empty string", run(&testCase{
data: "",
result: `-`,
err: nil,
}))

t.Run("nil", run(&testCase{
data: nil,
result: `-`,
err: nil,
}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ image.creation-date few seconds ago
image.modification-date few seconds ago
image.default-bootscript x86_64 mainline 4.9.93 rev1
image.extra-volumes 0
image.from-server
image.from-server -
image.organization 51b656e3-4865-41e8-adbc-0c45bdd780db
image.public true
image.root-volume dd5f5c10-23b1-4c9c-8445-eb6740957c84
Expand All @@ -32,6 +32,6 @@ boot-type local
volumes 1
security-group.id 980142a3-a959-4aee-8325-91a6ba787439
security-group.name Default security group
state-detail
state-detail -
arch x86_64
zone fr-par-1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ image.creation-date few seconds ago
image.modification-date few seconds ago
image.default-bootscript x86_64 mainline 4.9.93 rev1
image.extra-volumes 0
image.from-server
image.from-server -
image.organization 51b656e3-4865-41e8-adbc-0c45bdd780db
image.public true
image.root-volume dd5f5c10-23b1-4c9c-8445-eb6740957c84
Expand All @@ -33,7 +33,7 @@ boot-type local
volumes 1
security-group.id e5bf4522-94b4-4933-bebb-9b21f786b4af
security-group.name Default security group
state-detail
state-detail -
arch x86_64
zone fr-par-1

Expand All @@ -45,7 +45,7 @@ creation-date few seconds ago
modification-date few seconds ago
default-bootscript x86_64 mainline 4.9.93 rev1
extra-volumes 0
from-server
from-server -
organization 51b656e3-4865-41e8-adbc-0c45bdd780db
public true
root-volume dd5f5c10-23b1-4c9c-8445-eb6740957c84
Expand All @@ -57,4 +57,4 @@ Server - Allowed Actions:

Volumes:
ID NAME EXPORT URI SIZE VOLUME TYPE CREATION DATE MODIFICATION DATE ORGANIZATION STATE ZONE
db2275ad-aed3-4150-a106-0044e86b1e6b snapshot-de728daa-0bf6-4c64-abf5-a9477e791c83-2019-03-05_10:13 20 GB l_ssd few seconds ago few seconds ago 14d2f7ae-9775-414c-9bed-6810e060d500 available fr-par-1
db2275ad-aed3-4150-a106-0044e86b1e6b snapshot-de728daa-0bf6-4c64-abf5-a9477e791c83-2019-03-05_10:13 - 20 GB l_ssd few seconds ago few seconds ago 14d2f7ae-9775-414c-9bed-6810e060d500 available fr-par-1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ arch x86_64
creation-date few seconds ago
modification-date few seconds ago
extra-volumes 0
from-server
from-server -
organization aba2d0d0-b01d-4d88-b322-935edc96d0fd
public false
root-volume 26a882cd-62d5-47d6-b52f-c3e677c806b5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ID NAME STATE ZONE PUBLIC IP PRIVATE IP IMAGE NAME TAGS MODIFICATION DATE CREATION DATE IMAGE ID PROTECTED VOLUMES SECURITY GROUP ID SECURITY GROUP NAME STATE DETAIL ARCH
18e19087-4435-420b-abf7-a47c50a77fe7 cli-srv-mystifying-brattain archived fr-par-1 163.172.170.53 Ubuntu Bionic Beaver [] few seconds ago few seconds ago f974feac-abae-4365-b988-8ec7d1cec10d false 1 49822f6c-1d32-465c-a017-720691d159b2 Default security group x86_64
18e19087-4435-420b-abf7-a47c50a77fe7 cli-srv-mystifying-brattain archived fr-par-1 163.172.170.53 Ubuntu Bionic Beaver [] few seconds ago few seconds ago f974feac-abae-4365-b988-8ec7d1cec10d false 1 49822f6c-1d32-465c-a017-720691d159b2 Default security group - x86_64
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ server.image.creation-date few seconds ago
server.image.modification-date few seconds ago
server.image.default-bootscript x86_64 mainline 4.9.93 rev1
server.image.extra-volumes 0
server.image.from-server
server.image.from-server -
server.image.organization 51b656e3-4865-41e8-adbc-0c45bdd780db
server.image.public true
server.image.root-volume dd5f5c10-23b1-4c9c-8445-eb6740957c84
Expand All @@ -32,6 +32,6 @@ server.boot-type local
server.volumes 1
server.security-group.id e5bf4522-94b4-4933-bebb-9b21f786b4af
server.security-group.name Default security group
server.state-detail
server.state-detail -
server.arch x86_64
server.zone fr-par-1