Skip to content

Commit

Permalink
Define JSON variant
Browse files Browse the repository at this point in the history
  • Loading branch information
dhpiggott committed Mar 6, 2024
1 parent 185b0bd commit a741ba6
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ alloy.StructurePatternTrait$Provider
alloy.UrlFormFlattenedTrait$Provider
alloy.UrlFormNameTrait$Provider
alloy.UncheckedExamplesTrait$Provider
alloy.UnknownFieldRetentionTrait$Provider
alloy.UnknownDocumentFieldRetentionTrait$Provider
alloy.UnknownJsonFieldRetentionTrait$Provider
alloy.UntaggedUnionTrait$Provider
alloy.UuidFormatTrait$Provider
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ $version: "2"
namespace alloy

/// Retain unknown fields of a containing structure in a map.
/// This variant controls the behaviour for document codecs.
@trait(
selector: "structure > member :test(> document)"
)
structure unknownFieldRetention {}
structure unknownDocumentFieldRetention {}

/// Retain unknown fields of a containing structure in a map.
/// This variant controls the behaviour for JSON codecs.
@trait(
selector: "structure > member :test(> document)"
)
structure unknownJsonFieldRetention {}
34 changes: 34 additions & 0 deletions modules/core/src/alloy/UnknownDocumentFieldRetentionTrait.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright 2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package alloy;

import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.AnnotationTrait;

public final class UnknownDocumentFieldRetentionTrait extends AnnotationTrait {
public static ShapeId ID = ShapeId.from("alloy#unknownDocumentFieldRetention");

public UnknownDocumentFieldRetentionTrait() {
super(ID, Node.objectNode());
}

public static final class Provider extends AnnotationTrait.Provider<UnknownDocumentFieldRetentionTrait> {
public Provider() {
super(ID, (node) -> new UnknownDocumentFieldRetentionTrait());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.AnnotationTrait;

public final class UnknownFieldRetentionTrait extends AnnotationTrait {
public static ShapeId ID = ShapeId.from("alloy#unknownFieldRetention");
public final class UnknownJsonFieldRetentionTrait extends AnnotationTrait {
public static ShapeId ID = ShapeId.from("alloy#unknownJsonFieldRetention");

public UnknownFieldRetentionTrait() {
public UnknownJsonFieldRetentionTrait() {
super(ID, Node.objectNode());
}

public static final class Provider extends AnnotationTrait.Provider<UnknownFieldRetentionTrait> {
public static final class Provider extends AnnotationTrait.Provider<UnknownJsonFieldRetentionTrait> {
public Provider() {
super(ID, (node) -> new UnknownFieldRetentionTrait());
super(ID, (node) -> new UnknownJsonFieldRetentionTrait());
}
}
}
6 changes: 4 additions & 2 deletions modules/core/test/resources/META-INF/smithy/traits.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use alloy#openEnum
use alloy#simpleRestJson
use alloy#structurePattern
use alloy#uncheckedExamples
use alloy#unknownFieldRetention
use alloy#unknownDocumentFieldRetention
use alloy#unknownJsonFieldRetention
use alloy#untagged
use alloy#urlFormFlattened
use alloy#urlFormName
Expand Down Expand Up @@ -210,6 +211,7 @@ structure TestUrlFormName {
structure TestUnknownFieldRetention {
foo: String
bar: String
@unknownFieldRetention
@unknownDocumentFieldRetention
@unknownJsonFieldRetention
bazes: Document
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright 2022 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package alloy

import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.shapes.DocumentShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.Model
import java.util.Optional

final class UnknownDocumentFieldRetentionTraitProviderSpec
extends munit.FunSuite {

test("has trait") {
val documentShape = DocumentShape
.builder()
.id(ShapeId.fromParts("test", "MyDocument"))
.build()
val structId = ShapeId.fromParts("test", "MyStruct")
val targetId = structId.withMember("myMap")
val structShape = StructureShape
.builder()
.id(structId)
.addMember(
MemberShape
.builder()
.id(targetId)
.target(documentShape.getId)
.addTrait(new UnknownDocumentFieldRetentionTrait)
.build()
)
.build()

val model =
Model.assembler.disableValidation
.addShapes(structShape, documentShape)
.assemble()
.unwrap()

val result = model
.getShape(targetId)
.map(shape => shape.hasTrait(classOf[UnknownDocumentFieldRetentionTrait]))

assertEquals(result, Optional.of(true))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.Model
import java.util.Optional

final class UnknownFieldRetentionTraitProviderSpec extends munit.FunSuite {
final class UnknownJsonFieldRetentionTraitProviderSpec extends munit.FunSuite {

test("has trait") {
val documentShape = DocumentShape
Expand All @@ -39,7 +39,7 @@ final class UnknownFieldRetentionTraitProviderSpec extends munit.FunSuite {
.builder()
.id(targetId)
.target(documentShape.getId)
.addTrait(new UnknownFieldRetentionTrait)
.addTrait(new UnknownJsonFieldRetentionTrait)
.build()
)
.build()
Expand All @@ -52,7 +52,7 @@ final class UnknownFieldRetentionTraitProviderSpec extends munit.FunSuite {

val result = model
.getShape(targetId)
.map(shape => shape.hasTrait(classOf[UnknownFieldRetentionTrait]))
.map(shape => shape.hasTrait(classOf[UnknownJsonFieldRetentionTrait]))

assertEquals(result, Optional.of(true))
}
Expand Down

0 comments on commit a741ba6

Please sign in to comment.