asyncapi / modelina

A library for generating typed models based on inputs such as AsyncAPI, OpenAPI, and JSON Schema documents with high customization
https://modelina.org
Apache License 2.0
312 stars 180 forks source link

[FEATURE] Support marshal / unmarshal enum values to target types in Go generator #2043

Open condorcet opened 4 months ago

condorcet commented 4 months ago

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#L28

This also covered by docstring here

  • This renderer is a generic solution that works for all types of enum values.
    • This is also why you wont see `type MyEnum string´ even if possible.

For example, if we have string type enum like this

    myObject:
      type: object
      properties:
        id:
          type: string
        status:
          $id: status
          type: string
          enum:
            - created
            - updated
            - deleted

Generated code will be:

// Status represents an enum of Status.
type Status uint

const (
    StatusCreated Status = iota
    StatusUpdated
    StatusDeleted
)

// Value returns the value of the enum.
func (op Status) Value() any {
    if op >= Status(len(StatusValues)) {
        return nil
    }
    return StatusValues[op]
}

var StatusValues = []any{"created", "updated", "deleted"}
var ValuesToStatus = map[any]Status{
    StatusValues[StatusCreated]: StatusCreated,
    StatusValues[StatusUpdated]: StatusUpdated,
    StatusValues[StatusDeleted]: StatusDeleted,
}

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).

data, _ := json.Marshal(&MyObject{
    Status: StatusUpdated, // will be uint, not a string
})

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:

data, _ := json.Marshal(&MyObject{
    Status: StatusUpdated.Value(), // type mismatch
})

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:

func (op Status) MarshalText() ([]byte, error) {
    val := op.Value()
    if val == nil {
        return nil, fmt.Errorf("unknown enum value %d", op)
    }

        // only string type supported for simplicity of the example
    strVal, ok := val.(string)
    if !ok {
        return nil, fmt.Errorf("unexpected enum value type %T", val)
    }
    return []byte(strVal), nil
}

and

func (op *Status) UnmarshalText(text []byte) error {
    key := string(text)
    val, ok := ValuesToStatus[key]
    if !ok {
        val = ValuesToStatus[StatusValues[0]] // using first value as "default". We need to think is it correct to support such semantics?
    }
    *op = val
    return nil
}

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

github-actions[bot] commented 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.

kavania2002 commented 4 months ago

@condorcet I guess you can use Status.Value(StatusUpdated) which would return updated as a string.

jonaslagoni commented 4 months ago

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 commented 4 months ago

@condorcet I guess you can use Status.Value(StatusUpdated) which would return updated as a string.

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
})
condorcet commented 4 months ago

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?

jonaslagoni commented 4 months ago

Please do yea :v: Make sure you target next 🙂

github-actions[bot] commented 1 week ago

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: