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

feat: add scalar kind #7158

Merged
merged 15 commits into from May 6, 2024
Merged

feat: add scalar kind #7158

merged 15 commits into from May 6, 2024

Conversation

jedevc
Copy link
Member

@jedevc jedevc commented Apr 22, 2024

DEV-3403

Fixes #6466

This is the "proper" implementation of #6739. This allows us to preserve the original scalar name.

Implemented functionality:

  • TypeDefs have an AsScalar field, so that they can be connected to their original value.
    • For example, this allows the CLI to know that a flag is a dagger.Platform flag. Eventually, this could allow us to have a special value, like "current" as a shorthand to represent the client platform.
  • Modules can call WithScalar to define their own non-core scalar values - these are represented as scalars in TypeDefs, as well as in the GraphQL schema.
  • Other modules can import another module that have a custom scalar, and use it.

TODO:

  • Implement all the above functionality
  • Add a magical CLI handler for dagger.Platform (this was my motivation for doing all this lol)
  • Add some tests
  • Fixup all TODOs

@jedevc jedevc force-pushed the add-scalar-kind branch 2 times, most recently from fef52da to c6a99ae Compare April 23, 2024 13:51
@jedevc
Copy link
Member Author

jedevc commented Apr 23, 2024

A fun side-effect I just discovered - because of how dagql does enums, we get enum support included alongside this!

@jedevc jedevc force-pushed the add-scalar-kind branch 6 times, most recently from 7567e99 to 63973ac Compare April 25, 2024 11:14
@jedevc jedevc requested a review from helderco April 25, 2024 11:36
@jedevc jedevc marked this pull request as ready for review April 25, 2024 11:36
@jedevc jedevc requested review from a team as code owners April 25, 2024 11:36
@jedevc
Copy link
Member Author

jedevc commented Apr 25, 2024

Note: this is only fully enabled for the Go SDK right now - there will need to be minor updates to both Typescript and Python (cc @TomChv @helderco), so that functions that take a scalar argument mark this using WithScalar to the engine (instead of using WithString).

@helderco
Copy link
Contributor

I'll get on that asap and coordinate with @TomChv. Thanks for pushing this through! 🙏

@wingyplus
Copy link
Contributor

Asking for #6967, is WithString still support?.

cmd/dagger/flags.go Outdated Show resolved Hide resolved
@jedevc
Copy link
Member Author

jedevc commented Apr 30, 2024

Asking for #6967, is WithString still support?.

@wingyplus, yes, WithString is still here - it's just that now, by using WithScalar for a type like Platform, it indicates it as specifically a Platform input, instead of just a normal string - this allows the CLI to have a significantly better interface for it/etc.

@jedevc jedevc force-pushed the add-scalar-kind branch 3 times, most recently from e95637c to 5a3d325 Compare April 30, 2024 11:22
@jedevc jedevc requested a review from helderco April 30, 2024 12:46
@jedevc jedevc force-pushed the add-scalar-kind branch 2 times, most recently from 32a9d04 to 1471be4 Compare April 30, 2024 14:41
@jedevc
Copy link
Member Author

jedevc commented May 1, 2024

@helderco should be ready for another review now - do we want to land the typescript/python work in the context of this PR, or do we want to follow-up in later PRs?

@helderco
Copy link
Contributor

helderco commented May 1, 2024

It's more important to be in the same release than in the same PR. I've already made the change for Python locally but in holiday today and the tests need improvement. I asked Tom to do the same but it may not be as easy there because of how it's written. Hold on at least for Python and I'll check up with Tom.

core/integration/module_test.go Show resolved Hide resolved
core/integration/module_test.go Outdated Show resolved Hide resolved
@jedevc
Copy link
Member Author

jedevc commented May 3, 2024

Rebased, since main is a little less flaky right now 🎉

@TomChv
Copy link
Member

TomChv commented May 3, 2024

@jedevc @helderco

I pushed the support for TypeScript on your PR directly: a0a9d61

Both Scalar(a0a9d61) and Enums(bf11e82) are tested

@helderco
Copy link
Contributor

helderco commented May 3, 2024

@jedevc, needs a change log. SDKs not needed, I think. Just core.

@jedevc
Copy link
Member Author

jedevc commented May 3, 2024

@helderco will do - looks like TestModulePythonScalarKind and TestModulePythonEnumKind are failing in CI though.

@helderco
Copy link
Contributor

helderco commented May 3, 2024

@helderco will do - looks like TestModulePythonScalarKind and TestModulePythonEnumKind are failing in CI though.

Ah, forgot about those. Will fix in a minute 🙏

jedevc and others added 15 commits May 6, 2024 12:30
Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
@helderco helderco merged commit 84afe02 into dagger:main May 6, 2024
64 checks passed
@helderco
Copy link
Contributor

helderco commented May 6, 2024

@jedevc, user created enums are actually very useful to support (instead of custom scalars):

@TomChv
Copy link
Member

TomChv commented May 6, 2024

user created enums are actually very useful to support (instead of custom scalars):

This implies some changes in the TS SDK to support enumeration but it shouldn't be a big deal.

@jedevc
Copy link
Member Author

jedevc commented May 7, 2024

Yeah this is actually a bit more of a pain in go since native enum types aren't really a thing. I'm happy for someone else to lead on the custom enums work and I can work out how it makes sense in go, but probably makes sense to lead with a language that actually supports them nicely 😆

See main...jedevc:dagger:add-custom-scalar-kind for a base for how to handle the custom scalar plumbing (enums are pretty similar to enums for this).

@helderco
Copy link
Contributor

helderco commented May 7, 2024

I think Go's implementation of enums is fine. Custom scalars are more problematic because their implementation has to define:

  • How the scalar's value is represented in the module's language (aka, back-end representation)
  • How the value's back-end representation is serialized to a JSON-compatible type
  • How the JSON-compatible representation is deserialized to the back-end representation

That can be simplified if we just assume/limit to a normal string everywhere, but it still opens a can of worms when we don't have enough feedback that it's actually needed.

However with enums, it's just a fixed list of supported string values. No serialization/deserialization required. To support defining new enums via the API it’s similar to ObjectTypeDef, where the fields are just a list of values and their descriptions:

type EnumTypeDef {
  name: String!
  description: String!
  values: [EnumValueTypeDef!]!
}

type EnumValueTypeDef {
  name: String!
  description: String!
}

type TypeDef {
  """
  If kind is ENUM_KIND, the enum-specific type definition.
  If kind is not ENUM_KIND, this will be null.
  """
  asEnum: EnumTypeDef
    
  """
  Returns a TypeDef of kind Enum with the provided name.
  
  Note that an enum's values may be omitted if the intent is only to refer to an enum.
  This is how functions are able to return their own, or any other circular reference.
  """
  withEnum(name: String!, description: String = ""): TypeDef!

  """
  Adds a static value for an Enum TypeDef, failing if the type is not an enum.
  """
  withValue(name: String!, description: String = ""): TypeDef!
}

enum TypeDefKind {
  """
  A GraphQL enum type and its values.
  
  Always paired with an EnumTypeDef.
  """
  ENUM_KIND
}

vikram-dagger pushed a commit to vikram-dagger/dagger that referenced this pull request May 8, 2024
* chore: remove unneccessary InputType

Signed-off-by: Justin Chadwell <[email protected]>

* chore: format mod typedef query

Signed-off-by: Justin Chadwell <[email protected]>

* chore: avoid ModuleObjectType typed nil

Signed-off-by: Justin Chadwell <[email protected]>

* feat: add scalar typedefs to propagate scalars

Signed-off-by: Justin Chadwell <[email protected]>

* feat: interpret current arg to get current platform

Signed-off-by: Justin Chadwell <[email protected]>

* Add Python fix

Signed-off-by: Helder Correia <[email protected]>

* Replace ignored stdout with sync

Signed-off-by: Helder Correia <[email protected]>

* fix test to-platform

Signed-off-by: Justin Chadwell <[email protected]>

* Test return values and enum types

Signed-off-by: Helder Correia <[email protected]>

* Fix linter on changed function

Signed-off-by: Helder Correia <[email protected]>

* chore: regen rust

Signed-off-by: Justin Chadwell <[email protected]>

* feat: support scalar in TypeScript

Signed-off-by: Tom Chauveau <[email protected]>

* feat: add enum tests

Signed-off-by: Tom Chauveau <[email protected]>

* Remove old tests

Signed-off-by: Helder Correia <[email protected]>

* Add change log

Signed-off-by: Helder Correia <[email protected]>

---------

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Helder Correia <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Co-authored-by: Helder Correia <[email protected]>
Co-authored-by: Tom Chauveau <[email protected]>
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

Successfully merging this pull request may close these issues.

🐞 Platform type is converted to string by the code generator
4 participants