This repository was archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 145
schema/types: rework empty volume type code #541
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c9c118c
schema/types: rework empty volume type code
jonboulle e151013
schema/types: add Volume.String() benchmark
jonboulle 1141f80
schema/types: construct string more efficiently
jonboulle b2aaa28
schema/types: use *bool helper func for consistency
jonboulle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,9 +19,19 @@ import ( | |
| "testing" | ||
| ) | ||
|
|
||
| func TestVolumeFromString(t *testing.T) { | ||
| trueVar := true | ||
| falseVar := false | ||
| func bp(b bool) *bool { | ||
| return &b | ||
| } | ||
|
|
||
| func sp(s string) *string { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol. I hate this language. |
||
| return &s | ||
| } | ||
|
|
||
| func ip(i int) *int { | ||
| return &i | ||
| } | ||
|
|
||
| func TestVolumeToFromString(t *testing.T) { | ||
| tests := []struct { | ||
| s string | ||
| v Volume | ||
|
|
@@ -33,9 +43,9 @@ func TestVolumeFromString(t *testing.T) { | |
| Kind: "host", | ||
| Source: "/tmp", | ||
| ReadOnly: nil, | ||
| Mode: "", | ||
| UID: -1, | ||
| GID: -1, | ||
| Mode: nil, | ||
| UID: nil, | ||
| GID: nil, | ||
| }, | ||
| }, | ||
| { | ||
|
|
@@ -44,10 +54,10 @@ func TestVolumeFromString(t *testing.T) { | |
| Name: "foobar", | ||
| Kind: "host", | ||
| Source: "/tmp", | ||
| ReadOnly: &falseVar, | ||
| Mode: "", | ||
| UID: -1, | ||
| GID: -1, | ||
| ReadOnly: bp(false), | ||
| Mode: nil, | ||
| UID: nil, | ||
| GID: nil, | ||
| }, | ||
| }, | ||
| { | ||
|
|
@@ -56,10 +66,10 @@ func TestVolumeFromString(t *testing.T) { | |
| Name: "foobar", | ||
| Kind: "host", | ||
| Source: "/tmp", | ||
| ReadOnly: &trueVar, | ||
| Mode: "", | ||
| UID: -1, | ||
| GID: -1, | ||
| ReadOnly: bp(true), | ||
| Mode: nil, | ||
| UID: nil, | ||
| GID: nil, | ||
| }, | ||
| }, | ||
| { | ||
|
|
@@ -68,31 +78,31 @@ func TestVolumeFromString(t *testing.T) { | |
| Name: "foobar", | ||
| Kind: "empty", | ||
| ReadOnly: nil, | ||
| Mode: "0755", | ||
| UID: 0, | ||
| GID: 0, | ||
| Mode: sp(emptyVolumeDefaultMode), | ||
| UID: ip(emptyVolumeDefaultUID), | ||
| GID: ip(emptyVolumeDefaultGID), | ||
| }, | ||
| }, | ||
| { | ||
| "foobar,kind=empty,readOnly=true", | ||
| Volume{ | ||
| Name: "foobar", | ||
| Kind: "empty", | ||
| ReadOnly: &trueVar, | ||
| Mode: "0755", | ||
| UID: 0, | ||
| GID: 0, | ||
| ReadOnly: bp(true), | ||
| Mode: sp(emptyVolumeDefaultMode), | ||
| UID: ip(emptyVolumeDefaultUID), | ||
| GID: ip(emptyVolumeDefaultGID), | ||
| }, | ||
| }, | ||
| { | ||
| "foobar,kind=empty,readOnly=true,mode=0777", | ||
| Volume{ | ||
| Name: "foobar", | ||
| Kind: "empty", | ||
| ReadOnly: &trueVar, | ||
| Mode: "0777", | ||
| UID: 0, | ||
| GID: 0, | ||
| ReadOnly: bp(true), | ||
| Mode: sp("0777"), | ||
| UID: ip(emptyVolumeDefaultUID), | ||
| GID: ip(emptyVolumeDefaultGID), | ||
| }, | ||
| }, | ||
| { | ||
|
|
@@ -101,9 +111,9 @@ func TestVolumeFromString(t *testing.T) { | |
| Name: "foobar", | ||
| Kind: "empty", | ||
| ReadOnly: nil, | ||
| Mode: "0777", | ||
| UID: 1000, | ||
| GID: 0, | ||
| Mode: sp("0777"), | ||
| UID: ip(1000), | ||
| GID: ip(emptyVolumeDefaultGID), | ||
| }, | ||
| }, | ||
| { | ||
|
|
@@ -112,9 +122,9 @@ func TestVolumeFromString(t *testing.T) { | |
| Name: "foobar", | ||
| Kind: "empty", | ||
| ReadOnly: nil, | ||
| Mode: "0777", | ||
| UID: 1000, | ||
| GID: 1000, | ||
| Mode: sp("0777"), | ||
| UID: ip(1000), | ||
| GID: ip(1000), | ||
| }, | ||
| }, | ||
| } | ||
|
|
@@ -126,13 +136,22 @@ func TestVolumeFromString(t *testing.T) { | |
| if !reflect.DeepEqual(*v, tt.v) { | ||
| t.Errorf("#%d: v=%v, want %v", i, *v, tt.v) | ||
| } | ||
| // volume serialization should be reversible | ||
| o := v.String() | ||
| if o != tt.s { | ||
| t.Errorf("#%d: v.String()=%s, want %s", i, o, tt.s) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestVolumeFromStringBad(t *testing.T) { | ||
| tests := []string{ | ||
| "#foobar,kind=host,source=/tmp", | ||
| "foobar,kind=host,source=/tmp,readOnly=true,asdf=asdf", | ||
| "foobar,kind=host,source=tmp", | ||
| "foobar,kind=host,uid=3", | ||
| "foobar,kind=host,mode=0755", | ||
| "foobar,kind=host,mode=0600,readOnly=true,gid=0", | ||
| "foobar,kind=empty,source=/tmp", | ||
| } | ||
| for i, in := range tests { | ||
|
|
@@ -145,3 +164,17 @@ func TestVolumeFromStringBad(t *testing.T) { | |
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkVolumeToString(b *testing.B) { | ||
| vol := Volume{ | ||
| Name: "foobar", | ||
| Kind: "empty", | ||
| ReadOnly: bp(true), | ||
| Mode: sp("0777"), | ||
| UID: ip(3), | ||
| GID: ip(4), | ||
| } | ||
| for i := 0; i < b.N; i++ { | ||
| _ = vol.String() | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
even if it works this looks weird
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's idiomatic actually :/
https://golang.org/doc/effective_go.html
search for "req := req"