-
My scenarioIn my application I write OpenAPI yaml first, then I generate models using oapi-codegen. For instance the return struct of the request columns := ProjectionList{}
for _, column := range t.Listings.AllColumns {
columns = append(
columns,
column.AS("GetListingsId200JSONResponse."+column.Name()),
)
}
selectStatement := t.Listings.SELECT(
columns,
).WHERE(
t.Listings.ID.EQ(uuid),
)
var dest GetListingsId200JSONResponse
_ = selectStatement.Query(database.DB, &dest) The problemAnd this works fine and looks nice and whatnot, I am very happy with it. BUT! Due to the long model names that gets generated, the expression Potential solutions I have thought ofConceptually I want to "turn off" the requirement of specifying the struct name in the sql aliases, but after skimming throug the Solution 1: Use shorter names (don't really like this idea)A way to work around this is to have short column names, but I am not a fan of that if I have to trade off self descriptive column names. Also, some of the generated model names can get very long, which would force me to make really short column names, which I find unacceptable. Another way is to build postgres with another column size value (yes you have to build it), but I don't wanna do that. Solution 2: Use the "type combination functionality"I can do the following: type Listing struct {
GetListingsId200JSONResponse // generated by oapi-codegen
this_here_is_a_kinda_long_column_name string
} Then I can alias said column to "Listing.this_here_is_a_kinda_long_column_name", which will work. But I don't really want to do this as I will have to manually ensure that I do this for all columns that will be too long together with the struct name. Solution 3: Create anon structs in runtimeA solution I can think of then is to make a "recursive anonymous struct creator" during runtime that basically takes in the dest, basically creates an equivalent struct but with no names, uses said struct for QRM, then I do a deepcopy of the anonymous struct to the actual struct that I want. Pseudo code: columns := ProjectionList{}
for _, column := range t.Listings.AllColumns {
columns = append(columns, column.AS("GetListingsId200JSONResponse."+column.Name()))
}
selectStatement := t.Listings.SELECT(
columns,
).WHERE(
t.Listings.ID.EQ(uuid),
)
var actualDest GetListingsId200JSONResponse
tempDest := CreateAnonStruct(&actualDest)
_ = selectStatement.Query(database.DB, &tempDest)
DeepCopy(&tempDest, &actualDest) My questionSo my question is: is there any go-jet "native way" to deal with this, or do I just have to do the anonymization trick? What I know doesn't workType aliasing does not work, as the aliasing won't change the name when doing reflections, e.g. EDIT: I wrote "type aliasing does not work", but showed code for type declaration, basically type Listing = GetListingsId200JSONResponse // I meant this
type Listing GetListingsId200JSONResponse // I wrote this, which is NOT aliasing, but a declaration (and this works! see answer by houten) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Another example of a long generated name that I have is:
This name alone is longer than what postgres supports, so in theory I can't really use QRM for this model at all. |
Beta Was this translation helpful? Give feedback.
-
This should work, reflect.Type.Name() should return |
Beta Was this translation helpful? Give feedback.
This should work, reflect.Type.Name() should return
Listing
.