Selaa lähdekoodia

Partial AssetSet implementation

Kaelan Patel 4 vuotta sitten
vanhempi
sitoutus
590a85329f
2 muutettua tiedostoa jossa 76 lisäystä ja 1 poistoa
  1. 47 1
      pkg/kubecost/asset.go
  2. 29 0
      pkg/kubecost/asset_unmarshal_test.go

+ 47 - 1
pkg/kubecost/asset.go

@@ -9,6 +9,8 @@ import (
 	"sync"
 	"time"
 
+	gojson "encoding/json"
+
 	"github.com/kubecost/cost-model/pkg/log"
 	"github.com/kubecost/cost-model/pkg/util/json"
 )
@@ -2029,7 +2031,14 @@ func (n *Node) UnmarshalJSON(b []byte) error {
 		return err
 	}
 
-	fmap := f.(map[string]interface{})
+	n.InterfaceToNode(f)
+
+	return nil
+}
+
+func (n *Node) InterfaceToNode(itf interface{}) error {
+
+	fmap := itf.(map[string]interface{})
 
 	// parse properties map to AssetProperties
 	fproperties := fmap["properties"].(map[string]interface{})
@@ -2103,6 +2112,7 @@ func (n *Node) UnmarshalJSON(b []byte) error {
 	}
 
 	return nil
+
 }
 
 // String implements fmt.Stringer
@@ -2909,6 +2919,42 @@ func (as *AssetSet) MarshalJSON() ([]byte, error) {
 	return json.Marshal(as.assets)
 }
 
+func (as *AssetSet) UnmarshalJSON(b []byte) error {
+
+	var assetMap map[string]*gojson.RawMessage
+
+	err := gojson.Unmarshal(b, &assetMap)
+	if err != nil {
+		return err
+	}
+
+	var newAssets map[string]Asset
+
+	for _, rawMessage := range assetMap {
+
+		var f interface{}
+
+		err := json.Unmarshal(*rawMessage, &f)
+		if err != nil {
+			return err
+		}
+		fmt.Println(f)
+		fmap := f.(map[string]interface{})
+
+		switch t := fmap["type"]; t {
+		case "Node":
+			var n Node
+			n.InterfaceToNode(f)
+
+			fmt.Println(n)
+		}
+	}
+
+	fmt.Println(assetMap)
+
+	return nil
+}
+
 func (as *AssetSet) Set(asset Asset, aggregateBy []string) error {
 	if as.IsEmpty() {
 		as.Lock()

+ 29 - 0
pkg/kubecost/asset_unmarshal_test.go

@@ -2,6 +2,7 @@ package kubecost
 
 import (
 	"encoding/json"
+	"fmt"
 	"testing"
 	"time"
 )
@@ -110,3 +111,31 @@ func TestNode_Unmarshal(t *testing.T) {
 	}
 
 }
+
+func TestAssetset_Unmarshal(t *testing.T) {
+
+	var s time.Time
+	var e time.Time
+	unmarshalWindow := NewWindow(&s, &e)
+
+	node1 := NewNode("node1", "cluster1", "provider1", *unmarshalWindow.start, *unmarshalWindow.end, unmarshalWindow)
+	node2 := NewNode("node2", "cluster1", "provider1", *unmarshalWindow.start, *unmarshalWindow.end, unmarshalWindow)
+	node3 := NewNode("node3", "cluster1", "provider1", *unmarshalWindow.start, *unmarshalWindow.end, unmarshalWindow)
+
+	assetList := []Asset{node1, node2, node3}
+
+	assetset1 := NewAssetSet(s, e, assetList...)
+
+	//fmt.Println(assetset1)
+
+	bytes, _ := json.Marshal(assetset1)
+
+	//print(string(bytes))
+
+	var thisassetset AssetSet
+	assetset2 := &thisassetset
+
+	err := json.Unmarshal(bytes, assetset2)
+
+	fmt.Println(err)
+}