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

Supporting AddResource with a factory function #348

Open
Strobotti opened this issue Mar 15, 2023 · 0 comments
Open

Supporting AddResource with a factory function #348

Strobotti opened this issue Mar 15, 2023 · 0 comments

Comments

@Strobotti
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant