From 6bbb2a0b4092b852c90e39e896e8d8ba6bbfb110 Mon Sep 17 00:00:00 2001 From: pb Date: Tue, 9 Apr 2024 14:38:52 +0200 Subject: [PATCH] removed componentParser to reuse existing methods and json.Unmarshal --- componentParser.go | 86 ----------------------------- graph.go | 132 ++++++++++++++++++++++----------------------- 2 files changed, 64 insertions(+), 154 deletions(-) delete mode 100644 componentParser.go diff --git a/componentParser.go b/componentParser.go deleted file mode 100644 index 09fc338..0000000 --- a/componentParser.go +++ /dev/null @@ -1,86 +0,0 @@ -package main - -import ( - "fmt" - - "cloud.o-forge.io/core/oc-catalog/models" - "github.com/mitchellh/mapstructure" -) - -// This module allows us to : -// - Reduce the size of graph.go -// - apply specific object construction logic depending on the component's type - -// So far the unpacking of json/map data into the models has the caveat of not applying to the nested struct - -func ConstructComputingObject(id string, component_info map[string]interface{}, user_inputs map[string]interface{}) (computing models.ComputingModel) { - - computing.ID = id - err := mapstructure.Decode(component_info,&computing.ComputingNEWModel) - if err != nil { - fmt.Println("Computing : Error unpacking json into objects info") - log.Err(err) - } - - err = mapstructure.Decode(user_inputs,&computing.ComputingNEWModel) - if err != nil { - fmt.Println("Computing : Error unpacking data input into comp object") - log.Err(err).Msg(err.Error()) - } - - return -} - -func ConstructDataObject(id string, component_info map[string]interface{}, user_inputs map[string]interface{}) (data models.DataModel) { - - data.ID = id - err := mapstructure.Decode(component_info,&data.DataNEWModel) - if err != nil { - fmt.Println("Data: Error unpacking json into objects info") - log.Err(err) - } - - err = mapstructure.Decode(user_inputs,&data.DataNEWModel) - if err != nil { - fmt.Println("Data: Error unpacking data input into comp object") - log.Err(err).Msg(err.Error()) - } - - return -} - -func ConstructDatacenterObject(id string, component_info map[string]interface{}, user_inputs map[string]interface{}) (datacenter models.DatacenterModel) { - - datacenter.ID = id - err := mapstructure.Decode(component_info,&datacenter.DatacenterNEWModel) - if err != nil { - fmt.Println("Datacenter: Error unpacking json into objects info") - log.Err(err) - } - - err = mapstructure.Decode(user_inputs,&datacenter.DatacenterNEWModel) - if err != nil { - fmt.Println("Datacenter: Error unpacking data input into comp object") - log.Err(err).Msg(err.Error()) - } - - return -} - -func ConstructStorageObject(id string, component_info map[string]interface{}, user_inputs map[string]interface{}) (storage models.StorageModel) { - - storage.ID = id - err := mapstructure.Decode(component_info,&storage.StorageNEWModel) - if err != nil { - fmt.Println("Storage: Error unpacking json into objects info") - log.Err(err) - } - - err = mapstructure.Decode(user_inputs,&storage.StorageNEWModel) - if err != nil { - fmt.Println("Storage: Error unpacking data input into comp object") - log.Err(err).Msg(err.Error()) - } - - return -} \ No newline at end of file diff --git a/graph.go b/graph.go index 9d5bee8..013788a 100644 --- a/graph.go +++ b/graph.go @@ -84,46 +84,46 @@ func (g *Graph) LoadFrom(workspace string) error { g.GetWorkflowComponents(workspace) - // for _, element := range cells { - // id := element["@id"].(string) - // // Case MXCell - // if _, ok := element["@style"]; ok { - // if _, ok2 := element["@rID"]; ok2 { - // // Resolve elements - // // fmt.Print(id + ": ") - // // fmt.Println(element["@rID"], element["@rType"]) - // // fmt.Println(element) - // dictionnary[id] = element["@rID"].(string) - // g.addElementByType(element) - // } - // } else { - // // Case object : contains user's input through the GUI - // if _, ok := element["mxCell"]; ok { - // // Attribute values + for _, element := range cells { + // id := element["@id"].(string) + // // Case MXCell + // if _, ok := element["@style"]; ok { + // if _, ok2 := element["@rID"]; ok2 { + // // Resolve elements + // // fmt.Print(id + ": ") + // // fmt.Println(element["@rID"], element["@rType"]) + // // fmt.Println(element) + // dictionnary[id] = element["@rID"].(string) + // g.addElementByType(element) + // } + // } else { + // // Case object : contains user's input through the GUI + // if _, ok := element["mxCell"]; ok { + // // Attribute values - // // Extracts the cell ids - // element = element["mxCell"].(map[string]interface{}) - // if _, ok := element["@style"]; ok { - // if _, ok2 := element["@rID"]; ok2 { - // // Resolve elements - // // fmt.Print(id + ": ") - // // fmt.Println(element["@rID"], element["@rType"]) - // // fmt.Println(element) - // dictionnary[id] = element["@rID"].(string) - // g.addElementByType(element) - // } - // } - // } + // // Extracts the cell ids + // element = element["mxCell"].(map[string]interface{}) + // if _, ok := element["@style"]; ok { + // if _, ok2 := element["@rID"]; ok2 { + // // Resolve elements + // // fmt.Print(id + ": ") + // // fmt.Println(element["@rID"], element["@rType"]) + // // fmt.Println(element) + // dictionnary[id] = element["@rID"].(string) + // g.addElementByType(element) + // } + // } + // } - // } - // // register links - // if src, ok := element["@source"]; ok { - // //src = element["@source"].(string) - // idlinks = append(idlinks, Link{Src: src.(string), Dst: element["@target"].(string)}) - // // fmt.Println("Link: " + src.(string) + " " + element["@target"].(string)) - // } + // } + // register links + if src, ok := element["@source"]; ok { + //src = element["@source"].(string) + idlinks = append(idlinks, Link{Src: src.(string), Dst: element["@target"].(string)}) + // fmt.Println("Link: " + src.(string) + " " + element["@target"].(string)) + } - // } + } // translate links for _, link := range idlinks { @@ -133,20 +133,20 @@ func (g *Graph) LoadFrom(workspace string) error { return nil } -func (g *Graph) addElementByType(element map[string]interface{}) { - if element["@rType"] == "data" { - g.AddDataModel(element["@rID"].(string)) - } - if element["@rType"] == "datacenter" { - g.AddDatacenterModel(element["@rID"].(string)) - } - if element["@rType"] == "computing" { - g.AddComputingModel(element["@rID"].(string)) - } - if element["@rType"] == "storage" { - g.AddStorageModel(element["@rID"].(string)) - } -} +// func (g *Graph) addElementByType(element map[string]interface{}) { +// if element["@rType"] == "data" { +// g.AddDataModel(element["@rID"].(string)) +// } +// if element["@rType"] == "datacenter" { +// g.AddDatacenterModel(element["@rID"].(string)) +// } +// if element["@rType"] == "computing" { +// g.AddComputingModel(element["@rID"].(string)) +// } +// if element["@rType"] == "storage" { +// g.AddStorageModel(element["@rID"].(string)) +// } +// } // TODO : extract all the JSON/data processing to a new object that takes // the current graph as an attribute and deals with adding new objects @@ -163,29 +163,21 @@ func (g *Graph) GetWorkflowComponents(workflow string){ if (result.Type != gjson.Null) { result.ForEach(func(id, value gjson.Result) bool{ - component_db, ok := value.Value().(map[string]interface{}) - - if !ok { - fmt.Printf("error getting map from json") - } + comp_id := value.Get("referenceID").Str - comp_id := component_db["referenceID"].(string) - var obj interface{} if (comp_id != "") { - component_map, _ := g.GetComponentById(component_type, comp_id) switch component_type { case "computing": - obj = ConstructComputingObject(id.Str,component_map,component_db) + g.AddComputingModel(comp_id, value) case "data": - obj = ConstructDataObject(id.Str,component_map,component_db) + g.AddDataModel(comp_id, value) case "datacenter": - obj = ConstructDatacenterObject(id.Str,component_map,component_db) + g.AddDatacenterModel(comp_id, value) case "storage": - obj = ConstructStorageObject(id.Str,component_map,component_db) + g.AddStorageModel(comp_id, value) default : logs.Critical("Component type doesn't match a know type : " + component_type) } - g.AddComponentObject(component_type, obj) } return true }) @@ -234,46 +226,50 @@ func (g *Graph) GetComponentById(component_type string, id string) (map[string]i return comp, nil } -func (g *Graph) AddDataModel(id string) error { +func (g *Graph) AddDataModel(id string, user_input gjson.Result) error { var d models.DataModel resp, err := g.ws.Get("v1/data/" + id) if err != nil { return err } json.Unmarshal(resp, &d) + json.Unmarshal([]byte(user_input.Raw),&d.DataNEWModel) g.Datas = append(g.Datas, d) return nil } -func (g *Graph) AddDatacenterModel(id string) error { +func (g *Graph) AddDatacenterModel(id string, user_input gjson.Result) error { var d models.DatacenterModel resp, err := g.ws.Get("v1/datacenter/" + id) if err != nil { return err } json.Unmarshal(resp, &d) + json.Unmarshal([]byte(user_input.Raw),&d.DatacenterNEWModel) g.Datacenters = append(g.Datacenters, d) return nil } -func (g *Graph) AddComputingModel(id string) error { +func (g *Graph) AddComputingModel(id string, user_input gjson.Result) error { var c models.ComputingModel resp, err := g.ws.Get("v1/computing/" + id) if err != nil { return err } json.Unmarshal(resp, &c) + json.Unmarshal([]byte(user_input.Raw),&c.ComputingNEWModel) g.Computings = append(g.Computings, c) return nil } -func (g *Graph) AddStorageModel(id string) error { +func (g *Graph) AddStorageModel(id string, user_input gjson.Result) error { var s models.StorageModel resp, err := g.ws.Get("v1/data/" + id) if err != nil { return err } json.Unmarshal(resp, &s) + json.Unmarshal([]byte(user_input.Raw),&s.StorageNEWModel) g.Storages = append(g.Storages, s) return nil }