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

Weird compilation error on Route parsing #50

Closed
rcasula opened this issue Sep 13, 2022 · 3 comments
Closed

Weird compilation error on Route parsing #50

rcasula opened this issue Sep 13, 2022 · 3 comments

Comments

@rcasula
Copy link

rcasula commented Sep 13, 2022

Hi, I was trying to model an existing API but I'm encountering a problem.

Example:

This is one of the urls: BASE_URL/somepath/SOME_STRING_VAR/ANOTHER_STRING_VAR.
The method is POST and it has a String body.

I was trying to model it like this:

enum SiteRoute {
    case somepath(var1: String, var2: String, body: String)
}
... 
let router = OneOf {
    Route(.case(SiteRoute.somepath)) {
        Method.post
        Path { 
            "someroute"
            Parse(.string)
            Parse(.string)
        }
        Body(?) // I can't figure out what to put here.
    }
}

If I define the model like this, instead:

enum SiteRoute {
    case somepath(var1: String, var2: String, body: SomeModel)
}
... 
let router = OneOf {
    Route(.case(SiteRoute.somepath)) {
        Method.post
        Path { 
            "someroute"
            Parse(.string)
            Parse(.string)
        }
        Body(.json(SomeModel.self))
    }
}

It throws me this compile error.
Screenshot 2022-09-13 at 11 11 41

How can I solve this (theoretically shouldn't be) complicated task? I'm sure there's something I'm missing.
Thanks in advance.

@rcasula
Copy link
Author

rcasula commented Sep 26, 2022

Anyone?

@mbrandonw
Copy link
Member

Hi @rcasula, this is a bit of a gotcha with result builders, and hopefully it's something we can make a little bit nicer soon.

But the problem is that this kind of parser:

Path { 
  "someroute"
  Parse(.string)
  Parse(.string)
}
Body(.json(SomeModel.self))

…outputs a nested tuple of type ((String, String), SomeModel). The result builders can't automatically flatten that to (String, String, SomeModel).

The easiest fix is to define a type that holds both strings so you can parse into a single type:

Path { 
  Parse(.memberwise(SomeStrings.init)) {
    "someroute"
    Parse(.string)
    Parse(.string)
  }
}
Body(.json(SomeModel.self))

That will give you a tuple of (SomeStrings, SomeModel), and then that can be mapped into the enum case.

@rcasula
Copy link
Author

rcasula commented Sep 26, 2022

It was simple and tricky at the same time, I guess I need to study more the parser's library. Thank you so much for your help!

@rcasula rcasula closed this as completed Sep 26, 2022
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

2 participants