Skip to content

Supporting AddResource with a factory function #348

@Strobotti

Description

@Strobotti

I need to be able to have some predefined values injected to the prototype passed to the AddResource function and was thinking of opening a pull request for something like this, but before even starting that work I'd like to hear your opinion on this functionality and behaviour. As the internal addResource uses reflection to create the resource from the prototype I cannot prepopulate anything while calling the AddResource funtion itself.

For backwards compatibility I'd be adding a separate function for this, such as:

original function at https://github.com/manyminds/api2go/blob/master/api_public.go#L45

// The original AddResource:
func (api *API) AddResource(prototype jsonapi.MarshalIdentifier, source interface{}) {
	api.addResource(prototype, source)
}

// the interface that creates the resource
type ResourceCreator interface {
    CreateResourcePrototype() jsonapi.MarshalIdentifier
}

// Actual method; the name TBD
func (api *API) AddResourceCreator(creator ResourceCreator, source interface{}) {
	...
}

So it would be used along the lines of this (crappy example but hopefully illustrates the purpose):

type MyThing struct {
    PathFormat string
}

type MyCreator struct {
    // The PathFormat used as an example could be specified here along with other whatever dependencies
}

func (m MyCreator)  CreateResourcePrototype() jsonapi.MarshalIdentifier {
    // this way whatever configuration can be placed to the creator itself and it injects it to the prototype:
    return MyThing {
        PathFormat: "/product/{productId}/{slug}",
    }
}

Does this make sense?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions