Hi @skillian
I will rewrite the issue. That’s one of my first questions in golang and I didn’t pay attention to a few things.
-
I need to convert the JSON data from variable json_response
to the same structure as variable json_file
. Some fields in json_response
must be ignored and just select the same fiels as json_file
.
-
When both “variables” will contain the same structure I must compare each other with some kind of custom function.
compare(json_file, json_response) -> old features on server (to be deleted in the server)
compare(json_response, json_file) -> new features in file (to be added in the server)
These are the variables that I’m getting in JSON:
//variable json data
json_file := `{
"environments": [
{
"evironment": "dev",
"features": [
{ "name": "featA", "enabled": true },
{ "name": "featB", "enabled": true },
{ "name": "featD", "enabled": true }
]
}
]
}`
// server's reponse
json_response := `{
"environments": ["development", "production"],
"features": [{
"type": "release",
"name": "featA",
"stale": false,
"environments": [{
"name": "dev",
"enabled": true,
"type": "dev",
"sortOrder": 100
}, {
"name": "stag",
"enabled": false,
"type": "stag",
"sortOrder": 200
}]
}, {
"type": "release",
"name": "featC",
"stale": false,
"environments": [{
"name": "dev",
"enabled": true,
"type": "dev",
"sortOrder": 100
}, {
"name": "stag",
"enabled": false,
"type": "stag",
"sortOrder": 200
}]
}],
"members": 1,
"version": 1
}`
Both variables can contain different amount of features (different size), here the problem to compare each other though some build-in map function like DeepEqual
or similar.
What I tried so far:
1- I defined the struct for both variables so that I can work with structs.
2- Unmarshall the data from the variables to use structs.
3- I Created a slice of struct so that I can append info there.
4- For loop to iterate over the struct to get the info from the response andcopy it into the slice.
// define structures for both variables
// JSON structure for json_file
type Features struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
}
type Environment struct {
Environment string `json:"environment"`
Features []Features `json:"features"`
}
type File struct {
File []Environment `json:"environments"`
}
// JSON structure json_response
type Response struct {
Name string `json:"name"`
Environments []string `json:"environments"`
Features []struct {
Type string `json:"type"`
Name string `json:"name"`
Stale bool `json:"stale"`
Environments []struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
Type string `json:"type"`
SortOrder int `json:"sortOrder"`
} `json:"environments"`
} `json:"features"`
Members int `json:"members"`
Version int `json:"version"`
}
func main() {
// variables type struct
var json_file_struct File
var json_response_struct Response
// unmarshall the JSON data to store it into the variables type struct
err := json.Unmarshal([]byte(json_file), &json_file_struct)
if err != nil {
fmt.Println(err)
}
err := json.Unmarshal([]byte(json_response), &json_response_struct)
if err != nil {
fmt.Println(err)
}
// Iterating the struct variable json_response_struct to get the same field's info as json_file_struct so that I can compare both later.
// For that I created a slice from Fields struct so that i can append info there.
var variable []File
for _, e := range json_response_struct.features {
for _, i := range e.environments {
test = append(test, File{environments:{[{environment:i.name, features:[{name: i.name, enabled: i.enabled}]}]}})
}
}
// Rest of the code to create the comparision between both structs so that i can create/delete those feature in the server
}
I tried to avoid the use of reflect
in the for loop
as i think it’s not necessary in this case.
The previous loop it’s not working. In test=append(...)
it’s giving a syntax expected expression
error. If you can suggest a better way to achieve what i’m trying to do i’d really appreciate it.