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

Compile regexes for @pattern strings early #2058

Merged
merged 29 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b5f1341
Add docs for `compile_regex()`
jjant Nov 30, 2022
83f3203
Try to pass `@pattern` string shapes to service visitor
jjant Nov 30, 2022
22a0e03
Merge branch 'main' into jjant/init-regexes-early
jjant Dec 5, 2022
e3bc78c
Add `compile_rege` calls
jjant Dec 5, 2022
0edb00f
Remove unwanted changes
jjant Dec 5, 2022
7ab947c
Fix python code generator
jjant Dec 7, 2022
926edeb
Add hack for `@pattern` + `@enum` trait
jjant Dec 7, 2022
73b1cfd
Generate tests for regexes
jjant Dec 16, 2022
acdf8b7
Move some functionality to `TraitInfo`
jjant Dec 16, 2022
db05308
Add docs for `Symbol.testModule`
jjant Dec 16, 2022
be5dd0c
Improve docs for `Symbol.testModule`
jjant Dec 16, 2022
f1bacb7
Merge branch 'main' into jjant/init-regexes-early
jjant Dec 20, 2022
c93bb16
Merge branch 'main' into jjant/init-regexes-early
jjant Dec 21, 2022
34d7187
Update codegen-server/src/main/kotlin/software/amazon/smithy/rust/cod…
jjant Dec 21, 2022
8283a1a
Merge branch 'main' into jjant/init-regexes-early
jjant Jan 3, 2023
86bfa1b
Merge branch 'main' into jjant/init-regexes-early
jjant Jan 5, 2023
339b608
Refactor `regex_compiles` test rendering
jjant Jan 5, 2023
f03bf20
Refactor `Symbol.testModule` -> `SymbolProvider.testModuleForShape`
jjant Jan 5, 2023
6808573
Use `RustWriter.unitTest` instead of manually annotating `#[test]`s
jjant Jan 5, 2023
4213e88
Refactor `mapNotNull` -> `filter` + `map`
jjant Jan 5, 2023
39b6ed8
Only render doc comment when there's `@pattern` strings
jjant Jan 5, 2023
732730d
Rename `String.toRustName` -> `´String.unsafeToRustName`
jjant Jan 5, 2023
2873529
Add kotlin test for pattern with lookahead
jjant Jan 5, 2023
cfbdd4b
Merge branch 'main' into jjant/init-regexes-early
jjant Jan 5, 2023
37a9d2a
Merge branch 'main' into jjant/init-regexes-early
jjant Jan 6, 2023
9deedeb
Update `Attribute.Cfg` to new `Attribute.CfgTest`
jjant Jan 6, 2023
8475b14
Prepend `@pattern` docs
jjant Jan 9, 2023
9b21c59
Merge branch 'main' into jjant/init-regexes-early
jjant Jan 9, 2023
f9c1a98
Fix test name
jjant Jan 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ package software.amazon.smithy.rust.codegen.server.python.smithy.generators
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolSupport
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerServiceGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator
Expand All @@ -26,7 +26,7 @@ class PythonServerServiceGenerator(
protocolGenerator: ServerProtocolGenerator,
protocolSupport: ProtocolSupport,
protocol: ServerProtocol,
private val context: CodegenContext,
private val context: ServerCodegenContext,
) : ServerServiceGenerator(rustCrate, protocolGenerator, protocolSupport, protocol, context) {

override fun renderCombinedErrors(writer: RustWriter, operation: OperationShape) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ private data class Pattern(val patternTrait: PatternTrait) : StringTraitInfo() {
}
}

/// Attempts to compile the regex for this constrained type's `@pattern`.
/// This can fail if the specified regex is not supported by the #{Regex} crate.
jjant marked this conversation as resolved.
Show resolved Hide resolved
pub fn compile_regex() -> &'static #{Regex}::Regex {
static REGEX: #{OnceCell}::sync::Lazy<#{Regex}::Regex> = #{OnceCell}::sync::Lazy::new(|| #{Regex}::Regex::new(r##"$pattern"##).expect(r##"$errorMessageForUnsupportedRegex"##));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.join
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.ErrorsModule
import software.amazon.smithy.rust.codegen.core.smithy.InputsModule
import software.amazon.smithy.rust.codegen.core.smithy.OutputsModule
Expand All @@ -26,6 +25,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.Proto
import software.amazon.smithy.rust.codegen.core.util.toPascalCase
import software.amazon.smithy.rust.codegen.core.util.toSnakeCase
import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolTestGenerator
Expand All @@ -41,7 +41,7 @@ open class ServerServiceGenerator(
private val protocolGenerator: ServerProtocolGenerator,
private val protocolSupport: ProtocolSupport,
val protocol: ServerProtocol,
private val codegenContext: CodegenContext,
private val codegenContext: ServerCodegenContext,
) {
private val index = TopDownIndex.of(codegenContext.model)
protected val operations = index.getContainedOperations(codegenContext.serviceShape).sortedBy { it.id }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
package software.amazon.smithy.rust.codegen.server.smithy.generators

import software.amazon.smithy.model.knowledge.TopDownIndex
import software.amazon.smithy.model.neighbor.Walker
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.model.traits.PatternTrait
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWords
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
Expand All @@ -16,14 +19,15 @@ import software.amazon.smithy.rust.codegen.core.rustlang.join
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.util.hasTrait
import software.amazon.smithy.rust.codegen.core.util.toPascalCase
import software.amazon.smithy.rust.codegen.core.util.toSnakeCase
import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol

class ServerServiceGeneratorV2(
private val codegenContext: CodegenContext,
private val codegenContext: ServerCodegenContext,
private val protocol: ServerProtocol,
) {
private val runtimeConfig = codegenContext.runtimeConfig
Expand Down Expand Up @@ -193,6 +197,7 @@ class ServerServiceGeneratorV2(
)
}
}

rustTemplate(
"""
/// Constructs a [`$serviceName`] from the arguments provided to the builder.
Expand All @@ -213,6 +218,10 @@ class ServerServiceGeneratorV2(
});
}
let $expectMessageVariableName = "this should never panic since we are supposed to check beforehand that a handler has been registered for this operation; please file a bug report under https://github.com/awslabs/smithy-rs/issues";

// Eagerly initialize regexes for `@pattern` strings.
jjant marked this conversation as resolved.
Show resolved Hide resolved
#{PatternInitializations:W}

#{Router}::from_iter([#{RoutesArrayElements:W}])
};
Ok($serviceName {
Expand All @@ -224,9 +233,28 @@ class ServerServiceGeneratorV2(
"NullabilityChecks" to nullabilityChecks,
"RoutesArrayElements" to routesArrayElements,
"SmithyHttpServer" to smithyHttpServer,
"PatternInitializations" to patternInitializations(),
)
}

/**
* Renders `PatternString::compile_regex()` function calls for every
* `@pattern`-constrained string shape in the service closure.
*/
@Suppress("DEPRECATION")
private fun patternInitializations(): Writable =
Walker(model).walkShapes(service).mapNotNull { shape ->
jjant marked this conversation as resolved.
Show resolved Hide resolved
if (shape is StringShape && shape.hasTrait<PatternTrait>() && !shape.hasTrait<software.amazon.smithy.model.traits.EnumTrait>()) {
jjant marked this conversation as resolved.
Show resolved Hide resolved
codegenContext.constrainedShapeSymbolProvider.toSymbol(shape)
} else {
null
}
}.map { symbol ->
writable {
rustTemplate("#{Type}::compile_regex();", "Type" to symbol)
}
}.join("")

private fun buildUncheckedMethod(): Writable = writable {
val pairs = writable {
for (operationShape in operations) {
Expand Down