Skip to content

Wildcard support when configuring transformers with resources #240

Open

Description

@jwells89

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.

Activity

  1. jwells89 commented on Apr 27, 2018

    @jwells89
    Author

    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

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