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

[0.10] Support Smithy IDL Serialization #284

Merged
merged 16 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package software.amazon.smithy.model.shapes;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.stream.Stream;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.utils.IoUtils;

public class SmithyIdlModelSerializerTest {
@TestFactory
JordonPhillips marked this conversation as resolved.
Show resolved Hide resolved
public Stream<DynamicTest> generateTests() throws IOException {
return Files.list(Paths.get(
SmithyIdlModelSerializer.class.getResource("idl-serialization/cases").getPath()))
.map(path -> DynamicTest.dynamicTest(path.getFileName().toString(), () -> testConversion(path)));
}

public void testConversion(Path path) {
Model model = Model.assembler().addImport(path).assemble().unwrap();
SmithyIdlModelSerializer serializer = SmithyIdlModelSerializer.builder().build();
Map<Path, String> serialized = serializer.serialize(model);

if (serialized.size() != 1) {
throw new RuntimeException("Exactly one smithy file should be output for generated tests.");
}

String serializedString = serialized.entrySet().iterator().next().getValue();
assertThat(serializedString, equalTo(IoUtils.readUtf8File(path)));
}

@Test
public void multipleNamespacesGenerateMultipleFiles() {
Model model = Model.assembler()
.addImport(getClass().getResource("idl-serialization/multiple-namespaces/input.json"))
.assemble()
.unwrap();
SmithyIdlModelSerializer serializer = SmithyIdlModelSerializer.builder().build();
Map<Path, String> serialized = serializer.serialize(model);

Path outputDir = Paths.get(getClass().getResource("idl-serialization/multiple-namespaces/output").getFile());
serialized.forEach((path, generated) -> {
Path expectedPath = outputDir.resolve(path);
assertThat(generated, equalTo(IoUtils.readUtf8File(expectedPath)));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$version: "0.5.0"

namespace ns.foo

/// This trait isn't an annotation trait since it doesn't extend BooleanTrait
@trait
boolean FalseBooleanTrait

@FalseBooleanTrait(true)
@private
string Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$version: "0.5.0"

namespace ns.foo

@trait
list ListTrait {
member: String,
}

@trait
set SetTrait {
member: String,
}

@ListTrait([])
@SetTrait([])
string Bar

@ListTrait([
"first",
"second",
])
@SetTrait([
"first",
"second",
])
string Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
$version: "0.5.0"

namespace ns.foo

@trait
document DocumentTrait

@DocumentTrait(false)
string Boolean

@DocumentTrait([
"foo",
])
string List

@DocumentTrait(
JordonPhillips marked this conversation as resolved.
Show resolved Hide resolved
foo: "bar",
)
string Map

@DocumentTrait(null)
string Null

@DocumentTrait(123)
string Number

@DocumentTrait("foo")
string String
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$version: "0.5.0"

namespace ns.foo

/// Documentation comments are used.
/// $ is escaped
string Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
$version: "0.5.0"

metadata example.array = [
10,
true,
"hello",
]
metadata example.bool1 = true
metadata example.bool2 = false
metadata example.null = null
metadata example.number = 10
metadata example.object = {
foo: "baz",
}
metadata example.string = "hello there"
metadata "key must be quoted" = true

namespace ns.foo

string Placeholder
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
$version: "0.5.0"

namespace ns.foo

@trait
bigDecimal BigDecimalTrait

@trait
bigInteger BigIntegerTrait

@trait
byte ByteTrait

@trait
double DoubleTrait

@trait
float FloatTrait

@trait
integer IntegerTrait

@trait
long LongTrait

@trait
short ShortTrait

@BigDecimalTrait(3.14)
@BigIntegerTrait(123)
@ByteTrait(123)
@DoubleTrait(1.234)
@FloatTrait(-1.234)
@IntegerTrait(-123)
@LongTrait(321)
@ShortTrait(12321)
string Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
$version: "0.5.0"

namespace ns.foo

@trait
map MapTrait {
key: String,
value: String,
}

@trait
structure StructureTrait {
collectionMember: StringList,
nestedMember: NestedMember,
shapeIdMember: ShapeId,
staticMember: String,
}

@trait
union UnionTrait {
boolean: Boolean,
string: String,
}

structure NestedMember {
shapeIds: ShapeIdList,
}

list ShapeIdList {
member: ShapeId,
}

list StringList {
member: String,
}

@MapTrait
@StructureTrait
string EmptyBody

@MapTrait(
bar: "baz",
foo: "bar",
"must be quoted": "bam",
)
@StructureTrait(
collectionMember: [
"foo",
"bar",
],
nestedMember: {
shapeIds: [
String,
EmptyBody,
],
},
shapeIdMember: UnionTrait,
staticMember: "Foo",
)
@UnionTrait(
boolean: false,
)
string NonEmptyBody

@idRef
string ShapeId
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$version: "0.5.0"

namespace ns.foo

structure Structure {
localWithConflict: String,
prelude: Blob,
preludeWithConflict: smithy.api#String,
}

string String
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
$version: "0.5.0"

namespace ns.foo

service EmptyService {
version: "2020-02-18",
}

service MyService {
version: "2020-02-18",
operations: [
MyOperation,
],
resources: [
MyResource,
],
}

resource EmptyResource {}

resource MyResource {
identifiers: {
id: String,
},
put: ResourceOperation,
create: ResourceOperation,
read: ReadonlyResourceOperation,
update: ResourceOperation,
delete: ResourceOperation,
list: ReadonlyResourceOperation,
operations: [
ResourceOperation,
],
collectionOperations: [
ResourceOperation,
],
resources: [
SubResource,
],
}

resource SubResource {
identifiers: {
id: String,
},
}

operation EmptyOperation {}

operation MyOperation {
input: InputOutput,
output: InputOutput,
errors: [
Error,
],
}

@readonly
operation ReadonlyResourceOperation {
input: ResourceOperationInput,
}

@idempotent
operation ResourceOperation {
input: ResourceOperationInput,
}

@error("client")
structure Error {}

structure InputOutput {}

structure ResourceOperationInput {
id: String,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
$version: "0.5.0"

namespace ns.foo

union Union {
byte: Byte,
double: Double,
}

structure StructureWithMembers {
a: String,
b: String,
}

structure StructureWithoutMembers {}

list List {
member: String,
}

set Set {
member: String,
}

map Map {
key: String,
value: String,
}

bigDecimal BigDecimal

bigInteger BigInteger

blob Blob

boolean Boolean

byte Byte

document Document

double Double

float Float

integer Integer

long Long

short Short

string String
Loading