diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-07-19 11:17:19 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-07-20 09:48:15 +0300 |
commit | 93aee01d54a24f6d3be44fbee62816963a219550 (patch) | |
tree | 889ac0fd2efb876bb983d8e4483ce99111c6fbf4 | |
parent | 8c2b26ebf7395c9802ea1a9e2c94292c6356bb6b (diff) |
git2go: Unify all serialization-related code
We have two ways of serializing parameters for git2go subcommands, once
via JSON and once via gob. They're both located in separate files, even
though they're thematically related.
Refactor the code and move them into a single "serialization" code unit.
-rw-r--r-- | internal/git2go/command.go | 24 | ||||
-rw-r--r-- | internal/git2go/command_test.go | 28 | ||||
-rw-r--r-- | internal/git2go/serialization.go (renamed from internal/git2go/gob.go) | 25 | ||||
-rw-r--r-- | internal/git2go/serialization_test.go (renamed from internal/git2go/gob_test.go) | 20 |
4 files changed, 45 insertions, 52 deletions
diff --git a/internal/git2go/command.go b/internal/git2go/command.go index ad25a0662..59be8fcb0 100644 --- a/internal/git2go/command.go +++ b/internal/git2go/command.go @@ -3,15 +3,12 @@ package git2go import ( "bytes" "context" - "encoding/base64" - "encoding/json" "errors" "fmt" "io" "os" "os/exec" "path/filepath" - "strings" "gitlab.com/gitlab-org/gitaly/v14/internal/command" "gitlab.com/gitlab-org/gitaly/v14/internal/version" @@ -54,24 +51,3 @@ func run(ctx context.Context, binaryPath string, stdin io.Reader, args ...string return &stdout, nil } - -func serialize(v interface{}) (string, error) { - marshalled, err := json.Marshal(v) - if err != nil { - return "", err - } - return base64.StdEncoding.EncodeToString(marshalled), nil -} - -func deserialize(serialized string, v interface{}) error { - base64Decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(serialized)) - jsonDecoder := json.NewDecoder(base64Decoder) - return jsonDecoder.Decode(v) -} - -func serializeTo(writer io.Writer, v interface{}) error { - base64Encoder := base64.NewEncoder(base64.StdEncoding, writer) - defer base64Encoder.Close() - jsonEncoder := json.NewEncoder(base64Encoder) - return jsonEncoder.Encode(v) -} diff --git a/internal/git2go/command_test.go b/internal/git2go/command_test.go deleted file mode 100644 index 14f1c0a3a..000000000 --- a/internal/git2go/command_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package git2go - -import ( - "bytes" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestSerialization_SerializeTo(t *testing.T) { - type testStruct struct { - Contents string `json:"contents"` - } - - var buf bytes.Buffer - - input := testStruct{ - Contents: "foobar", - } - err := serializeTo(&buf, &input) - require.NoError(t, err) - require.NotZero(t, buf.Len()) - - var output testStruct - err = deserialize(buf.String(), &output) - require.NoError(t, err) - require.Equal(t, input, output) -} diff --git a/internal/git2go/gob.go b/internal/git2go/serialization.go index c6a53875c..c706f14a5 100644 --- a/internal/git2go/gob.go +++ b/internal/git2go/serialization.go @@ -3,10 +3,14 @@ package git2go import ( "bytes" "context" + "encoding/base64" "encoding/gob" + "encoding/json" "errors" "fmt" + "io" "reflect" + "strings" "gitlab.com/gitlab-org/gitaly/v14/internal/git" ) @@ -93,6 +97,27 @@ func SerializableError(err error) error { return err } +func serialize(v interface{}) (string, error) { + marshalled, err := json.Marshal(v) + if err != nil { + return "", err + } + return base64.StdEncoding.EncodeToString(marshalled), nil +} + +func deserialize(serialized string, v interface{}) error { + base64Decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(serialized)) + jsonDecoder := json.NewDecoder(base64Decoder) + return jsonDecoder.Decode(v) +} + +func serializeTo(writer io.Writer, v interface{}) error { + base64Encoder := base64.NewEncoder(base64.StdEncoding, writer) + defer base64Encoder.Close() + jsonEncoder := json.NewEncoder(base64Encoder) + return jsonEncoder.Encode(v) +} + // runWithGob runs the specified gitaly-git2go cmd with the request gob-encoded // as input and returns the commit ID as string or an error. func runWithGob(ctx context.Context, binaryPath string, cmd string, request interface{}) (git.ObjectID, error) { diff --git a/internal/git2go/gob_test.go b/internal/git2go/serialization_test.go index e18a10961..f5804f5b0 100644 --- a/internal/git2go/gob_test.go +++ b/internal/git2go/serialization_test.go @@ -64,3 +64,23 @@ func TestSerializableError(t *testing.T) { }) } } + +func TestSerialization_SerializeTo(t *testing.T) { + type testStruct struct { + Contents string `json:"contents"` + } + + var buf bytes.Buffer + + input := testStruct{ + Contents: "foobar", + } + err := serializeTo(&buf, &input) + require.NoError(t, err) + require.NotZero(t, buf.Len()) + + var output testStruct + err = deserialize(buf.String(), &output) + require.NoError(t, err) + require.Equal(t, input, output) +} |