Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
70 changes: 21 additions & 49 deletions private/smithy-rpcv2-cbor-schema/src/schemas/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
const _B = "Boolean";
const _BL = "BooleanList";
const _BLl = "BlobList";
const _Bl = "Blob";
const _By = "Byte";
const _CE = "ComplexError";
const _CNED = "ComplexNestedErrorData";
const _COD = "ClientOptionalDefaults";
const _D = "Double";
const _DBM = "DenseBooleanMap";
const _DNM = "DenseNumberMap";
const _DSM = "DenseStringMap";
const _DSMe = "DenseSetMap";
const _DSMen = "DenseStructMap";
const _DT = "DateTime";
const _De = "Defaults";
const _D = "Defaults";
const _DSM = "DenseSetMap";
const _DSMe = "DenseStructMap";
const _EIO = "EmptyInputOutput";
const _ES = "EmptyStructure";
const _F = "Float";
const _FE = "FooEnum";
const _FEL = "FooEnumList";
const _F = "Foo";
const _FO = "Float16Output";
const _FS = "FractionalSeconds";
const _FSO = "FractionalSecondsOutput";
const _Fl = "Float16";
const _Fo = "Foo";
const _GS = "GreetingStruct";
const _GWE = "GreetingWithErrors";
const _GWEO = "GreetingWithErrorsOutput";
const _I = "Integer";
const _IE = "IntegerEnum";
const _IEL = "IntegerEnumList";
const _IG = "InvalidGreeting";
const _IL = "IntegerList";
const _L = "Long";
const _M = "Message";
const _N = "Nested";
const _NIO = "NoInputOutput";
Expand All @@ -51,30 +33,20 @@ const _RVCL = "RpcV2CborLists";
const _RVCLIO = "RpcV2CborListInputOutput";
const _RVCSM = "RpcV2CborSparseMaps";
const _RVCSMIO = "RpcV2CborSparseMapsInputOutput";
const _S = "String";
const _SBM = "SparseBooleanMap";
const _SL = "StringList";
const _SL = "StructureList";
const _SLM = "StructureListMember";
const _SLt = "StructureList";
const _SNM = "SparseNumberMap";
const _SNO = "SparseNullsOperation";
const _SNOIO = "SparseNullsOperationInputOutput";
const _SS = "StringSet";
const _SS = "SimpleStructure";
const _SSL = "SparseStringList";
const _SSM = "SparseStringMap";
const _SSMp = "SparseSetMap";
const _SSMpa = "SparseStructMap";
const _SSM = "SparseSetMap";
const _SSMp = "SparseStructMap";
const _SSMpa = "SparseStringMap";
const _SSP = "SimpleScalarProperties";
const _SSS = "SimpleScalarStructure";
const _SSi = "SimpleStructure";
const _Sh = "Short";
const _T = "Timestamp";
const _TE = "TestEnum";
const _TIE = "TestIntEnum";
const _TL = "TimestampList";
const _TLo = "TopLevel";
const _TSL = "TestStringList";
const _TSM = "TestStringMap";
const _TL = "TopLevel";
const _VE = "ValidationException";
const _VEF = "ValidationExceptionField";
const _VEFL = "ValidationExceptionFieldList";
Expand Down Expand Up @@ -189,15 +161,15 @@ export var ComplexError = error(
{
[_e]: _c,
},
[_TLo, _N],
[_TL, _N],
[0, () => ComplexNestedErrorData],

__ComplexError
);
export var ComplexNestedErrorData = struct(n1, _CNED, 0, [_Fo], [0]);
export var ComplexNestedErrorData = struct(n1, _CNED, 0, [_F], [0]);
export var Defaults = struct(
n1,
_De,
_D,
0,
[
_dS,
Expand Down Expand Up @@ -299,7 +271,7 @@ export var RpcV2CborDenseMapsInputOutput = struct(
_RVCDMIO,
0,
[_dSM, _dNM, _dBM, _dSMe, _dSMen],
[() => DenseStructMap, 128 | 1, 128 | 2, 128 | 0, map(n1, _DSMe, 0, 0, 64 | 0)]
[() => DenseStructMap, 128 | 1, 128 | 2, 128 | 0, map(n1, _DSM, 0, 0, 64 | 0)]
);
export var RpcV2CborListInputOutput = struct(
n1,
Expand Down Expand Up @@ -328,7 +300,7 @@ export var SimpleScalarStructure = struct(
[_tBV, _fBV, _bV, _dV, _fV, _iV, _lV, _sV, _sVt, _bVl],
[2, 2, 1, 1, 1, 1, 1, 1, 0, 21]
);
export var SimpleStructure = struct(n1, _SSi, 0, [_v], [0]);
export var SimpleStructure = struct(n1, _SS, 0, [_v], [0]);
export var SparseNullsOperationInputOutput = struct(
n1,
_SNOIO,
Expand All @@ -350,7 +322,7 @@ export var RpcV2ProtocolServiceException = error(
__RpcV2ProtocolServiceException
);
export var ValidationExceptionFieldList = list(n0, _VEFL, 0, () => ValidationExceptionField);
export var StructureList = list(n1, _SLt, 0, () => StructureListMember);
export var StructureList = list(n1, _SL, 0, () => StructureListMember);
export var TestStringList = 64 | 0;

export var BlobList = 64 | 21;
Expand Down Expand Up @@ -382,10 +354,10 @@ export var DenseBooleanMap = 128 | 2;

export var DenseNumberMap = 128 | 1;

export var DenseSetMap = map(n1, _DSMe, 0, 0, 64 | 0);
export var DenseSetMap = map(n1, _DSM, 0, 0, 64 | 0);
export var DenseStringMap = 128 | 0;

export var DenseStructMap = map(n1, _DSMen, 0, 0, () => GreetingStruct);
export var DenseStructMap = map(n1, _DSMe, 0, 0, () => GreetingStruct);
export var SparseBooleanMap = map(
n1,
_SBM,
Expand All @@ -406,7 +378,7 @@ export var SparseNumberMap = map(
);
export var SparseSetMap = map(
n1,
_SSMp,
_SSM,
{
[_s]: 1,
},
Expand All @@ -415,7 +387,7 @@ export var SparseSetMap = map(
);
export var SparseStructMap = map(
n1,
_SSMpa,
_SSMp,
{
[_s]: 1,
},
Expand All @@ -426,7 +398,7 @@ export var TestStringMap = 128 | 0;

export var SparseStringMap = map(
n2,
_SSM,
_SSMpa,
{
[_s]: 1,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public void run() {
*/
private void loadShapes(Shape shape) {
String absoluteName = shape.getId().toString();
String name = shape.getId().getName();

if (shape.isMemberShape()) {
loadShapes(model.expectShape(shape.asMemberShape().get().getTarget()));
Expand All @@ -139,9 +138,6 @@ private void loadShapes(Shape shape) {
return;
}

if (!elision.isReferenceSchema(shape)) {
stringStore.var(name);
}
loadShapesVisited.add(absoluteName);

switch (shape.getType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ public boolean isReferenceSchema(Shape shape) {
}
ShapeType type = targetShape.getType();
switch (type) {
case BOOLEAN, STRING, BYTE, DOUBLE, FLOAT, INTEGER, LONG, SHORT, ENUM, INT_ENUM -> {
return false;
}
case TIMESTAMP, BLOB -> {
case STRING,
BOOLEAN,
BYTE, DOUBLE, FLOAT, SHORT, INTEGER, LONG,
ENUM, INT_ENUM,
BIG_INTEGER, BIG_DECIMAL,
TIMESTAMP, BLOB, DOCUMENT -> {
return false;
}
case LIST, SET, MAP -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.KnowledgeIndex;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.AuthDefinitionTrait;
import software.amazon.smithy.model.traits.EndpointTrait;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.EventHeaderTrait;
Expand All @@ -35,8 +37,8 @@
import software.amazon.smithy.model.traits.SensitiveTrait;
import software.amazon.smithy.model.traits.SparseTrait;
import software.amazon.smithy.model.traits.StreamingTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait;
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.model.traits.TraitDefinition;
import software.amazon.smithy.model.traits.XmlAttributeTrait;
import software.amazon.smithy.model.traits.XmlFlattenedTrait;
import software.amazon.smithy.model.traits.XmlNameTrait;
Expand All @@ -46,11 +48,19 @@

@SmithyInternalApi
final class SchemaTraitFilterIndex implements KnowledgeIndex {
private static final Set<ShapeId> EXCLUDED_TRAITS = SetUtils.of(
// excluded due to special schema handling.
TimestampFormatTrait.ID
);

/**
* All of these are added by scanning the ProtocolDefinition and AuthDefinition meta traits.
* The hard coded initial list is shown as an example of what this set contains.
*/
private final Set<ShapeId> includedTraits = new HashSet<>(
// (wrapped for mutability)
SetUtils.of(
SparseTrait.ID,
// excluded by special schema handling.
// TimestampFormatTrait.ID,
SensitiveTrait.ID,
IdempotencyTokenTrait.ID,
JsonNameTrait.ID,
Expand Down Expand Up @@ -81,15 +91,18 @@ final class SchemaTraitFilterIndex implements KnowledgeIndex {
private final Model model;

SchemaTraitFilterIndex(Model model) {
Set<Shape> shapesWithTrait = model.getShapesWithTrait(ProtocolDefinitionTrait.class);
for (Shape shape : shapesWithTrait) {
// todo(schema) use protocol and authDefinition traits as the initial allowlist.
// System.out.println("shape having authDef: " + shape.getId().getName());
Set<Shape> protocolDefinitionTraits = model.getShapesWithTrait(ProtocolDefinitionTrait.class);
Set<Shape> authDefinitionTraits = model.getShapesWithTrait(AuthDefinitionTrait.class);
Set<Shape> definitionTraits = new TreeSet<>();
definitionTraits.addAll(protocolDefinitionTraits);
definitionTraits.addAll(authDefinitionTraits);

for (Shape shape : definitionTraits) {
shape.getTrait(ProtocolDefinitionTrait.class).ifPresent(protocolDefinitionTrait -> {
protocolDefinitionTrait.getTraits().forEach(traitShapeId -> {
Shape traitShape = model.expectShape(traitShapeId);
TraitDefinition traitDefinition = model.getTraitDefinition(traitShapeId).get();
// System.out.println("\t trait shape: " + traitShapeId.getName());
if (!EXCLUDED_TRAITS.contains(traitShapeId)) {
includedTraits.add(traitShapeId);
}
});
});
}
Expand All @@ -104,6 +117,14 @@ public static SchemaTraitFilterIndex of(Model model) {
return model.getKnowledge(SchemaTraitFilterIndex.class, SchemaTraitFilterIndex::new);
}

/**
* @param traitShapeId - query.
* @return whether trait should be included in schema generation.
*/
public boolean includeTrait(ShapeId traitShapeId) {
return includedTraits.contains(traitShapeId) || SchemaTraitExtension.INSTANCE.contains(traitShapeId);
}

/**
* @param shape - structure or member, usually.
* @return whether it has at least 1 trait that is needed in a schema.
Expand All @@ -112,7 +133,7 @@ public boolean hasSchemaTraits(Shape shape) {
return hasSchemaTraits(shape, 0);
}

public boolean hasSchemaTraits(Shape shape, int depth) {
private boolean hasSchemaTraits(Shape shape, int depth) {
if (cache.containsKey(shape)) {
return cache.get(shape);
}
Expand All @@ -139,12 +160,4 @@ public boolean hasSchemaTraits(Shape shape, int depth) {
cache.put(shape, membersHaveSchemaTraits || targetHasSchemaTraits);
return cache.get(shape);
}

/**
* @param traitShapeId - query.
* @return whether trait should be included in schema generation.
*/
public boolean includeTrait(ShapeId traitShapeId) {
return includedTraits.contains(traitShapeId) || SchemaTraitExtension.INSTANCE.contains(traitShapeId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,6 @@ public String serializeTraitData(Trait trait, StringStore stringStore) {
return stringStore.var(strTrait.getValue());
} else if (ANNOTATION_TRAITS.contains(trait.toShapeId()) && trait instanceof AnnotationTrait) {
return ANNOTATION_TRAIT_VALUE;
} else if (trait instanceof HttpErrorTrait httpError) {
return Objects.toString(httpError.getCode());
} else if (trait instanceof HttpTrait httpTrait) {
return """
["%s", "%s", %s]
""".formatted(
httpTrait.getMethod(),
httpTrait.getUri(),
httpTrait.getCode()
);
} else if (DATA_TRAITS.contains(trait.toShapeId())) {
if (trait instanceof EndpointTrait endpointTrait) {
return """
Expand All @@ -120,6 +110,16 @@ public String serializeTraitData(Trait trait, StringStore stringStore) {
stringStore.var(xmlNamespaceTrait.getPrefix().orElse("")),
stringStore.var(xmlNamespaceTrait.getUri())
);
} else if (trait instanceof HttpErrorTrait httpError) {
return Objects.toString(httpError.getCode());
} else if (trait instanceof HttpTrait httpTrait) {
return """
["%s", "%s", %s]
""".formatted(
httpTrait.getMethod(),
httpTrait.getUri(),
httpTrait.getCode()
);
}
} else if (SchemaTraitExtension.INSTANCE.contains(trait)) {
return SchemaTraitExtension.INSTANCE.render(trait);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class StringStore {
private final TreeMap<String, String> variableToLiteral = new TreeMap<>();

// controls incremental output.
private final Set<String> writelog = new HashSet<>();
private final Set<String> writeLog = new HashSet<>();

public StringStore() {}

Expand Down Expand Up @@ -79,7 +79,7 @@ public String flushVariableDeclarationCode() {
for (Map.Entry<String, String> entry : variableToLiteral.entrySet()) {
String variable = entry.getKey();
String literal = entry.getValue();
if (writelog.add(variable)) {
if (writeLog.add(variable)) {
sourceCode.append(String.format("const %s = \"%s\";%n", variable, literal));
}
}
Expand Down
Loading