Skip to content

Commit e38688a

Browse files
fix: json parser issues
1 parent 0c89684 commit e38688a

File tree

2 files changed

+14
-64
lines changed

2 files changed

+14
-64
lines changed

cmd/generate.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,25 @@ var generateCmd = &cobra.Command{
1818
1919
Change the file as per your needs and then run the compage generate command to generate the code.`,
2020
Run: func(cmd *cobra.Command, args []string) {
21-
err := GenerateCode()
21+
wD, err := os.Getwd()
22+
if err != nil {
23+
log.Errorf("error while getting the current directory [" + err.Error() + "]")
24+
return
25+
}
26+
// set the project directory environment variable, if this is set, then the project will be generated in this folder
27+
err = os.Setenv("COMPAGE_GENERATED_PROJECT_DIRECTORY", wD)
28+
if err != nil {
29+
log.Errorf("error while setting the project directory [" + err.Error() + "]")
30+
return
31+
}
32+
33+
err = GenerateCode()
2234
cobra.CheckErr(err)
2335
},
2436
}
2537

2638
func init() {
2739
rootCmd.AddCommand(generateCmd)
28-
wD, err := os.Getwd()
29-
if err != nil {
30-
log.Errorf("error while getting the current directory [" + err.Error() + "]")
31-
return
32-
}
33-
// set the project directory environment variable, if this is set, then the project will be generated in this folder
34-
err = os.Setenv("COMPAGE_GENERATED_PROJECT_DIRECTORY", wD)
35-
if err != nil {
36-
log.Errorf("error while setting the project directory [" + err.Error() + "]")
37-
return
38-
}
3940
// Here you will define your flags and configuration settings.
4041

4142
// Cobra supports Persistent Flags which will work for this command

internal/converter/converter.go

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,12 @@ import (
77
"github.com/intelops/compage/internal/languages"
88
"github.com/intelops/compage/internal/languages/templates"
99
log "github.com/sirupsen/logrus"
10-
"golang.org/x/exp/maps"
1110
)
1211

13-
// GetNodes converts nodes map to string.
14-
func GetNodes(nodes interface{}) interface{} {
15-
if nodes != nil {
16-
nodesBytes, err := json.Marshal(maps.Values(nodes.(map[string]interface{})))
17-
if err != nil {
18-
log.Errorf("error marshalling nodes: %v", err)
19-
return err
20-
}
21-
return string(nodesBytes)
22-
}
23-
return ""
24-
}
25-
26-
// GetEdges converts an edge map to string.
27-
func GetEdges(edges interface{}) interface{} {
28-
if edges != nil {
29-
edgesBytes, err := json.Marshal(maps.Values(edges.(map[string]interface{})))
30-
if err != nil {
31-
log.Errorf("error marshalling edges: %v", err)
32-
return err
33-
}
34-
return string(edgesBytes)
35-
}
36-
return ""
37-
}
38-
39-
// ConvertMap converts compageJSON structure to {edges: [], nodes:[]}
40-
func ConvertMap(x map[string]interface{}) map[string]interface{} {
41-
// convert key-value-based edges to edges Slice
42-
if x["edges"] != nil {
43-
x["edges"] = maps.Values(x["edges"].(map[string]interface{}))
44-
}
45-
// convert key-value-based nodes to nodes Slice
46-
if x["nodes"] != nil {
47-
x["nodes"] = maps.Values(x["nodes"].(map[string]interface{}))
48-
}
49-
return x
50-
}
51-
5212
// GetCompageJSONForGRPC converts json string to CompageJSON struct
5313
func GetCompageJSONForGRPC(jsonString string) (*core.CompageJSON, error) {
54-
x := map[string]interface{}{}
55-
if err := json.Unmarshal([]byte(jsonString), &x); err != nil {
56-
log.Errorf("error unmarshalling compageJSON: %v", err)
57-
return nil, err
58-
}
59-
convertedX := ConvertMap(x)
60-
convertedXBytes, err1 := json.Marshal(convertedX)
61-
if err1 != nil {
62-
log.Errorf("error marshalling compageJSON: %v", err1)
63-
return nil, err1
64-
}
6514
compageJSON := &core.CompageJSON{}
66-
if err2 := json.Unmarshal(convertedXBytes, compageJSON); err2 != nil {
15+
if err2 := json.Unmarshal([]byte(jsonString), compageJSON); err2 != nil {
6716
log.Errorf("error unmarshalling compageJSON: %v", err2)
6817
return nil, err2
6918
}

0 commit comments

Comments
 (0)