Forráskód Böngészése

Merge pull request #1080 from kubecost/bolt/go-json

Go-JSON Replacement
Matt Bolt 4 éve
szülő
commit
ca858ec5fa

+ 1 - 1
go.mod

@@ -22,9 +22,9 @@ require (
 	github.com/aws/aws-sdk-go-v2/service/sts v1.14.0
 	github.com/davecgh/go-spew v1.1.1
 	github.com/getsentry/sentry-go v0.6.1
+	github.com/goccy/go-json v0.9.4
 	github.com/gofrs/uuid v4.2.0+incompatible // indirect
 	github.com/google/uuid v1.3.0
-	github.com/json-iterator/go v1.1.11
 	github.com/jszwec/csvutil v1.2.1
 	github.com/julienschmidt/httprouter v1.3.0
 	github.com/lib/pq v1.2.0

+ 2 - 0
go.sum

@@ -213,6 +213,8 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
 github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
 github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
 github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
+github.com/goccy/go-json v0.9.4 h1:L8MLKG2mvVXiQu07qB6hmfqeSYQdOnqPot2GhsIwIaI=
+github.com/goccy/go-json v0.9.4/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
 github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
 github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
 github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=

+ 28 - 28
pkg/kubecost/allocation_test.go

@@ -1,13 +1,13 @@
 package kubecost
 
 import (
-	"encoding/json"
 	"fmt"
 	"math"
 	"testing"
 	"time"
 
 	"github.com/kubecost/cost-model/pkg/util"
+	"github.com/kubecost/cost-model/pkg/util/json"
 )
 
 func TestAllocation_Add(t *testing.T) {
@@ -2623,9 +2623,9 @@ func TestAllocationSetRange_Start(t *testing.T) {
 			name: "Single allocation",
 			arg: &AllocationSetRange{
 				allocations: []*AllocationSet{
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"a": &Allocation{
+							"a": {
 								Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
 							},
 						},
@@ -2639,12 +2639,12 @@ func TestAllocationSetRange_Start(t *testing.T) {
 			name: "Two allocations",
 			arg: &AllocationSetRange{
 				allocations: []*AllocationSet{
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"a": &Allocation{
+							"a": {
 								Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
 							},
-							"b": &Allocation{
+							"b": {
 								Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
 							},
 						},
@@ -2658,16 +2658,16 @@ func TestAllocationSetRange_Start(t *testing.T) {
 			name: "Two AllocationSets",
 			arg: &AllocationSetRange{
 				allocations: []*AllocationSet{
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"a": &Allocation{
+							"a": {
 								Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
 							},
 						},
 					},
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"b": &Allocation{
+							"b": {
 								Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
 							},
 						},
@@ -2711,9 +2711,9 @@ func TestAllocationSetRange_End(t *testing.T) {
 			name: "Single allocation",
 			arg: &AllocationSetRange{
 				allocations: []*AllocationSet{
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"a": &Allocation{
+							"a": {
 								End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
 							},
 						},
@@ -2727,12 +2727,12 @@ func TestAllocationSetRange_End(t *testing.T) {
 			name: "Two allocations",
 			arg: &AllocationSetRange{
 				allocations: []*AllocationSet{
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"a": &Allocation{
+							"a": {
 								End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
 							},
-							"b": &Allocation{
+							"b": {
 								End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
 							},
 						},
@@ -2746,16 +2746,16 @@ func TestAllocationSetRange_End(t *testing.T) {
 			name: "Two AllocationSets",
 			arg: &AllocationSetRange{
 				allocations: []*AllocationSet{
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"a": &Allocation{
+							"a": {
 								End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
 							},
 						},
 					},
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"b": &Allocation{
+							"b": {
 								End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
 							},
 						},
@@ -2798,9 +2798,9 @@ func TestAllocationSetRange_Minutes(t *testing.T) {
 			name: "Single allocation",
 			arg: &AllocationSetRange{
 				allocations: []*AllocationSet{
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"a": &Allocation{
+							"a": {
 								Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
 								End:   time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
 							},
@@ -2815,13 +2815,13 @@ func TestAllocationSetRange_Minutes(t *testing.T) {
 			name: "Two allocations",
 			arg: &AllocationSetRange{
 				allocations: []*AllocationSet{
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"a": &Allocation{
+							"a": {
 								Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
 								End:   time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
 							},
-							"b": &Allocation{
+							"b": {
 								Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
 								End:   time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
 							},
@@ -2836,17 +2836,17 @@ func TestAllocationSetRange_Minutes(t *testing.T) {
 			name: "Two AllocationSets",
 			arg: &AllocationSetRange{
 				allocations: []*AllocationSet{
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"a": &Allocation{
+							"a": {
 								Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
 								End:   time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
 							},
 						},
 					},
-					&AllocationSet{
+					{
 						allocations: map[string]*Allocation{
-							"b": &Allocation{
+							"b": {
 								Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
 								End:   time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
 							},

+ 5 - 12
pkg/kubecost/asset_unmarshal.go

@@ -6,9 +6,6 @@ import (
 	"reflect"
 	"time"
 
-	// gojson is default golang json, required for RawMessage decoding
-	gojson "encoding/json"
-
 	"github.com/kubecost/cost-model/pkg/util/json"
 )
 
@@ -721,12 +718,10 @@ func (as *AssetSet) MarshalJSON() ([]byte, error) {
 
 // Unmarshals a marshaled AssetSet json into AssetSetResponse
 func (asr *AssetSetResponse) UnmarshalJSON(b []byte) error {
-
-	// gojson used here, as jsonitter UnmarshalJSON won't work with RawMessage
-	var assetMap map[string]*gojson.RawMessage
+	var assetMap map[string]*json.RawMessage
 
 	// Partial unmarshal to map of json RawMessage
-	err := gojson.Unmarshal(b, &assetMap)
+	err := json.Unmarshal(b, &assetMap)
 	if err != nil {
 		return err
 	}
@@ -739,7 +734,7 @@ func (asr *AssetSetResponse) UnmarshalJSON(b []byte) error {
 	return nil
 }
 
-func (asr *AssetSetResponse) RawMessageToAssetSetResponse(assetMap map[string]*gojson.RawMessage) error {
+func (asr *AssetSetResponse) RawMessageToAssetSetResponse(assetMap map[string]*json.RawMessage) error {
 
 	newAssetMap := make(map[string]Asset)
 
@@ -853,12 +848,10 @@ func (asr *AssetSetResponse) RawMessageToAssetSetResponse(assetMap map[string]*g
 }
 
 func (asrr *AssetSetRangeResponse) UnmarshalJSON(b []byte) error {
-
-	// gojson used here, as jsonitter UnmarshalJSON won't work with RawMessage
-	var assetMapList []map[string]*gojson.RawMessage
+	var assetMapList []map[string]*json.RawMessage
 
 	// Partial unmarshal to map of json RawMessage
-	err := gojson.Unmarshal(b, &assetMapList)
+	err := json.Unmarshal(b, &assetMapList)
 	if err != nil {
 		return err
 	}

+ 2 - 1
pkg/kubecost/asset_unmarshal_test.go

@@ -1,7 +1,8 @@
 package kubecost
 
 import (
-	"encoding/json"
+	"github.com/kubecost/cost-model/pkg/util/json"
+
 	"testing"
 	"time"
 )

+ 8 - 7
pkg/util/json/json.go

@@ -3,15 +3,16 @@ package json
 import (
 	"encoding/json"
 
-	jsoniter "github.com/json-iterator/go"
+	gojson "github.com/goccy/go-json"
 )
 
-var Marshal = jsoniter.ConfigCompatibleWithStandardLibrary.Marshal
-var Unmarshal = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal
+var Marshal = gojson.Marshal
+var Unmarshal = gojson.Unmarshal
 
-type Marshaler json.Marshaler
-type Unmarshaler json.Unmarshaler
+var NewEncoder = json.NewEncoder
+var NewDecoder = json.NewDecoder
 
-type RawMessage json.RawMessage
+type Marshaler = json.Marshaler
+type Unmarshaler = json.Unmarshaler
 
-var NewDecoder = json.NewDecoder
+type RawMessage = json.RawMessage

+ 1 - 1
test/clusterinfo_test.go

@@ -1,10 +1,10 @@
 package test
 
 import (
-	"encoding/json"
 	"testing"
 
 	"github.com/kubecost/cost-model/pkg/prom"
+	"github.com/kubecost/cost-model/pkg/util/json"
 )
 
 func TestClusterInfoLabels(t *testing.T) {