Open condorcet opened 4 months ago
Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.
@condorcet
I guess you can use Status.Value(StatusUpdated)
which would return updated
as a string
.
Have you tried using the next
branch? I know there are some Go improvements coming there, but don't recall if this is fixed :v:
@condorcet I guess you can use
Status.Value(StatusUpdated)
which would returnupdated
as astring
.
Yes, it works, but as I mentioned before it's impossible to marshal type "as is", because underlying type is uint
.
For example, we can't do something like that
// Status represents an enum of Status.
type Status uint
const (
StatusCreated Status = iota
StatusUpdated
StatusDeleted
)
type MyObject struct {
Status Status
}
data, _ := json.Marshal(&MyObject{
Status: StatusUpdated.Value(), // type mismatch, Value returns any, but type is uint
})
Have you tried using the
next
branch? I know there are some Go improvements coming there, but don't recall if this is fixed ✌️
@jonaslagoni Yes, I've tried, the problem still exists.
For my project I extend generator with MarshalJSON / UnmarshalJSON. Maybe I can make PR with proposal?
Please do yea :v: Make sure you target next
🙂
This issue has been automatically marked as stale because it has not had recent activity :sleeping:
It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.
There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.
Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.
Thank you for your patience :heart:
Why do we need this improvement?
For now, generated enums in Go generator represents as
uint
https://github.com/asyncapi/modelina/blob/master/src/generators/go/renderers/EnumRenderer.ts#L28This also covered by docstring here
For example, if we have string type enum like this
Generated code will be:
It becomes blocker to use generated type "as is", because when we need marshal / unmarshal this type to JSON, we can't rely on string type (underlying
Status
type is uint).Of course, we have
Value()
method to use actual values of enum, but it forces to make some additional wrappers on original type. We can't make something like this:How will this change help?
This change unlocks using of generated enum types "as is" with different types (e.g. string) to satisfy original JSON schema.
Screenshots
No response
How could it be implemented/designed?
One of the solution is to add methods MarshalText / UnmarshalText to generated type. It helps json codec to serialize data to target type according JSON schema.
In scope of the example very simplified solution for string enum values may look this:
and
This solution aims not to change internal representation of enum value, but only JSON serialization. So, this change can be considered backward compatible.
🚧 Breaking changes
No
👀 Have you checked for similar open issues?
🏢 Have you read the Contributing Guidelines?
Are you willing to work on this issue?
None