-
Notifications
You must be signed in to change notification settings - Fork 149
[Generator] Accept header #185
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0e4b57c
[Prototype] Accept header
czechboy0 838303d
Merge branch 'main' into hd-accept-header-prototype
czechboy0 2bc4fc1
Merge branch 'main' into hd-accept-header-prototype
czechboy0 d051353
PR feedback
czechboy0 5c45519
Merge branch 'main' into hd-accept-header-prototype
czechboy0 6ad1630
Prototype working end-to-end
czechboy0 ed03f89
Merge branch 'main' into hd-accept-header-prototype
czechboy0 6cefe2b
Fixed up bad merges
czechboy0 4220717
Formatting
czechboy0 b9ace9a
PR feedback
czechboy0 2f96123
Bump runtime to 0.1.10
czechboy0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
...s/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateRawRepresentableEnum.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the SwiftOpenAPIGenerator open source project | ||
| // | ||
| // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| import OpenAPIKit30 | ||
|
|
||
| extension FileTranslator { | ||
|
|
||
| /// Returns a declaration of the specified raw representable enum. | ||
| /// - Parameters: | ||
| /// - typeName: The name of the type to give to the declared enum. | ||
| /// - conformances: The list of types the enum conforms to. | ||
| /// - userDescription: The contents of the documentation comment. | ||
| /// - cases: The list of cases to generate. | ||
| /// - unknownCaseName: The name of the extra unknown case that preserves | ||
| /// the string value that doesn't fit any of the cases. If nil is | ||
| /// passed, the unknown case is not generated. | ||
| /// - unknownCaseDescription: The contents of the documentation comment | ||
| /// for the unknown case. | ||
| /// - customSwitchedExpression: A closure | ||
| func translateRawRepresentableEnum( | ||
| typeName: TypeName, | ||
| conformances: [String], | ||
| userDescription: String?, | ||
| cases: [(caseName: String, rawValue: String)], | ||
| unknownCaseName: String?, | ||
| unknownCaseDescription: String?, | ||
| customSwitchedExpression: (Expression) -> Expression = { $0 } | ||
| ) throws -> Declaration { | ||
|
|
||
| let generateUnknownCases = unknownCaseName != nil | ||
| let knownCases: [Declaration] = | ||
| cases | ||
| .map { caseName, rawValue in | ||
| .enumCase( | ||
| name: caseName, | ||
| kind: generateUnknownCases ? .nameOnly : .nameWithRawValue(rawValue) | ||
| ) | ||
| } | ||
|
|
||
| let otherMembers: [Declaration] | ||
| if let unknownCaseName { | ||
| let undocumentedCase: Declaration = .commentable( | ||
| unknownCaseDescription.flatMap { .doc($0) }, | ||
| .enumCase( | ||
| name: unknownCaseName, | ||
| kind: .nameWithAssociatedValues([ | ||
| .init(type: "String") | ||
| ]) | ||
| ) | ||
| ) | ||
| let rawRepresentableInitializer: Declaration | ||
| do { | ||
| let knownCases: [SwitchCaseDescription] = cases.map { caseName, rawValue in | ||
| .init( | ||
| kind: .case(.literal(rawValue)), | ||
| body: [ | ||
| .expression( | ||
| .assignment( | ||
| Expression | ||
| .identifier("self") | ||
| .equals( | ||
| .dot(caseName) | ||
| ) | ||
| ) | ||
| ) | ||
| ] | ||
| ) | ||
| } | ||
| let unknownCase = SwitchCaseDescription( | ||
| kind: .default, | ||
| body: [ | ||
| .expression( | ||
| .assignment( | ||
| Expression | ||
| .identifier("self") | ||
| .equals( | ||
| .functionCall( | ||
| calledExpression: .dot( | ||
| unknownCaseName | ||
| ), | ||
| arguments: [ | ||
| .identifier("rawValue") | ||
| ] | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| ] | ||
| ) | ||
| rawRepresentableInitializer = .function( | ||
| .init( | ||
| accessModifier: config.access, | ||
| kind: .initializer(failable: true), | ||
| parameters: [ | ||
| .init(label: "rawValue", type: "String") | ||
| ], | ||
| body: [ | ||
| .expression( | ||
| .switch( | ||
| switchedExpression: customSwitchedExpression( | ||
| .identifier("rawValue") | ||
| ), | ||
| cases: knownCases + [unknownCase] | ||
| ) | ||
| ) | ||
| ] | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| let rawValueGetter: Declaration | ||
| do { | ||
| let knownCases: [SwitchCaseDescription] = cases.map { caseName, rawValue in | ||
| .init( | ||
| kind: .case(.dot(caseName)), | ||
| body: [ | ||
| .expression( | ||
| .return(.literal(rawValue)) | ||
| ) | ||
| ] | ||
| ) | ||
| } | ||
| let unknownCase = SwitchCaseDescription( | ||
| kind: .case( | ||
| .valueBinding( | ||
| kind: .let, | ||
| value: .init( | ||
| calledExpression: .dot( | ||
| unknownCaseName | ||
| ), | ||
| arguments: [ | ||
| .identifier("string") | ||
| ] | ||
| ) | ||
| ) | ||
| ), | ||
| body: [ | ||
| .expression( | ||
| .return(.identifier("string")) | ||
| ) | ||
| ] | ||
| ) | ||
|
|
||
| let variableDescription = VariableDescription( | ||
| accessModifier: config.access, | ||
| kind: .var, | ||
| left: "rawValue", | ||
| type: "String", | ||
| body: [ | ||
| .expression( | ||
| .switch( | ||
| switchedExpression: .identifier("self"), | ||
| cases: [unknownCase] + knownCases | ||
| ) | ||
| ) | ||
| ] | ||
| ) | ||
|
|
||
| rawValueGetter = .variable( | ||
| variableDescription | ||
| ) | ||
| } | ||
|
|
||
| let allCasesGetter: Declaration | ||
| do { | ||
| let caseExpressions: [Expression] = cases.map { caseName, _ in | ||
| .memberAccess(.init(right: caseName)) | ||
| } | ||
| allCasesGetter = .variable( | ||
| .init( | ||
| accessModifier: config.access, | ||
| isStatic: true, | ||
| kind: .var, | ||
| left: "allCases", | ||
| type: "[Self]", | ||
| body: [ | ||
| .expression(.literal(.array(caseExpressions))) | ||
| ] | ||
| ) | ||
| ) | ||
| } | ||
| otherMembers = [ | ||
| undocumentedCase, | ||
| rawRepresentableInitializer, | ||
| rawValueGetter, | ||
| allCasesGetter, | ||
| ] | ||
| } else { | ||
| otherMembers = [] | ||
| } | ||
|
|
||
| let enumDescription = EnumDescription( | ||
| isFrozen: true, | ||
| accessModifier: config.access, | ||
| name: typeName.shortSwiftName, | ||
| conformances: conformances, | ||
| members: knownCases + otherMembers | ||
| ) | ||
| let comment: Comment? = | ||
| typeName | ||
| .docCommentWithUserDescription(userDescription) | ||
| return .commentable( | ||
| comment, | ||
| .enum(enumDescription) | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.