Skip to content

Generate a convenience method to create a response case without any parameters #651

Closed
@czechboy0

Description

@czechboy0

Motivation

Today, some responses have no headers nor body (for example, 204 No Content).

In generated code it looks like this:

internal enum Output: Sendable, Hashable {
    internal struct NoContent: Sendable, Hashable {
        /// Creates a new `NoContent`.
        internal init() {}
    }
    case noContent(Operations.addPollAnswer.Output.NoContent)
}

And when writing a server handler, adopters have to spell it as:

return .noContent(.init())

The (.init()) bit is unnecessary, and we should make this common case prettier.

Proposed solution

One possible way to fix this (I welcome other ideas, though) is by generating a static var for any response cases that have no required values in the initializer. In practice, since response bodies are always required, this would only apply to responses with no body and with only optional response headers.

The generated code would look closer to the following:

internal enum Output: Sendable, Hashable {
    internal struct NoContent: Sendable, Hashable {
        /// Creates a new `NoContent`.
        internal init() {}
    }
    case noContent(Operations.addPollAnswer.Output.NoContent)
    internal static var noContent: Self { .noContent(.init()) } // <<< this is new
}

which would allow the server handler to just have:

return .noContent

Alternatives considered

No response

Additional information

No response

Metadata

Metadata

Assignees

Labels

area/generatorAffects: plugin, CLI, config file.kind/usabilityUsability of generated code, ergonomics.size/SSmall task. (A couple of hours of work.)

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions