-
Notifications
You must be signed in to change notification settings - Fork 180
/
SymbolMetadataProvider.kt
179 lines (157 loc) · 7.65 KB
/
SymbolMetadataProvider.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.core.smithy
import software.amazon.smithy.codegen.core.CodegenException
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.CollectionShape
import software.amazon.smithy.model.shapes.ListShape
import software.amazon.smithy.model.shapes.MapShape
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.NumberShape
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.model.traits.EnumDefinition
import software.amazon.smithy.model.traits.EnumTrait
import software.amazon.smithy.model.traits.SensitiveTrait
import software.amazon.smithy.model.traits.StreamingTrait
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.RustMetadata
import software.amazon.smithy.rust.codegen.core.rustlang.Visibility
import software.amazon.smithy.rust.codegen.core.util.hasTrait
/**
* Default delegator to enable easily decorating another symbol provider.
*/
open class WrappingSymbolProvider(private val base: RustSymbolProvider) : RustSymbolProvider {
override fun config(): SymbolVisitorConfig {
return base.config()
}
override fun toEnumVariantName(definition: EnumDefinition): MaybeRenamed? {
return base.toEnumVariantName(definition)
}
override fun toSymbol(shape: Shape): Symbol {
return base.toSymbol(shape)
}
override fun toMemberName(shape: MemberShape): String {
return base.toMemberName(shape)
}
}
/**
* Attach `meta` to symbols. `meta` is used by the generators (e.g. StructureGenerator) to configure the generated models.
*
* Protocols may inherit from this class and override the `xyzMeta` methods to modify structure generation.
*/
abstract class SymbolMetadataProvider(private val base: RustSymbolProvider) : WrappingSymbolProvider(base) {
override fun toSymbol(shape: Shape): Symbol {
val baseSymbol = base.toSymbol(shape)
val meta = when (shape) {
is MemberShape -> memberMeta(shape)
is StructureShape -> structureMeta(shape)
is UnionShape -> unionMeta(shape)
is ListShape -> listMeta(shape)
is MapShape -> mapMeta(shape)
is NumberShape -> numberMeta(shape)
is BlobShape -> blobMeta(shape)
is StringShape -> if (shape.hasTrait<EnumTrait>()) {
enumMeta(shape)
} else {
stringMeta(shape)
}
else -> null
}
return baseSymbol.toBuilder().meta(meta).build()
}
abstract fun memberMeta(memberShape: MemberShape): RustMetadata
abstract fun structureMeta(structureShape: StructureShape): RustMetadata
abstract fun unionMeta(unionShape: UnionShape): RustMetadata
abstract fun enumMeta(stringShape: StringShape): RustMetadata
abstract fun listMeta(listShape: ListShape): RustMetadata
abstract fun mapMeta(mapShape: MapShape): RustMetadata
abstract fun stringMeta(stringShape: StringShape): RustMetadata
abstract fun numberMeta(numberShape: NumberShape): RustMetadata
abstract fun blobMeta(blobShape: BlobShape): RustMetadata
}
fun containerDefaultMetadata(
shape: Shape,
model: Model,
additionalAttributes: List<Attribute> = emptyList(),
): RustMetadata {
val defaultDerives = setOf(RuntimeType.Debug, RuntimeType.PartialEq, RuntimeType.Clone)
val isSensitive = shape.hasTrait<SensitiveTrait>() ||
// Checking the shape's direct members for the sensitive trait should suffice.
// Whether their descendants, i.e. a member's member, is sensitive does not
// affect the inclusion/exclusion of the derived `Debug` trait of _this_ container
// shape; any sensitive descendant should still be printed as redacted.
shape.members().any { it.getMemberTrait(model, SensitiveTrait::class.java).isPresent }
val setOfDerives = if (isSensitive) {
defaultDerives - RuntimeType.Debug
} else {
defaultDerives
}
return RustMetadata(
setOfDerives,
additionalAttributes,
Visibility.PUBLIC,
)
}
/**
* The base metadata supports a set of attributes that are used by generators to decorate code.
*
* By default we apply `#[non_exhaustive]` in [additionalAttributes] only to client structures since breaking model
* changes are fine when generating server code.
*/
class BaseSymbolMetadataProvider(
base: RustSymbolProvider,
private val model: Model,
private val additionalAttributes: List<Attribute>,
) : SymbolMetadataProvider(base) {
override fun memberMeta(memberShape: MemberShape): RustMetadata =
when (val container = model.expectShape(memberShape.container)) {
is StructureShape -> {
// TODO(https://github.com/awslabs/smithy-rs/issues/943): Once streaming accessors are usable,
// then also make streaming members `#[doc(hidden)]`
if (memberShape.getMemberTrait(model, StreamingTrait::class.java).isPresent) {
RustMetadata(visibility = Visibility.PUBLIC)
} else {
RustMetadata(
// At some point, visibility _may_ be made `PRIVATE`, so make these `#[doc(hidden)]` for now.
visibility = Visibility.PUBLIC,
additionalAttributes = listOf(Attribute.DocHidden),
)
}
}
is UnionShape, is CollectionShape, is MapShape -> RustMetadata(visibility = Visibility.PUBLIC)
else -> TODO("Unrecognized container type: $container")
}
override fun structureMeta(structureShape: StructureShape) = containerDefaultMetadata(structureShape, model, additionalAttributes)
override fun unionMeta(unionShape: UnionShape) = containerDefaultMetadata(unionShape, model, additionalAttributes)
override fun enumMeta(stringShape: StringShape): RustMetadata =
containerDefaultMetadata(stringShape, model, additionalAttributes).withDerives(
// Smithy's `enum` shapes can additionally be `Eq`, `PartialOrd`, `Ord`, and `Hash` because they can
// only contain strings.
RuntimeType.Eq,
RuntimeType.PartialOrd,
RuntimeType.Ord,
RuntimeType.Hash,
)
// Only the server subproject uses these, so we provide a sane and conservative default implementation here so that
// the rest of symbol metadata providers can just delegate to it.
private val defaultRustMetadata = RustMetadata(visibility = Visibility.PRIVATE)
override fun listMeta(listShape: ListShape) = defaultRustMetadata
override fun mapMeta(mapShape: MapShape) = defaultRustMetadata
override fun stringMeta(stringShape: StringShape) = defaultRustMetadata
override fun numberMeta(numberShape: NumberShape) = defaultRustMetadata
override fun blobMeta(blobShape: BlobShape) = defaultRustMetadata
}
private const val META_KEY = "meta"
fun Symbol.Builder.meta(rustMetadata: RustMetadata?): Symbol.Builder = this.putProperty(META_KEY, rustMetadata)
fun Symbol.expectRustMetadata(): RustMetadata = this.getProperty(META_KEY, RustMetadata::class.java).orElseThrow {
CodegenException(
"Expected `$this` to have metadata attached but it did not.",
)
}