Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Add code generator for a reference/resource meta type interface #1354

Open
@katmatt

Description

@katmatt

We should use the jackson @JsonTypeName for all of our resources and i already started to do this while working on our support for subscriptions (#1338).

The idea is to use this annotation to get rid of our handwritten methods that return the type name for a resource.
Currently our pattern looks like this:

@JsonTypeName('cart-discount')
public interface CartDiscount extends Resource<CartDiscount> {
    static String referenceTypeId() {
        return "cart-discount";
    }

    default Reference<CartDiscount> toReference() {
        return Reference.of(referenceTypeId(), getId(), this);
    }

    static TypeReference<CartDiscount> typeReference() {
        return new TypeReference<CartDiscount>() {
            @Override
            public String toString() {
                return "TypeReference<CartDiscount>";
            }
        };
    }

    static Reference<CartDiscount> referenceOfId(final String id) {
        return Reference.of(referenceTypeId(), id);
    }

To avoid having to write this code manually, we could extract the type name from the @JsonTypename annotation and use it in a new code generator to generate a CartDiscountMetaType interface from it:

interface CartDiscountMetaType {
    static String referenceTypeId() {
        return "cart-discount";
    }

    default Reference<CartDiscount> toReference() {
        return Reference.of(referenceTypeId(), getId(), this);
    }

    static TypeReference<CartDiscount> typeReference() {
        return new TypeReference<CartDiscount>() {
            @Override
            public String toString() {
                return "TypeReference<CartDiscount>";
            }
        };
    }

    static Reference<CartDiscount> referenceOfId(final String id) {
        return Reference.of(referenceTypeId(), id);
    }

Then we could get rid of our manually written code with:

public interface CartDiscount extends Resource<CartDiscount>, CartDiscountMetaType {
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions