-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Code-generate Acceptors for waiters (#483)
- Loading branch information
Showing
24 changed files
with
1,552 additions
and
223 deletions.
There are no files selected for viewing
This file contains 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 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 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,102 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
/// Utility functions for performing comparisons between values in JMESPath expressions. | ||
/// | ||
/// `Bool` may be compared for equality & inequality. | ||
/// | ||
/// `String` and a `RawRepresentable where RawValue == String` may be interchangeable compared for equality and inequality. | ||
/// | ||
/// `Int` and `Double` may be interchangeably compared for equality, inequality, and order. | ||
/// | ||
/// When one of the values in an order comparison is `nil`, the result is `false`. | ||
public enum JMESUtils { | ||
|
||
// Function for comparing Bool to Bool. | ||
|
||
public static func compare(_ lhs: Bool?, _ comparator: (Bool?, Bool?) -> Bool, _ rhs: Bool?) -> Bool { | ||
return comparator(lhs, rhs) | ||
} | ||
|
||
// Functions for comparing Double to Double. | ||
|
||
public static func compare(_ lhs: Double?, _ comparator: (Double?, Double?) -> Bool, _ rhs: Double?) -> Bool { | ||
comparator(lhs, rhs) | ||
} | ||
|
||
public static func compare(_ lhs: Double?, _ comparator: (Double, Double) -> Bool, _ rhs: Double?) -> Bool { | ||
guard let lhs = lhs, let rhs = rhs else { return false } | ||
return comparator(lhs, rhs) | ||
} | ||
|
||
// Functions for comparing Int to Int. | ||
|
||
public static func compare(_ lhs: Int?, _ comparator: (Int?, Int?) -> Bool, _ rhs: Int?) -> Bool { | ||
comparator(lhs, rhs) | ||
} | ||
|
||
public static func compare(_ lhs: Int?, _ comparator: (Int, Int) -> Bool, _ rhs: Int?) -> Bool { | ||
guard let lhs = lhs, let rhs = rhs else { return false } | ||
return comparator(lhs, rhs) | ||
} | ||
|
||
// Function for comparing String to String. | ||
|
||
public static func compare(_ lhs: String?, _ comparator: (String?, String?) -> Bool, _ rhs: String?) -> Bool { | ||
comparator(lhs, rhs) | ||
} | ||
|
||
// Function for comparing two types that are each raw representable by String. | ||
|
||
public static func compare<L: RawRepresentable, R: RawRepresentable>( | ||
_ lhs: L?, | ||
_ comparator: (String?, String?) -> Bool, | ||
_ rhs: R? | ||
) -> Bool where L.RawValue == String, R.RawValue == String { | ||
comparator(lhs?.rawValue, rhs?.rawValue) | ||
} | ||
|
||
// Extensions for comparing Int and / or Double. | ||
|
||
public static func compare(_ lhs: Int?, _ comparator: (Double?, Double?) -> Bool, _ rhs: Double?) -> Bool { | ||
comparator(lhs.map { Double($0) }, rhs) | ||
} | ||
|
||
public static func compare(_ lhs: Double?, _ comparator: (Double?, Double?) -> Bool, _ rhs: Int?) -> Bool { | ||
comparator(lhs, rhs.map { Double($0) }) | ||
} | ||
|
||
public static func compare(_ lhs: Int?, _ comparator: (Double, Double) -> Bool, _ rhs: Double?) -> Bool { | ||
guard let lhs = lhs, let rhs = rhs else { return false } | ||
return comparator(Double(lhs), rhs) | ||
} | ||
|
||
public static func compare(_ lhs: Double?, _ comparator: (Double, Double) -> Bool, _ rhs: Int?) -> Bool { | ||
guard let lhs = lhs, let rhs = rhs else { return false } | ||
return comparator(lhs, Double(rhs)) | ||
} | ||
|
||
// Extensions for comparing String with types having raw value of String. | ||
|
||
public static func compare<T: RawRepresentable>( | ||
_ lhs: T?, | ||
_ comparator: (String?, String?) -> Bool, | ||
_ rhs: String? | ||
) -> Bool where T.RawValue == String { | ||
comparator(lhs?.rawValue, rhs) | ||
} | ||
|
||
public static func compare<T: RawRepresentable>( | ||
_ lhs: String?, | ||
_ comparator: (String?, String?) -> Bool, | ||
_ rhs: T? | ||
) -> Bool where T.RawValue == String { | ||
comparator(lhs, rhs?.rawValue) | ||
} | ||
} |
109 changes: 0 additions & 109 deletions
109
smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/WaiterGenerator.kt
This file was deleted.
Oops, something went wrong.
This file contains 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
56 changes: 56 additions & 0 deletions
56
...-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/utils/BufferWriter.kt
This file contains 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,56 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
package software.amazon.smithy.swift.codegen.utils | ||
|
||
import software.amazon.smithy.swift.codegen.SwiftWriter | ||
import java.util.UUID | ||
|
||
/** | ||
* A class which uses a writer to "record" code into an in-memory buffer, | ||
* then "plays it back" onto the writer at a later time. | ||
* | ||
* Useful when the result of rendering an expression is needed before the expression | ||
* is actually to be included in the code. | ||
*/ | ||
class BufferWriter( | ||
val writer: SwiftWriter, | ||
private val mutableBuffer: MutableList<String> = mutableListOf() | ||
) { | ||
/** | ||
* Record whatever text is written in the block to the internal buffer. | ||
*/ | ||
fun record(block: (SwiftWriter) -> Unit) { | ||
// Create a random string to use as a section identifier. | ||
val sectionID = UUID.randomUUID().toString() | ||
|
||
// Open a new section on the writer. | ||
writer.pushState(sectionID) | ||
|
||
// onSection will "intercept" the code written to the given section and | ||
// allow it to be redirected using the trailing closure. | ||
writer.onSection(sectionID) { | ||
val text = it as? String | ||
if (text != null) { | ||
// Text sometimes comes in as multiple strings, so trim indentation then split it | ||
mutableBuffer.addAll(text.trimIndent().split("\n")) | ||
} | ||
} | ||
|
||
// Perform the writes performed in the trailing closure to this function. | ||
block(writer) | ||
|
||
// Close the section, returning the writer to its previous state. | ||
writer.popState() | ||
} | ||
|
||
/** | ||
* Write the contents of the buffer to the writer, then erase the buffer. | ||
*/ | ||
fun playback() { | ||
mutableBuffer.forEach { writer.write(it) } | ||
mutableBuffer.removeAll { true } | ||
} | ||
} |
Oops, something went wrong.