Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dynamodb] Support Text(Un)marshaler interfaces with string struct tag #3419

Closed
finnbear opened this issue Jul 12, 2020 · 1 comment
Closed
Labels
feature-request A feature should be added or improved.

Comments

@finnbear
Copy link

finnbear commented Jul 12, 2020

Is your feature request related to a problem? Please describe.
In my project, I have several enumerators like

type ToolType int
const (
    HammerTool ToolType = iota
    TrowelTool
    FrameTool
    PistolTool
    CannonTool
)

type Entity struct {
    Object
    Tool ToolType `json:"tool"` `dynamodbav:"tool,string"` // Marshaled in dynamo as {S: "2"} not {S: "frame"}, for example
    ...
}

I have defined MarshalText and UnmarshalText for each of them, so that they may be serialized as strings in JSON.

func (toolType ToolType) MarshalText() ([]byte, error) {
    switch toolType {
    case HammerTool:
        return []byte("hammer"), nil
    case TrowelTool:
        return []byte("trowel"), nil
    case FrameTool:
        return []byte("frame"), nil
    case PistolTool:
        return []byte("pistol"), nil
    case CannonTool:
        return []byte("cannon"), nil
    default:
        return nil, errors.New("Unknown tool " + fmt.Sprint(toolType))
    }
}

func (toolType *ToolType) UnmarshalText(text []byte) error {
    switch string(text) {
    case "hammer":
        *toolType = HammerTool
    case "trowel":
        *toolType = TrowelTool
    case "frame":
        *toolType = FrameTool
    case "pistol":
        *toolType = PistolTool
    case "cannon":
        *toolType = CannonTool
    default:
        return errors.New("Unknown tool " + string(text))
    }
    return nil
}

I want to do the same in DynamoDB, but the only option is to create special DynamoDB marshalers and unmarshalers. The issue is that those special marshalers must be defined in the same package (due to Go's rules), and that is impossible in my project as the package that defines the enumerator type is shared with a client-side WebAssembly application that cannot afford to include an import of AWS SDK.

Describe the solution you'd like
If the struct tag string is used, and the type supports the Text(Un)marshaler interface, marshal/unmarshal using that interface.

Describe alternatives you've considered
I've switched all my code to https://github.com/guregu/dynamo which supports this feature (even without a special struct tag). This one issue is the only reason I switched.

You could also add a new struct tag text in reference to TextMarshaler

Additional context
Full details in Go forum question: https://forum.golangbridge.org/t/implementing-custom-marshalers-unmarshalers-on-imported-types/19680

@finnbear finnbear added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jul 12, 2020
@diehlaws diehlaws self-assigned this Jul 15, 2020
@diehlaws diehlaws removed the needs-triage This issue or PR still needs to be triaged. label Jul 15, 2020
@diehlaws
Copy link
Contributor

Hi @finnbear, thanks for reaching out to us about this. As with #3408, this would be a good feature to have in version 2 of the AWS SDK for Go however it is not something we would be interested in implementing in version 1. I suggest opening a new feature request under V2's repository to discuss this feature in the appropriate repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

2 participants