Unverified Commit 9c54f1c7 authored by 82marbag's avatar 82marbag Committed by GitHub
Browse files

Implement `@length` on blobs (#2131)



* Length on blobs

Signed-off-by: default avatarDaniele Ahmed <ahmeddan@amazon.de>
parent 5363c41c
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -445,6 +445,11 @@ structure ConA {
    maxLengthString: MaxLengthString,
    fixedLengthString: FixedLengthString,

    lengthBlob: LengthBlob,
    minLengthBlob: MinLengthBlob,
    maxLengthBlob: MaxLengthBlob,
    fixedLengthBlob: FixedLengthBlob,

    rangeInteger: RangeInteger,
    minRangeInteger: MinRangeInteger,
    maxRangeInteger: MaxRangeInteger,
@@ -486,6 +491,12 @@ structure ConA {
    // setOfLengthString: SetOfLengthString,
    mapOfLengthString: MapOfLengthString,

    listOfLengthBlob: ListOfLengthBlob,
    // TODO(https://github.com/awslabs/smithy-rs/issues/1401): a `set` shape is
    //  just a `list` shape with `uniqueItems`, which hasn't been implemented yet.
    // setOfLengthBlob: SetOfLengthBlob,
    mapOfLengthBlob: MapOfLengthBlob,

    listOfRangeInteger: ListOfRangeInteger,
    // TODO(https://github.com/awslabs/smithy-rs/issues/1401): a `set` shape is
    //  just a `list` shape with `uniqueItems`, which hasn't been implemented yet.
@@ -532,6 +543,11 @@ structure ConA {
    // lengthSetOfPatternString: LengthSetOfPatternString,
}

map MapOfLengthBlob {
    key: String,
    value: LengthBlob,
}

map MapOfLengthString {
    key: LengthString,
    value: LengthString,
@@ -640,6 +656,23 @@ string MaxLengthString
@length(min: 69, max: 69)
string FixedLengthString

@length(min: 2, max: 8)
list LengthListOfLengthBlob {
    member: LengthBlob
}

@length(min: 2, max: 70)
blob LengthBlob

@length(min: 2)
blob MinLengthBlob

@length(max: 70)
blob MaxLengthBlob

@length(min: 70, max: 70)
blob FixedLengthBlob

@pattern("[a-d]{5}")
string PatternString

@@ -732,6 +765,10 @@ set SetOfLengthString {
    member: LengthString
}

set SetOfLengthBlob {
    member: LengthBlob
}

set SetOfPatternString {
    member: PatternString
}
@@ -749,6 +786,10 @@ list ListOfLengthString {
    member: LengthString
}

list ListOfLengthBlob {
    member: LengthBlob
}

// TODO(https://github.com/awslabs/smithy-rs/issues/1401): a `set` shape is
//  just a `list` shape with `uniqueItems`, which hasn't been implemented yet.
// set SetOfRangeInteger {
+4 −1
Original line number Diff line number Diff line
@@ -28,12 +28,14 @@ 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.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.escape
import software.amazon.smithy.rust.codegen.core.rustlang.render
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlockTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.withBlock
import software.amazon.smithy.rust.codegen.core.rustlang.withBlockTemplate
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.smithy.CodegenTarget
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
@@ -49,6 +51,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.isRustBoxed
import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpBindingResolver
import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpLocation
import software.amazon.smithy.rust.codegen.core.smithy.protocols.deserializeFunctionName
import software.amazon.smithy.rust.codegen.core.smithy.rustType
import software.amazon.smithy.rust.codegen.core.util.PANIC
import software.amazon.smithy.rust.codegen.core.util.dq
import software.amazon.smithy.rust.codegen.core.util.hasTrait
@@ -296,7 +299,7 @@ class JsonParserGenerator(
    private fun RustWriter.deserializeBlob(target: BlobShape) {
        rustTemplate(
            "#{expect_blob_or_null}(tokens.next())?#{ConvertFrom:W}",
            "ConvertFrom" to typeConversionGenerator.convertViaFrom(target),
            "ConvertFrom" to writable { RuntimeType.blob(runtimeConfig).toSymbol().rustType().render() },
            *codegenScope,
        )
    }
+2 −1
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ package software.amazon.smithy.rust.codegen.server.smithy
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.knowledge.NullableIndex
import software.amazon.smithy.model.shapes.BlobShape
import software.amazon.smithy.model.shapes.ByteShape
import software.amazon.smithy.model.shapes.CollectionShape
import software.amazon.smithy.model.shapes.IntegerShape
@@ -99,7 +100,7 @@ class ConstrainedShapeSymbolProvider(
                }
            }

            is StringShape, is IntegerShape, is ShortShape, is LongShape, is ByteShape -> {
            is StringShape, is IntegerShape, is ShortShape, is LongShape, is ByteShape, is BlobShape -> {
                if (shape.isDirectlyConstrained(base)) {
                    val rustType = RustType.Opaque(shape.contextName(serviceShape).toPascalCase())
                    symbolBuilder(shape, rustType).locatedIn(ModelsModule).build()
+2 −1
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ package software.amazon.smithy.rust.codegen.server.smithy

import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.BlobShape
import software.amazon.smithy.model.shapes.ByteShape
import software.amazon.smithy.model.shapes.CollectionShape
import software.amazon.smithy.model.shapes.IntegerShape
@@ -127,7 +128,7 @@ class ConstraintViolationSymbolProvider(
                    .build()
            }

            is StringShape, is IntegerShape, is ShortShape, is LongShape, is ByteShape -> {
            is StringShape, is IntegerShape, is ShortShape, is LongShape, is ByteShape, is BlobShape -> {
                val module = shape.shapeModule()
                val rustType = RustType.Opaque(constraintViolationName, module.fullyQualifiedPath())
                Symbol.builder()
+3 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ package software.amazon.smithy.rust.codegen.server.smithy
import software.amazon.smithy.codegen.core.SymbolProvider
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.neighbor.Walker
import software.amazon.smithy.model.shapes.BlobShape
import software.amazon.smithy.model.shapes.ByteShape
import software.amazon.smithy.model.shapes.CollectionShape
import software.amazon.smithy.model.shapes.IntegerShape
@@ -90,6 +91,7 @@ fun Shape.isDirectlyConstrained(symbolProvider: SymbolProvider): Boolean = when
    is StringShape -> this.hasTrait<EnumTrait>() || supportedStringConstraintTraits.any { this.hasTrait(it) }
    is CollectionShape -> supportedCollectionConstraintTraits.any { this.hasTrait(it) }
    is IntegerShape, is ShortShape, is LongShape, is ByteShape -> this.hasTrait<RangeTrait>()
    is BlobShape -> this.hasTrait<LengthTrait>()
    else -> false
}

@@ -118,6 +120,7 @@ fun Shape.hasPublicConstrainedWrapperTupleType(model: Model, publicConstrainedTy
    is StringShape -> !this.hasTrait<EnumTrait>() && (publicConstrainedTypes && supportedStringConstraintTraits.any(this::hasTrait))
    is IntegerShape, is ShortShape, is LongShape, is ByteShape -> publicConstrainedTypes && this.hasTrait<RangeTrait>()
    is MemberShape -> model.expectShape(this.target).hasPublicConstrainedWrapperTupleType(model, publicConstrainedTypes)
    is BlobShape -> publicConstrainedTypes && this.hasTrait<LengthTrait>()
    else -> false
}

Loading