Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Commit

Permalink
perf: improve marshaling speed
Browse files Browse the repository at this point in the history
  • Loading branch information
clambin committed Nov 28, 2022
1 parent 524eaae commit 227ff86
Show file tree
Hide file tree
Showing 44 changed files with 1,076 additions and 1,272 deletions.
20 changes: 7 additions & 13 deletions annotation/annotation.go → annotation.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package annotation
package simplejson

import (
"encoding/json"
"github.com/clambin/simplejson/v3/common"
"time"
)

// Request is a query for annotation.
type Request struct {
// AnnotationRequest is a query for annotation.
type AnnotationRequest struct {
Annotation RequestDetails `json:"annotation"`
Args
}

// Args contains arguments for the Annotations endpoint.
type Args struct {
common.Args
}

// RequestDetails specifies which annotation should be returned.
type RequestDetails struct {
Name string `json:"name"`
Expand All @@ -25,13 +19,13 @@ type RequestDetails struct {
Query string `json:"query"`
}

// UnmarshalJSON unmarshalls a Request from JSON
func (r *Request) UnmarshalJSON(b []byte) (err error) {
type Request2 Request
// UnmarshalJSON unmarshalls a AnnotationRequest from JSON
func (r *AnnotationRequest) UnmarshalJSON(b []byte) (err error) {
type Request2 AnnotationRequest
var c Request2
err = json.Unmarshal(b, &c)
if err == nil {
*r = Request(c)
*r = AnnotationRequest(c)
}
return err
}
Expand Down
15 changes: 6 additions & 9 deletions annotation/annotation_test.go → annotation_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package annotation_test
package simplejson

import (
"encoding/json"
"flag"
"github.com/clambin/simplejson/v3/annotation"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"strings"
"testing"
"time"
)

var update = flag.Bool("update", false, "update .golden files")

func TestAnnotation_MarshalJSON(t *testing.T) {
ann := annotation.Annotation{
ann := Annotation{
Time: time.Date(2022, time.January, 23, 0, 0, 0, 0, time.UTC),
Title: "foo",
Text: "bar",
Tags: []string{"A", "B"},
Request: annotation.RequestDetails{
Request: RequestDetails{
Name: "snafu",
Datasource: "datasource",
Enable: true,
Expand All @@ -31,7 +28,7 @@ func TestAnnotation_MarshalJSON(t *testing.T) {
body, err := json.Marshal(ann)
require.NoError(t, err)

gp := filepath.Join("testdata", t.Name()+"_1.golden")
gp := filepath.Join("testdata", strings.ToLower(t.Name()), "1.golden")
if *update {
t.Logf("updating golden file for %s", t.Name())
err = os.WriteFile(gp, body, 0644)
Expand All @@ -54,7 +51,7 @@ func TestAnnotation_MarshalJSON(t *testing.T) {
body, err = json.Marshal(ann)
require.NoError(t, err)

gp = filepath.Join("testdata", t.Name()+"_2.golden")
gp = filepath.Join("testdata", strings.ToLower(t.Name()), "2.golden")
if *update {
t.Logf("updating golden file for %s", t.Name())
err = os.WriteFile(gp, body, 0644)
Expand Down
2 changes: 1 addition & 1 deletion common/common.go → common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package simplejson

import "time"

Expand Down
41 changes: 0 additions & 41 deletions data/response.go

This file was deleted.

183 changes: 0 additions & 183 deletions dataset/dataset.go

This file was deleted.

Loading

0 comments on commit 227ff86

Please sign in to comment.