Skip to content

Commit

Permalink
Update trait examples to use smithy-trait-package plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hpmellema committed Jul 24, 2024
1 parent fd7d1b4 commit d7f563e
Show file tree
Hide file tree
Showing 36 changed files with 98 additions and 248 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
description = "A package used to define an annotation trait"

plugins {
`java-library`
id("com.github.spotbugs").version("4.7.1")
id("software.amazon.smithy.gradle.smithy-jar")
id("software.amazon.smithy.gradle.smithy-trait-package")
}

java {
Expand Down Expand Up @@ -55,11 +54,5 @@ dependencies {
val smithyVersion: String by project

smithyCli("software.amazon.smithy:smithy-cli:$smithyVersion")

implementation("software.amazon.smithy:smithy-model:$smithyVersion")
}

smithy {
// Set to an empty list because no smithy-build config files are used
smithyBuildConfigs.set(project.files())
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$version: "2.0"

namespace smithy.example
namespace example.traits

@trait(selector: "structure")
structure special {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rootProject.name = "custom-annotation-trait"
pluginManagement {
val smithyGradleVersion: String by settings
plugins {
id("software.amazon.smithy.gradle.smithy-jar").version(smithyGradleVersion)
id("software.amazon.smithy.gradle.smithy-trait-package").version(smithyGradleVersion)
}

repositories {
Expand Down
14 changes: 12 additions & 2 deletions custom-trait-examples/custom-annotation-trait/smithy-build.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"version": "1.0"
}
"version": "1.0",
"plugins": {
"trait-codegen": {
"package": "io.examples.traits",
"namespace": "example.traits",
"header": [
"Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.",
"SPDX-License-Identifier: MIT-0"
]
}
}
}

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions custom-trait-examples/custom-string-trait/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Finally, update the `io.smithy.examples.traits.JsonNameTrait` entry in the
`src/main/resources/META-INF/services/software.amazon.smithy.model.traits.TraitService` file to reflect the
new name and package of your updated trait.

If you updated the validator, also update the `io.smithy.examples.validators.JsonNameTraitValidator`
If you updated the validator, also update the `io.examples.validators.JsonNameTraitValidator`
entry in the `src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator`
file as well.

Expand All @@ -46,4 +46,4 @@ depend on this package.

## Distribution
If you want to distribute the JAR file create by this package, consider adding the
[maven-publish](https://docs.gradle.org/current/userguide/publishing_maven.html) plugin to the project to publish the JAR to a maven repository.
[maven-publish](https://docs.gradle.org/current/userguide/publishing_maven.html) plugin to the project to publish the JAR to a maven repository.
8 changes: 1 addition & 7 deletions custom-trait-examples/custom-string-trait/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
description = "Package for a custom Smithy string trait"

plugins {
`java-library`
id("com.github.spotbugs").version("4.7.3")
id("software.amazon.smithy.gradle.smithy-jar")
id("software.amazon.smithy.gradle.smithy-trait-package")
}

java {
Expand Down Expand Up @@ -63,8 +62,3 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.10.3")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.10.3")
}

smithy {
// Set to an empty list because no smithy-build config files are used
smithyBuildConfigs.set(project.files())
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$version: "2.0"

namespace io.smithy.example
namespace example.traits

@trait(selector: "member")
string jsonName
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rootProject.name = "custom-string-trait"
pluginManagement {
val smithyGradleVersion: String by settings
plugins {
id("software.amazon.smithy.gradle.smithy-jar").version(smithyGradleVersion)
id("software.amazon.smithy.gradle.smithy-trait-package").version(smithyGradleVersion)
}

repositories {
Expand Down
13 changes: 13 additions & 0 deletions custom-trait-examples/custom-string-trait/smithy-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1.0",
"plugins": {
"trait-codegen": {
"package": "io.examples.traits",
"namespace": "example.traits",
"header": [
"Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.",
"SPDX-License-Identifier: MIT-0"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* SPDX-License-Identifier: MIT-0
*/

package io.smithy.examples.validators;
package io.examples.validators;

import io.smithy.examples.traits.JsonNameTrait;
import io.examples.traits.JsonNameTrait;
import java.util.List;
import java.util.stream.Collectors;
import software.amazon.smithy.model.Model;
Expand All @@ -17,11 +17,11 @@ public class JsonNameTraitValidator extends AbstractValidator {
@Override
public List<ValidationEvent> validate(Model model) {
return model.shapes()
.filter(shape -> shape.hasTrait(JsonNameTrait.class))
.filter(Shape::isMemberShape)
.filter(shape -> shape.hasTrait(software.amazon.smithy.model.traits.JsonNameTrait.class))
.map(shape -> error(shape,
"This shape cannot apply both the root jsonName trait and the custom jsonName trait!"))
.collect(Collectors.toList());
.filter(shape -> shape.hasTrait(JsonNameTrait.class))
.filter(Shape::isMemberShape)
.filter(shape -> shape.hasTrait(software.amazon.smithy.model.traits.JsonNameTrait.class))
.map(shape -> error(shape,
"This shape cannot apply both the root jsonName trait and the custom jsonName trait!"))
.collect(Collectors.toList());
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.smithy.examples.validators.JsonNameTraitValidator
io.examples.validators.JsonNameTraitValidator
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.smithy.examples.traits;
package io.examples.traits;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Objects;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.Shape;
Expand All @@ -13,7 +14,7 @@ public class JsonNameTraitTest {
public void loadsFromModel() {
Model result = Model.assembler()
.discoverModels(getClass().getClassLoader())
.addImport(getClass().getResource("custom-trait-test.smithy"))
.addImport(Objects.requireNonNull(getClass().getResource("custom-trait-test.smithy")))
.assemble()
.unwrap();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.smithy.examples.validators;
package io.examples.validators;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Objects;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ShapeId;
Expand All @@ -13,14 +14,14 @@ public class JsonNameTraitValidatorTest {
public void dectectsViolationModel() {
ValidatedResult<Model> validatedResult = Model.assembler()
.discoverModels(getClass().getClassLoader())
.addImport(getClass().getResource("validator-test.smithy"))
.addImport(Objects.requireNonNull(getClass().getResource("validator-test.smithy")))
.assemble();
var validationEvents = validatedResult.getValidationEvents();

assertEquals(validationEvents.size(), 1);
var validationEvent = validationEvents.get(0);
assertEquals(validationEvent.getId(), "JsonNameTrait");
assertEquals(validationEvent.getSeverity(), Severity.ERROR);
assertEquals(validationEvent.getShapeId().get(), ShapeId.from("io.smithy.example.test#TestStructure$testMember"));
assertEquals(validationEvent.getShapeId().get(), ShapeId.from("example.test#TestStructure$testMember"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ $version: "2.0"

namespace io.smithy.example.test

use io.smithy.example#jsonName
use example.traits#jsonName

structure TestStructure {
@jsonName("TESTING")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
$version: "2.0"

namespace io.smithy.example.test
namespace example.test

structure TestStructure {
@io.smithy.example#jsonName("TESTING")
@example.traits#jsonName("TESTING")
@smithy.api#jsonName("OOPS! DUPLICATION!")
testMember: String
}
4 changes: 2 additions & 2 deletions custom-trait-examples/custom-structure-trait/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Finally, update the `io.smithy.examples.traits.ResourceMetadataTrait` entry in t
`src/main/resources/META-INF/services/software.amazon.smithy.model.traits.TraitService` file to reflect the
new name and package of your updated trait.

If you updated the validator, also update the `io.smithy.examples.validators.ResourceMetadataTraitValidator`
If you updated the validator, also update the `io.examples.validators.ResourceMetadataTraitValidator`
entry in the `src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator`
file as well.

Expand All @@ -46,4 +46,4 @@ depend on this package.

## Distribution
If you want to distribute the JAR file create by this package, consider adding the
[maven-publish](https://docs.gradle.org/current/userguide/publishing_maven.html) plugin to the project to publish the JAR to a maven repository.
[maven-publish](https://docs.gradle.org/current/userguide/publishing_maven.html) plugin to the project to publish the JAR to a maven repository.
13 changes: 6 additions & 7 deletions custom-trait-examples/custom-structure-trait/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
description = "Custom Smithy structure trait with multiple inputs"

plugins {
`java-library`
id("com.github.spotbugs").version("4.7.3")
id("software.amazon.smithy.gradle.smithy-jar")
id("software.amazon.smithy.gradle.smithy-trait-package")
}

java {
Expand Down Expand Up @@ -41,6 +40,11 @@ spotbugs {
}
}

// Exclude generated code from checkstyle
tasks.withType<Checkstyle>().configureEach {
exclude("io/examples/traits/**")
}

repositories {
mavenLocal()
mavenCentral()
Expand All @@ -58,8 +62,3 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.10.3")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.10.3")
}

smithy {
// Set to an empty list because no smithy-build config files are used
smithyBuildConfigs.set(project.files())
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$version: "2.0"

namespace io.smithy.example
namespace example.traits

@trait(
selector: "resource"
Expand Down Expand Up @@ -31,6 +31,8 @@ enum ResourceType {

@private
list StructureIdList {
@idRef(failWhenMissing: true, selector: "structure")
member: String
member: IdRefString
}

@idRef(failWhenMissing: true, selector: "structure")
string IdRefString
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pluginManagement {
val smithyGradleVersion: String by settings
plugins {
id("software.amazon.smithy.gradle.smithy-jar").version(smithyGradleVersion)
id("software.amazon.smithy.gradle.smithy-trait-package").version(smithyGradleVersion)
}

repositories {
Expand Down
13 changes: 13 additions & 0 deletions custom-trait-examples/custom-structure-trait/smithy-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1.0",
"plugins": {
"trait-codegen": {
"package": "io.examples.traits",
"namespace": "example.traits",
"header": [
"Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.",
"SPDX-License-Identifier: MIT-0"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* SPDX-License-Identifier: MIT-0
*/

package io.smithy.examples.validators;
package io.examples.validators;

import io.smithy.examples.traits.ResourceMetadataTrait;
import io.examples.traits.ResourceMetadataTrait;
import java.util.List;
import java.util.stream.Collectors;
import software.amazon.smithy.model.Model;
Expand Down
Loading

0 comments on commit d7f563e

Please sign in to comment.