One struct multiple json representation [duplicate] - go

This question already has answers here:
Is it possible to have a struct with multiple JSON tags?
(2 answers)
Closed 3 years ago.
The Problem I am trying to solve is that I have a representation as below:
type Request struct{
ItId string `form:"itId"`
tR string `form:"treason"`
cd string `form:"cdetails"`
}
but I want the above one in such a manner that it can accept:
itId and ItrId both for ItId
It will be something like this:
type Request struct{
ItId string `form:"itId"` || ItId string `form:"ItrId"`
tR string `form:"treason"`
cd string `form:"cdetails"`
}
what can be the possible solution for such a case?

I am not sure which package are you importing that uses form struct tag, but to try and answer the question, I am assuming it is similar to json tag that is imported by encoding/json package.
In short, no it is not possible to have a same struct tag for a field, as answered in this SOF question - https://stackoverflow.com/a/37118633/5353730
The OP in above question was trying to accomplish something like:
type Foo struct {
Name string `json:"name" json:"employee_name"`
Age int `json:"age" json:"-"`
}
...which I assume is what you want to achieve.
If, however, you might've been trying to use different meta tags for the same field, then that is easily supportable as shown in this answer - https://stackoverflow.com/a/18635910/5353730
type Page struct {
PageId string `bson:"pageId" json:"pageId"`
Meta map[string]interface{} `bson:"meta" json:"meta"`
}
P.S. You can always create 2 separate fields, one each for itId and ItrId to solve your problem but then you will need to check for both these fields at the place of usage of struct.

Related

How do you access this Go struct? [duplicate]

This question already has an answer here:
Strange type definition syntax in Golang (name, then type, then string literal)
(1 answer)
Closed 1 year ago.
I'm trying to make sense of this Go struct:
type ListClustersOutput struct {
_ struct{} `type:"structure"`
// A list of all of the clusters for your account in the specified Region.
Clusters []*string `locationName:"clusters" type:"list"`
// The nextToken value to include in a future ListClusters request. When the
// results of a ListClusters request exceed maxResults, you can use this value
// to retrieve the next page of results. This value is null when there are no
// more results to return.
NextToken *string `locationName:"nextToken" type:"string"`
}
Looking at the docs: https://golangdocs.com/structs-in-golang#defining-a-struct-in-go
it gives an example:
type Fruit struct {
name string
}
which seems very different.
In the more complex code, I assume this Clusters []*string `locationName:"clusters" type:"list"` is equivalent to name string but struggling to unpack it.
I'm struggling to find much out about type: "list" - most of the examples seem to refer to slices. Why are they using a list?
what is a locationName?
how do you access the first element of the list in that struct?
Note, for this last question, if I use result.Clusters[0] (where result is of this struct type) I get a pointer. E.g.
fmt.Println("Result: ", result.Clusters[0])
Result: 0xc000372260
How do I dereference it?
Looking at this:
How does pointer dereferencing work in Go?
it seems you need an asterisk or an ampersand. Not clear which one you use or whether you tack it on the beginning or the end.
You are struggling with struct tags.
In your code:
Clusters []*string `locationName:"clusters" type:"list"`
The Clusters field has a type ([]*string) and the rest of the declaration are 2 struct tags that you should take the value(s) of tags using struct tag.
Here's how you access it:
fmt.Println("Result: ", *result.Clusters[0])

"invalid recursive type" and "illegal cycle in declaration of" [duplicate]

This question already has answers here:
invalid recursive type in a struct in go
(2 answers)
Closed 11 months ago.
Shortened example of the two custom types below. "Question" contains a "SavedQuestion" and "SavedQuestion" contains a "Question".
Errors:
1. illegal cycle in declaration of SavedQuestion
2. invalid recursive type Question
type Question struct {
ID int `json:"id"`
Name string `json:"name"`
QueryText string `json:"query_text"`
SavedQuestion SavedQuestion `json:"saved_question"`
}
type SavedQuestion struct {
ID int `json:"id"`
Index int `json:"index"`
Name string `json:"name"`
Packages PackageSpecList `json:"packages"`
QueryText string `json:"query_text"`
Question Question `json:"question"`
}
I'm modeling some types to consume a 3rd party API... The 3rd party API's objects are modeled this way. It seems perfectly normal that a "SavedQuestion" is a separate type that essentially contains a "Question" along with a bunch of other data about it... I can see the loop with a "Question" also containing a "SavedQuestion"... but that's what the API does. As you can see from the json tags I'm just trying to model exactly what the API is sending so I can marshal/unmarshal etc..
How do I make this work? I can make the "Question" in the "SavedQuestion" a pointer to a "Question" and the errors go away... however I don't know if I should or need to do that, or if it will actually work as the code is not yet complete enough to run a test...
New gopher confused...
Thanks for any input.
You cannot recursively include one struct within another for the simple reason that the size and memory layout of the struct becomes recursive. Size of Question is some data + size of (SavedQuestion), and size of SavedQuestion is size of some data + size of Question...
However, if you use pointers, the sizing problem is solved:
type Question struct {
ID int `json:"id"`
Name string `json:"name"`
QueryText string `json:"query_text"`
SavedQuestion *SavedQuestion `json:"saved_question"`
}
You need to make sure when to reference question.SavedQuestion, it is not nil. This will work just fine for marshaling/unmarshaling. When you unmarshal, if the JSON does not have a saved_question element, it'll be nil.

Converting between struct types in Go

So I have a question that I've been trying to figure out for a while and hopefully someone has some insight. Basically, I have two similar, but different structs in Go and I would like to convert between them.
Something like the following:
type A struct {
Name string
}
type B struct {
Name NameStruct
}
type NameStruct struct {
Firstname string
Lastname string
}
In this example,
B.Name == NameStruct{
Firstname: strings.Split(A.Name, " ")[0],
Lastname: strings.Split(A.Name, " ")[1],
}
Basically I would like to be able to define a mapping of fields from one struct to the other as well as a conversion function, so that I can do this without writing all of the code to do the conversion manually.
What I'm imagining is that there might be some sort of Go generator out there that could do something like this. I don't think it is possible using reflection because of the nested structs (although I'm open to suggestions).
Basically, does anyone know of a way to do this that is better than either writing conversion code manually or creating my own generator?

Conversion of Go struct fields with tags

I am really stuck here with a seemingly trivial problem in Go:
I have a Golang microservice, which outputs data in json format. Let's say I have a simple struct with json tags for this result:
type Result struct {
Name string `json:"name"`
Age int `json:"age"`
}
In the code part where the data is actually pulled out of the database, I have a quite similar struct, like this:
type ResultBackend struct {
Name string `bson:"fullName"`
Age int `bson:"age"`
}
The struct fields are similar, except the different tags. I would like to keep things simple, and return just one struct from the backend service (ResultBackend), which can then be send as a JSON response, like this:
func process() Result {
var result ResultBackend
... do a MongoDB query here and store results in result variable ...
return result
}
This certainly would not work, because we have two different structs here.
Of course one solution would be to embed both tags in one struct like so:
type Result struct {
Name string `json:"name" bson:"fullName"`
Age int `json:"age bson:"age"`
}
and then use this struct in the main code and the "process" function.
This works, but this seems like "poisoning" the Result struct of the main code with the bson tags. What, for instance, if the backend result is a XML file? I'd have to add xml tags to the struct as well. Or maybe someday tags some very obsure database adapter.
This doesn't seem to be the cleanest approach in my eyes. I'd rather have a clean Result struct in the main code, and simply to a conversion from one struct to another.
Is there any easy way doing that, or do I really have to copy all the fields of a ResultBackend struct to a new Result struct and return that? Or I am trying to over-simplify my code here? :)
Cheers!
What I'd do is create a separate type for every "serialisation format" if you will. This approach has several advantages:
Your concerns are separated.
JSON un/marshalling doesn't interfere with BSON/XML, so you can add any kind of additional struct fields (like xml.Name for example).
You can actually create several such types for different APIs that need different parameters.
The only disadvantage I see is that it's more code and even more code to move data between those. Ultimately it's up to you and your application design. If you're sure that your JSON/BSON will stay the same, you can use one type. Otherwise, I'd recommend specialisation.
To those who want to add bson tags in golang struct, here is a tool which can be helpful. I have spent a whole day searching for this and I want others to save their valuable time.
You can convert the following struct
type Result struct {
Name string `json:"name"`
Age int `json:"age"`
}
into
type Result struct {
Name string `json:"name" bson:"fullName"`
Age int `json:"age bson:"age"`
}
With this tool, you can not only add bson tags but also
XML tags,
Define validation and scope
Format tag values
Apply transformation
skip unexported fields.
Go Modify Tags: https://github.com/fatih/gomodifytags
Cheers,

convert struct to struct in golang [duplicate]

This question already has answers here:
Assign struct with another struct
(4 answers)
Closed 6 years ago.
trying to figure out what the best option is to convert one struct to another, Example
type user1 struct {
FirstName string
LastName string
UserName string
}
type user2 struct {
FirstName string
LastName string
}
The easiest is probably to simply do something like u2 := user2{FirstName: u1.FirstName, LastName: u1.LastName}. That is assuming you don't mind that the username is lost in the "conversion".
It's possible to build more general converters using reflection, but I would normally try to structure my code so it's not needed.

Resources