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

Remove MetadataType from the core package struct #1735

Closed
wagoodman opened this issue Apr 13, 2023 · 1 comment · Fixed by #1983
Closed

Remove MetadataType from the core package struct #1735

wagoodman opened this issue Apr 13, 2023 · 1 comment · Fixed by #1983
Assignees
Labels
breaking-change Change is not backwards compatible
Milestone

Comments

@wagoodman
Copy link
Contributor

(reporting on the behalf of @kzantow) The only reason to have a MetadataType field is so that when marshalled to JSON, there is an identifier available to determine which type to unmarshal to. However, this can be entirely handled in the JSON encoding/decoding using something similar to:

func (p Package) MarshalJSON() ([]byte, error) {
    type pk Package
    return json.Marshal(struct {
        pk
        MetadataType string
    }{
        pk: pk(p),
        MetadataType:    reflect.TypeOf(p.Metadata).Name(),
    })
}

func (b *Package) UnmarshalJSON(data []byte) error {
    var typ struct {
        MetadataType string
    }
    if err := json.Unmarshal(data, &typ); err != nil {
        return err
    }

    metadataType := pkg.MetadataTypeByName[typ.MetadataType]

    type tmp Package // avoids infinite recursion
    err := json.Unmarshal(data, (*tmp)(p))
    if err != nil { return err }

    // handle metadata

    return nil
}
@wagoodman wagoodman added the breaking-change Change is not backwards compatible label Apr 13, 2023
@wagoodman wagoodman added this to the Stabilize user surfaces milestone Apr 13, 2023
@wagoodman wagoodman added breaking-change Change is not backwards compatible and removed breaking-change Change is not backwards compatible labels Apr 13, 2023
@wagoodman
Copy link
Contributor Author

Should be done in conjunction with #1844 . Also consider consolidating all instances where a user would need to make an addition when a new metadata struct is added to also be generated by the same method as done in #1841 .

@wagoodman wagoodman mentioned this issue Jun 26, 2023
4 tasks
@wagoodman wagoodman self-assigned this Jul 12, 2023
@wagoodman wagoodman changed the title Remove rid of MetadataType in internal package struct Remove MetadataType from the core package struct Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Change is not backwards compatible
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant