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

[http-server-javascript] Implement scalar encoding #5819

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

witemple-msft
Copy link
Member

This PR implements scalar encoders for HSJ.

JsScalar has been significantly expanded from a simple string type to an object that provides scalar encoding data.

The internal API is

const jsScalar = getJsScalar(ctx, module, scalar, diagnosticTarget);

const encoder = jsScalar.getEncoding("<encoding name>", targetScalar);

const encodedExpr = encoder?.encode("<expr>");
const decodedExpr = encoder?.decode("<expr>");
  • Encodings are described declaratively using a ScalarInfo data structure that allows:

    • Many different encoding pairs [target, encoding] to be specified for a given scalar.
    • Default encodings for a given target (most scalars implement this for at least string)
    • Default encoding pairs by MIME type.
    • Default encoding pairs for HTTP query/header/cookie/path.
    • Encodings that work via another encoding and automatically stack (base64url for bytes is implemented via: "base64").
    • Encodings that are dependent upon imported types or settings in the emitter options (datetime: temporal in the future?)
  • A simple representation of TypeSpec.duration is implemented with ISO 8601-2 parsing.

  • Values of scalars with unrecognized encoding are treated as if they are unknown and serialized literally.

WIP checklist:

  • Document new scalar APIs thoroughly.
  • Implement csv, etc. encoders for query parameters with array types
  • Properly validate url encoding/decoding of query/path params.
  • Implement DateTime types.

@azure-sdk
Copy link
Collaborator

azure-sdk commented Jan 30, 2025

All changed packages have been documented.

  • @typespec/http-server-javascript
Show changes

@typespec/http-server-javascript - feature ✏️

Support encoding/decoding scalar types.

@azure-sdk
Copy link
Collaborator

azure-sdk commented Jan 30, 2025

You can try these changes here

🛝 Playground 🌐 Website 📚 Next docs 🛝 VSCode Extension

const encodingName = encoding.encoding ?? "default";
const scalarEncoder = scalar.getEncoding(encodingName, encoding.type);

// TODO/witemple - we should detect this before realizing models and use a transform to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like having issue references with TODO comments, is there an issue we can correlate with this?

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.

4 participants