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

Wildcard support when configuring transformers with resources #240

Open
jwells89 opened this issue Jan 24, 2018 · 1 comment
Open

Wildcard support when configuring transformers with resources #240

jwells89 opened this issue Jan 24, 2018 · 1 comment

Comments

@jwells89
Copy link

jwells89 commented Jan 24, 2018

Currently, if one wants to configure Siesta to use transformers on a resource with a wildcard in its path, they have to perform said configuration with strings, meaning that a certain amount of repeated information is unavoidable. It also adds a lot of long, unchecked strings that are easy to inadvertently modify without noticing. This isn't too bad with simple APIs, but for those with many endpoints it can really pile up.

I propose the addition of a way to use wildcard elements when configuring Siesta's transformers with resources. One way this could be done is with an any property on Resource that translates to * when path conversion is performed (which would use the same pattern matching logic found in the ConfigurationPatternConvertible extension on String).

Using a class/struct hierarchy that matches your endpoints' paths along with Swift's Codable could look something like this:
configureTransformer(API(.v2).tasks.any.assignees) { try decoder.decode([Person].self, from: $0.content) }

which can be shortened to this with a small extension in application code:
configureTransformer(API(.v2).tasks.any.assignees, [Person].self)

instead of:
configureTransformer("/v2/tasks/*/assignees") { try decoder.decode([Person].self, from: $0.content) }

This enables more conciseness, reduced stringiness, and improved readability. Something like this can done purely in application code but it'd be great if it were part of Siesta itself.

@jwells89
Copy link
Author

jwells89 commented Apr 27, 2018

Quick update on this: in our application I've created an extension for Siesta's Service that allows transformers to be configured with instances of my custom Resource wrapping APIComponent class:

func configureTransformer<T: Decodable>(for component: APIComponent, methods: [RequestMethod], type: T.Type) {
    configureTransformer(component.resource.path,
                         requestMethods: methods) {try self.decoder.decode(type, from: $0.content) }
}

which makes for syntax quite similar to that described in the original post:

configureTransformer(for: Tasks(.v2), methods: [.get],   type: STKPaged<STKTask>.self)
configureTransformer(for: Tasks(.v2).any,                type: STKTask.self)
configureTransformer(for: Tasks(.v2).any.assignees,      type: STKUser.self)

It works and is an improvement on the string-based setup, but it'd still be very nice to see some less specialized form of this idea make its way into Siesta proper.

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