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

Use logfmt for plan modifiers #109

Merged
merged 2 commits into from
Feb 14, 2024
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
24 changes: 24 additions & 0 deletions cmd/pg-schema-diff/datastructs.go
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) {
Copy link
Collaborator

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.

Copy link
Collaborator Author

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...

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
}
42 changes: 42 additions & 0 deletions cmd/pg-schema-diff/datastructs_test.go
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))
})
}
}
25 changes: 25 additions & 0 deletions cmd/pg-schema-diff/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import (
"fmt"
"strings"

"github.com/go-logfmt/logfmt"
"github.com/jackc/pgx/v4"
"github.com/spf13/cobra"
"github.com/stripe/pg-schema-diff/pkg/log"
Expand All @@ -27,3 +31,24 @@ func parseConnConfig(c connFlags, logger log.Logger) (*pgx.ConnConfig, error) {

return pgx.ParseConfig(c.dsn)
}

// LogFmtToMap parses all LogFmt key/value pairs from the provided string into a
// map.
//
// All records are scanned. If a duplicate key is found, an error is returned.
func LogFmtToMap(logFmt string) (map[string]string, error) {
logMap := make(map[string]string)
decoder := logfmt.NewDecoder(strings.NewReader(logFmt))
for decoder.ScanRecord() {
for decoder.ScanKeyval() {
if _, ok := logMap[string(decoder.Key())]; ok {
return nil, fmt.Errorf("duplicate key %q in logfmt", string(decoder.Key()))
}
logMap[string(decoder.Key())] = string(decoder.Value())
}
}
if decoder.Err() != nil {
return nil, decoder.Err()
}
return logMap, nil
}
72 changes: 72 additions & 0 deletions cmd/pg-schema-diff/flags_test.go
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)
})
}
}
Loading
Loading