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

Stable ordering of traits? #2492

Open
kubukoz opened this issue Dec 21, 2024 · 0 comments
Open

Stable ordering of traits? #2492

kubukoz opened this issue Dec 21, 2024 · 0 comments

Comments

@kubukoz
Copy link
Contributor

kubukoz commented Dec 21, 2024

Hi! I ran into what appears to be a discrepancy between how traits get ordered between multiple machines. For example, for this shape:

structure TraitOrderingTest {
    @required
    @length(min: 3)
    ident: String
}

My CI machine (Linux, x86) shows required first, then length (when traits are queried via model.expectShape(ShapeId.from("example.public#TraitOrderingTest$ident")).getAllTraits()).

My local machine (Mac, arm64) shows length first.

This doesn't seem to be just a difference in the file location (which has been a culprit before), as I tried the CI path and it yields the same result as my local path.

Here's a scala-cli snippet to quickly see the order (and the reported line numbers of the traits, which are correct):

//> using dep software.amazon.smithy:smithy-model:1.53.0
//> using dep com.lihaoyi::os-lib:0.11.3
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.ShapeId
import scala.jdk.CollectionConverters.*

def showcase(name: String) = {
  val model = Model
    .assembler()
    .addUnparsedModel(
      name,
      """$version: "2"
        |
        |namespace example.public
        |
        |structure AnotherModel {
        |    @required
        |    @length(min: 3)
        |    ident: String
        |}
        |
        |structure TraitOrderingTest {
        |    @required
        |    @length(min: 3)
        |    ident: String
        |}
        |""".stripMargin,
    )
    .assemble()
    .unwrap()

  println(model.expectShape(ShapeId.from("example.public#TraitOrderingTest$ident")).getAllTraits())
  println(
    model
      .expectShape(ShapeId.from("example.public#TraitOrderingTest$ident"))
      .getAllTraits()
      .asScala
      .values
      .toList
      .map(t => t.getSourceLocation().getLine())
  )
}

@main def demo = showcase("example-service.smithy")

On my machine, it outputs:

{smithy.api#length=Trait `smithy.api#length`, defined at example-service.smithy [13, 5], smithy.api#required=software.amazon.smithy.model.traits.RequiredTrait@f4b52789}
List(13, 12)

I suppose the question is: is this OK, or should traits be returned in the order they're written? Or at least, in deterministic order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant