I have a struct, which is decoded from a JSON http POST req. My purpose of having this struct is: - Simplifying JSON request from client - Using the structs property data in another (external library) struct.
If i had to only use the external library struct, the client JSON setup will look confusing. How do i use my structs values inside another struct (and especially their arrays)?
I have a working solution for some of the values with simple types.
Consider the following: Ext lib struct:
type ExtStruct struct {
From *Email
Subject string
Personalizations []*Personalization
}
My lib struct:
type MyStruct struct {
From *Email
Subject string
Personalizations []*Personalization
}
This is my code as is:
myStruct := &MyStruct{}
err := json.NewDecoder(body).Decode(myStruct)
extStruct := &ExtStruct{
Subject: myStruct.Subject,
From: (*extStruct.Email)(myStruct.From),
Personalizations: []*extStruct.Personalization{
To: ([]*extStruct.Email)(myStruct.To),
}}
The Subject
and From
value works, but i'm getting errors when trying to referencing array values.
I fail to see where i am wrong. AFAIK there's no other option to "simplify" input JSON from a client, only to reference the values in another bigger struct.
I can include the other referenced structs if needed, but think of it as:
type A struct {
RefB []*B
}
type B struct {
RefC []*C
Value string
}
type C struct {
Value string
}
And i need my struct to refer to C.