-
Notifications
You must be signed in to change notification settings - Fork 26
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
Use logfmt for plan modifiers #109
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
func mustGetAndDeleteKey(m map[string]string, key string) (string, error) { | ||
val, ok := m[key] | ||
if !ok { | ||
return "", fmt.Errorf("could not find key %q", key) | ||
} | ||
delete(m, key) | ||
return val, nil | ||
} | ||
|
||
func keys(m map[string]string) []string { | ||
var vals []string | ||
for k := range m { | ||
vals = append(vals, k) | ||
} | ||
sort.Strings(vals) | ||
return vals | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKeys(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
m map[string]string | ||
|
||
want []string | ||
}{ | ||
{ | ||
name: "nil map", | ||
|
||
want: nil, | ||
}, | ||
{ | ||
name: "empty map", | ||
|
||
want: nil, | ||
}, | ||
{ | ||
name: "filled map", | ||
m: map[string]string{ | ||
// Use an arbitrary order | ||
"key2": "value2", | ||
"key3": "value3", | ||
"key1": "value1", | ||
}, | ||
|
||
want: []string{"key1", "key2", "key3"}, | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.want, keys(tt.m)) | ||
}) | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLogFmtToMap(t *testing.T) { | ||
type args struct { | ||
logFmt string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want map[string]string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty string", | ||
args: args{ | ||
logFmt: "", | ||
}, | ||
want: map[string]string{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "single key value pair", | ||
args: args{ | ||
logFmt: "key=value", | ||
}, | ||
want: map[string]string{"key": "value"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "multiple key value pairs", | ||
args: args{ | ||
logFmt: "key1=value1 key2=value2", | ||
}, | ||
want: map[string]string{"key1": "value1", "key2": "value2"}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "duplicate key", | ||
args: args{ | ||
logFmt: "key=value1 key=value2", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "multiple records", | ||
args: args{ | ||
logFmt: "key1=value1 key2=value2\nkey3=value3", | ||
}, | ||
want: map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := LogFmtToMap(tt.args.logFmt) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
nit: Doesn't must typically imply a panic will be raised, not an error returned? Could be totally off on that and I don't have a better name.
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.
You are right. This is not very idiomatic naming...