I am new to golang. I have a json file with nested structures that I want to parse and populate.
I am trying to use mapstructure to try and populate. I am able to do it for simple structures. But when it comes to array for dictionaries(key:struct). The map[string]interface{} seems to fail with runtime error: index out of range.
I tried to do the following for the json example below.
type Window struct {
loc []int
wrtc string
label string
}
type View struct {
windows []Window
}
type Views struct {
views []View
}
type Desktop struct {
views []Views `mapstructure:views`
rotation_speed string `mapstructure:"rotationSpeed" json:rotationSpeed"`
}
func main() {
file, _ := ioutil.ReadFile("test.json")
data := Desktop{}
_ = json.Unmarshal([]byte(file), &data)
fmt.Println("data: ", data.views[0])
}
{
"desktop": {
"view": [{// configs for view1
"random_id1": {
"loc": [0,0,640,360],
"wrtc": "some string",
"label": "window 1"
},
"random_id213443": {
"loc": [640,360,1280,720],
"wrtc": "some string blah",
"label": "window 2"
},
// more windows with random ids....
},
{
// configs for view2...
}
],
"rotationSpeed": 30
}
Since the window id is random I am not able to define it in a struct.
I tried using mapstructure:",squash" but that seems to fail as well.
I appreciate any help with this.
#Burak Serdar is right
You don't need mapstructure. JSON unmarshaling can deal with this.
you code have many error, like struct, uppercase, 'views' etc..
follow is a demo:
package main
import (
"encoding/json"
"fmt"
)
var data = `
{
"desktop":{
"view":[
{
"random_id1_1":{
"loc":[
0,
0,
640,
360
],
"wrtc":"some string",
"label":"window 1"
},
"random_id1_2":{
"loc":[
640,
360,
1280,
720
],
"wrtc":"some string blah",
"label":"window 2"
}
},
{
"random_id2_1":{
"loc":[
0,
0,
640,
360
],
"wrtc":"some string",
"label":"window 1"
},
"random_id2_2":{
"loc":[
640,
360,
1280,
720
],
"wrtc":"some string blah",
"label":"window 2"
}
}
],
"rotationSpeed":30
}
}
`
type Window struct {
Loc []int
Wrtc string
Label string
}
type Desktop struct {
View []map[string]Window
Rotation_speed int `json:"rotationSpeed" mapstructure:"rotationSpeed"`
}
type Config struct {
Desktop Desktop
}
func main() {
c := Config{}
json.Unmarshal([]byte(data), &c)
fmt.Println("json.Unmarshal: ", c)
}
json.Unmarshal: {{[map[random_id1_1:{[0 0 640 360] some string window 1} random_id1_2:{[640 360 1280 720] some s
tring blah window 2}] map[random_id2_1:{[0 0 640 360] some string window 1} random_id2_2:{[640 360 1280 720] some
string blah window 2}]] 30}}
also you can use mapstructure by "remain", if you want View struct
type Window struct {
Loc []int
Wrtc string
Label string
}
type View struct {
Windows map[string]Window `mapstructure:",remain"`
}
type Desktop struct {
View []View
Rotation_speed int `json:"rotationSpeed" mapstructure:"rotationSpeed"`
}
type Config struct {
Desktop Desktop
}
func main() {
c2 := Config{}
m := map[string]interface{}{}
_ = json.Unmarshal([]byte(data), &m)
mapstructure.Decode(m, &c2)
fmt.Println("mapstructure: ", c2)
}
Related
I need help for transforming this input map into the output map. I try with switch/case and for but I didn't succeed it. Thanks a lot !
Input :
Values{
"toto_voiture_brand": Ad{
"CITROEN": "CITROEN",
},
"toto_voiture_model": Ad{
"CITROEN_toto": "C3",
},
"toto_moto_brand": Ad{
"KAWASAKI": "KAWASAKI",
},
"toto_moto_model": Ad{
"KAWASAKI_tata": "Ninja 1000 SX",
},
"toto_camion_brand": Ad{
"RENAULT": "RENAULT",
"PEUGEOT": "PEUGEOT",
},
"toto_camion_model": Ad{
"RENAULT_toto": "J5",
"PEUGEOT_tata": "255",
},
},
}
Output
Values{
"toto_voiture_model": {
"Citroen": {
{Value: "C3"},
},
},
"toto_moto_model": {
"Kawasaki": {
{Value: "Ninja 1000 SX"},
},
},
"toto_camion_model": {
"RENAULT": {
{Value: "J5"},
},
"PEUGEOT": {
{Value: "255"},
},
},
}
I've tried with switch case and loop for and map. But I don't have the result attendee, I didn't found how to match every map, key and value. Thanks a lot
I should have managed what you need with the following code:
package main
import (
"encoding/json"
"fmt"
"strings"
)
type Output struct {
Value string `json:"Value"`
}
func main() {
// declare output
output := make(map[string]map[string]Output, 0)
// input
input := make(map[string]map[string]string, 0)
input["toto_voiture_brand"] = map[string]string{
"CITROEN": "CITROEN",
}
input["toto_voiture_model"] = map[string]string{
"CITROEN_toto": "C3",
}
input["toto_moto_model"] = map[string]string{
"KAWASAKI_tata": "Ninja 1000 SX",
}
input["toto_camion_model"] = map[string]string{
"RENAULT_toto": "J5",
"PEUGEOT_tata": "255",
}
// transformation
for k, v := range input {
if strings.HasSuffix(k, "_model") {
tempMap := make(map[string]Output, len(v))
for kk, vv := range v {
key := strings.Split(kk, "_")[0]
tempMap[key] = Output{
Value: vv,
}
}
output[k] = tempMap
}
}
data, _ := json.MarshalIndent(&output, "", "\t")
fmt.Println(string(data))
}
I put some comments within the code just to separate sections. The first two parts are only supposed to define your input and output variables.
The section starting with // transformation is a good candidate to become a function but I preferred to leave it within the main function for demo purposes. Let me recap what's happens in the loop:
You range over the entries of your input variable
If the key has the suffix _model, you take it into consideration
You define a locally-scoped map (called tempMap) of type map[string]Output with the right number of elements that we're gonna add
You range over the v variable (that's why we're dealing with nested maps)
For each item, you're gonna add an entry to the tempMap
At the end of the nested loop, you add an entry to the parent map (output)
The last part is only for printing a beautiful JSON that can be easily read and checked.
Note that this code is simplified just to show off how to achieve your goal, adjust it before putting it into production.
Let me know if this helps, thanks!
I have a nested structure definition flatened into a slice (this hypothesis is not negociable, I have to deal with it) :
type element struct {
Name string
Type string // can be basic type string (eg "uint8")
// or a definition.ID for nested struct
}
type definition struct {
ID string
Elements []element
}
type definitions []definition
allDefs := definitions{
{
ID: "root",
Elements: []element{
{
Name: "SubStruct",
Type: "sub1", // this is reference to another definition
},
{
Name: "VarU32",
Type: "uint32", // this is a basic type string representation
},
},
},
{
ID: "sub1",
Elements: []element{
{
Name: "VarU8",
Type: "uint8",
},
{
Name: "VarU16",
Type: "uint16",
},
},
},
}
And I would like to build the corresponding nested struct using a recursive method (don't know the real depth) :
func (defs definitions) getStruct(id string) interface{} {
for _, def := range defs {
if def.ID == id { // found a reference to definition
fields := []reflect.StructField{}
for _, e := range def.Elements {
s := defs.getStruct(e.Type)
fields = append(fields, reflect.StructField{
Name: e.Name,
Type: reflect.TypeOf(s),
Tag: "",
})
}
return reflect.New(reflect.StructOf(fields)).Interface()
}
}
// didn't found a reference to a definition, it should be a basic type
if id == "uint8" {
return reflect.ValueOf(uint8(0)).Interface()
}
if id == "uint16" {
return reflect.ValueOf(uint16(0)).Interface()
}
if id == "uint32" {
return reflect.ValueOf(uint32(0)).Interface()
}
// ignore the fact id could be anything else for this example
return nil
}
root := allDefs.getStruct("root")
// using spew to inspect variable : github.com/davecgh/go-spew/spew
spew.Dump(root)
// (*struct { SubStruct *struct { VarU8 uint8; VarU16 uint16 }; VarU32 uint32 })(0xc00004e400)({
// SubStruct: (*struct { VarU8 uint8; VarU16 uint16 })(<nil>),
// VarU32: (uint32) 0
// })
Then I want to be able to assign some variable's values :
rootValElem := reflect.ValueOf(root).Elem()
rootValElem.FieldByName("VarU32").SetUint(1)
spew.Dump(root)
// (*struct { SubStruct *struct { VarU8 uint8; VarU16 uint16 }; VarU32 uint32 })(0xc00004e400)({
// SubStruct: (*struct { VarU8 uint8; VarU16 uint16 })(<nil>),
// VarU32: (uint32) 1
// })
Setting root level variable value is OK, but I am unable to entering Sub level and assign any variable, no matter how I play with reflect ValueOf()/Elem()/Addr()..., here are some examples :
fieldSub := rootValElem.FieldByName("SubStruct")
// fieldSub.Kind() : ptr
// fieldSub.CanSet() : true
subVal := reflect.ValueOf(fieldSub)
// subVal.Kind() : struct
// subVal.CanSet() : false
fieldU16 := subVal.FieldByName("VarU16")
// fieldU16.Kind() : invalid
// fieldU16.CanSet() : false
fieldSubElem := fieldSub.Elem()
// fieldSubElem.Kind() : invalid
// fieldSubElem.CanSet() : false
fieldU16 := fieldSubElem.FieldByName("VarU16")
// panic: reflect: call of reflect.Value.FieldByName on zero Value
Thanks to mkopriva comment above, I understand now my mistake : fieldSub is a pointer and I should check if nil, then allocate the struct value before trying to get Elem() then Field :
fieldSub := rootValElem.FieldByName("SubStruct")
// fieldSub.Kind() : ptr
// fieldSub.CanSet() : true
// fieldSub.IsNil() : true
if fieldSub.IsNil() && fieldSub.CanSet() {
fieldSub.Set(reflect.New(fieldSub.Type().Elem()))
}
fieldU8 := fieldSub.Elem().FieldByName("VarU8")
// fieldSub.Kind() : uint8
// fieldSub.CanSet() : true
if fieldU8.CanSet() {
fieldU8.SetUint(8)
}
spew.Dump(root)
// (*struct { SubStruct *struct { VarU8 uint8; VarU16 uint16 }; VarU32 uint32 })(0xc00008a3f0)({
// SubStruct: (*struct { VarU8 uint8; VarU16 uint16 })(0xc0000ac430)({
// VarU8: (uint8) 8,
// VarU16: (uint16) 0
// }),
// VarU32: (uint32) 1
// })
I am working on unit test for a restAPI implementation in Golang.
I need to pass an array of object into url.
Here is an example of struct I have:
type version struct {
Name string `json:"name"`
Ver string `json:"ver"`
}
type event struct {
ID string `json:"id"`
Title string `json:"Title"`
Description string `json:"Description"`
Versions []version `json:"versions"`
}
The sample json input i tested in postman will be look like this one
{
"id": "101",
"title": "This is simple Golang title for testing!",
"Description":"Sample code for REST api implementation in Golang 2021!",
"versions": [
{
"name": "pingPong",
"ver": "10.2"
},
{
"name": "Ninja",
"ver": "10.24"
}
]
}
My question is that how can i pass an array of objects as URL parameters.
I expect to have something like below but not how to fill the ending part i highlighted by the ...
url?ID=20&Title=urlTitle&Description=UrlDescription&...
I don't know how you want the URL like, so I wrote it myself in a way that you can change it any way you want, And let me add that I don't know how many versions you have, so I wrote in such a way that no matter how many versions you have, it can handle it.
package main
import (
"fmt"
"strings"
"encoding/json"
)
var jsonData string =
`{
"id": "101",
"title": "This is simple Golang title for testing!",
"Description":"Sample code for REST api implementation in Golang 2021!",
"versions": [
{
"name": "pingPong",
"ver": "10.2"
},
{
"name": "Ninja",
"ver": "10.24"
}
]
}`
type (
Event struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Versions []Version `json:"versions"`
}
Version struct {
Name string `json:"name"`
Ver string `json:"ver"`
}
)
func fillVersions(event *Event, baseUrl string) string {
var finalUrl string = baseUrl
for index, value := range event.Versions {
restUrl := fmt.Sprintf("Version%d=%s-%s", index + 1, value.Name, value.Ver)
finalUrl = fmt.Sprintf(
finalUrl + "%s" + "&",
restUrl,
)
}
return strings.TrimRight(finalUrl, "&")
}
func main() {
var event Event
json.Unmarshal([]byte(jsonData), &event)
baseUrl := fmt.Sprintf(
"https://test.com/test?Id=%s&Title=%s&Description=%s&",
event.Id,
event.Title,
event.Description,
)
finalUrl := fillVersions(&event, baseUrl)
fmt.Println(finalUrl)
}
The output of the program is as follows:
https://test.com/test?Id=101&Title=This is simple Golang title for testing!&Description=Sample code for REST api implementation in Golang 2021!&Version1=pingPong-10.2&Version2=Ninja-10.24
I would also like to say that the last & will be removed, If you don't want to do this, Remove the following line and write as follows (also remove the strings library from the import scope):
return strings.TrimRight(finalUrl, "&") // remove this
return finalUrl // add this
I'm having trouble with a List containing Text rows that have text that wraps, on the Mac.
Here's the code:
struct ContentView: View {
var messages = [
"This is a long piece of text that's going to need to be wrapped to fit into the view.",
"This is another long piece of text that's going to need to be wrapped to fit into the view."
]
var body: some View {
List(messages, id: \.self) { message in
Text(message)
.lineLimit(nil)
.border(Color.blue)
.fixedSize(horizontal: false, vertical: true)
// Also tried .fixedSize(horizontal: true, vertical: false)
// which seems more correct, but doesn't wrap the text at all
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
When I run this on iOS, the result is what I'd expect:
On the Mac, however, it seems like the underlying table that SwiftUI is creating isn't adjusting the row heights to fit the wrapped content:
I've reported this as a bug (FB7421021) but asking here in case anyone else struggling with this.
Thanks
Here is adopted approach from the similar use-case which works for me. Hope it would be useful for somebody else.
struct TestWrappedText: View {
var messages = [
"This is a long piece of text that's going to need to be wrapped to fit into the view.",
"This is another long piece of text that's going to need to be wrapped to fit into the view."
]
#State var alignedHeight: [CGFloat] = [0, 0]
var body: some View {
List(0 ..< messages.count) { i in
Text(self.messages[i])
.border(Color.blue)
.fixedSize(horizontal: false, vertical: true)
.modifier(ViewHeightKey())
.onPreferenceChange(ViewHeightKey.self) {
if self.alignedHeight[i] != $0 {
self.alignedHeight[i] = $0
}
}
.frame(height: self.alignedHeight[i])
}
}
}
struct ViewHeightKey: PreferenceKey {
static var defaultValue: CGFloat { 0 }
static func reduce(value: inout Value, nextValue: () -> Value) {
value = nextValue()
}
}
extension ViewHeightKey: ViewModifier {
func body(content: Content) -> some View {
return content.background(GeometryReader { proxy in
Color.clear.preference(key: Self.self, value: proxy.size.height)
})
}
}
So I have an Struct that holds data that has a AddedByUser which links to my User Struct.
What I want to be able to do it remove the UserLevel from the AddedByUser
Now I want to be able to do it from this function only, so using the json:"-" is not an option. That would remove it from all json output. I only want to remove it form this one function.
I should also say that these are Gorm models and when I have been trying to remove the 10 option (UserLevels) it only removes the outer data set not the UserLevel from all of the data.
{
"ID": 1,
"CreatedAt": "2019-01-08T16:33:09.514711Z",
"UpdatedAt": "2019-01-08T16:33:09.514711Z",
"DeletedAt": null,
"UUID": "00000000-0000-0000-0000-000000000000",
"Title": "title000",
"Information": "info999",
"EventDate": "2006-01-02T15:04:05Z",
"AddedByUser": {
"ID": 2,
"CreatedAt": "2019-01-08T15:27:52.435397Z",
"UpdatedAt": "2019-01-08T15:27:52.435397Z",
"DeletedAt": null,
"UUID": "b019df80-a7e4-4397-814a-795e7e84b4ca",
"Firstname": "Me",
"Surname": "admin",
"Password": "....",
"Email": "admin#email.co.uk",
"UserLevel": {
"ID": 0,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z",
"DeletedAt": null,
"LevelTitle": "",
"UserLevel": null
},
So this is what I have tried,
data := []models.MyData{}
data = append(data[0:2])
I have about 14 results, with out the append it loads all the results but with this is only loads two results. The idea was to remove either UpdateAt or Title. As I am not sure if the gorm model information is all 0 or if the slice sees them as 0,1,2,3,4 etc.
I have also tried to range over the slice of models, while I can access each of the sections, I can not seem to find a simple method to remove data by name from a struct? Maps seem to have that but not structs which I am not sure why?
Thanks.
UPDATE
This is the model I am using:
//Model
type MyData struct {
gorm.Model
UUID uuid.UUID
Title string
Information string
EventDate time.Time
AddedByUser Users `gorm:"ForeignKey:added_by_user_fk"`
AddedByUserFK uint
}
//Users Model
type Users struct {
gorm.Model
UUID uuid.UUID
Firstname string
Surname string
Password string
Email string
UserLevel UserLevels `gorm:"ForeignKey:user_level_fk" json:",omitempty"`
UserLevelFK uint
}
As mentioned in the comments, you cannot remove fields from a struct value, because that would yield a value of a different type.
However, you can set fields to their zero value. Combined with the omitempty JSON tag, you can exclude fields from the JSON encoding. To make this work properly, you have to change the UserLevel field to a pointer type (otherwise you end up with empty objects in the JSON document).
Types shortened for brevity:
package main
import (
"encoding/json"
"fmt"
)
type MyData struct {
Title string
AddedByUser Users
}
type Users struct {
ID int
UserLevel *UserLevels `json:",omitempty"` // pointer type with omitempty
}
type UserLevels struct {
LevelTitle string
}
func main() {
var x MyData
x.Title = "foo"
x.AddedByUser.ID = 2
x.AddedByUser.UserLevel = &UserLevels{}
f(x)
b, _ := json.MarshalIndent(x, "", " ")
fmt.Println("main:\n" + string(b))
}
func f(x MyData) {
// "unset" UserLevel. Since we are receiving a copy of MyData, this is
// invisible to the caller.
x.AddedByUser.UserLevel = nil
b, _ := json.MarshalIndent(x, "", " ")
fmt.Println("f:\n" + string(b))
}
// Output:
// f:
// {
// "Title": "foo",
// "AddedByUser": {
// "ID": 2
// }
// }
// main:
// {
// "Title": "foo",
// "AddedByUser": {
// "ID": 2,
// "UserLevel": {
// "LevelTitle": ""
// }
// }
// }
Try it on the playground: https://play.golang.org/p/trUgnYamVOA
Alternatively, you can define new types that exclude the AddedByUser field. However, since this field isn't at the top level, this is a lot of work, and it's easy to forget to update those types when new fields are added to the original types.
If the field were at the top level, the compiler would do most of the work for you, because types that only differ in their field tags can be directly converted to one another:
type MyData struct {
ID int
Title string
}
func main() {
var x MyData
x.ID = 1
x.Title = "foo"
f(x)
}
func f(x MyData) {
type data struct { // same as MyData, except the field tags
ID int
Title string `json:"-"`
}
b, _ := json.MarshalIndent(data(x), "", " ")
fmt.Println("main:\n" + string(b))
}