Prechádzať zdrojové kódy

Replace json-iter with go-json, update the tests, replace type definitions with alias for json pkg.

Matt Bolt 4 rokov pred
rodič
commit
21aec434b0

+ 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),
 							},

+ 7 - 10
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"
 )
 
@@ -722,11 +719,11 @@ 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
+	// json used here, as jsonitter UnmarshalJSON won't work with 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 +736,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)
 
@@ -854,11 +851,11 @@ 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
+	// json used here, as jsonitter UnmarshalJSON won't work with 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"
 )

+ 5 - 5
pkg/util/json/json.go

@@ -8,10 +8,10 @@ import (
 
 var Marshal = gojson.Marshal
 var Unmarshal = gojson.Unmarshal
+var NewEncoder = json.NewEncoder
+var NewDecoder = json.NewDecoder
 
-type Marshaler json.Marshaler
-type Unmarshaler json.Unmarshaler
-
-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) {