From 7f03bd4aba63de6dfe98227b41269e351b8563a1 Mon Sep 17 00:00:00 2001 From: Tate Thurston Date: Fri, 3 Jan 2025 08:08:29 -0800 Subject: [PATCH] Fix duration serialization --- .github/workflows/ci.yml | 65 +++++++------------ .github/workflows/publish.yml | 2 +- .gitignore | 2 + .husky/pre-commit | 3 - CHANGELOG.md | 4 ++ .../protobuf/test_messages_proto3.pb.ts | 6 +- e2e/serialization/json.pb.ts | 6 +- e2e/treeshaking/treeshaking.pb.ts | 3 +- package.json | 2 +- packages/protoscript/package.json | 2 +- .../src/codegen/autogenerate/index.ts | 2 +- .../src/runtime/well-known-types/any.pb.ts | 3 +- .../src/runtime/well-known-types/api.pb.ts | 2 +- 13 files changed, 40 insertions(+), 62 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d1ce3da..ae7d248 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,63 +5,42 @@ on: - main pull_request: jobs: - lint_test: + build-package: + name: Build Package + runs-on: ubuntu-latest + outputs: + package: ${{ steps.publish-local-package.outputs.package }} + steps: + - uses: tatethurston/github-actions/publish-local-package@main + id: publish-local-package + with: + path: packages/protoscript + ci: name: Lint and Test runs-on: ubuntu-latest steps: - # Install protoc for e2e:setup - uses: arduino/setup-protoc@v2 with: - version: "23.4" - - uses: bufbuild/buf-setup-action@v1.25.0 + version: "29.2" - uses: tatethurston/github-actions/test@main with: codecov_token: ${{ secrets.CODECOV_TOKEN }} - - name: Check for uncommited changes to generated files - run: | - pnpm regen - if git diff --quiet; then - echo "No changes detected." - else - echo "Changes detected:" - git diff --name-only - git diff - echo "Failing build." - exit 1 - fi - build_package: - name: Build Package - runs-on: ubuntu-latest - outputs: - package-name: package-${{ github.event.pull_request.number }} - steps: - - uses: tatethurston/github-actions/build@main - - name: NPM Pack - run: | - cd packages/protoscript - pnpm pack - mv *.tgz protoscript.tgz - - uses: actions/upload-artifact@v4 + - uses: bufbuild/buf-setup-action@v1.25.0 + - uses: tatethurston/github-actions/check-generated-files@main with: - name: package-${{ github.event.pull_request.number }} - path: packages/protoscript/*.tgz - if-no-files-found: error - retention-days: 1 - overwrite: true - check_windows: - name: Windows CI Check + cmd: pnpm regen + ci-windows: + name: Windows CI runs-on: windows-latest - needs: build_package + needs: build-package steps: - - uses: arduino/setup-protoc@v2 + - uses: tatethurston/github-actions/install-local-package@main with: - version: "23.4" - - uses: actions/download-artifact@v4 + name: ${{ needs.build-package.outputs.package }} + - uses: arduino/setup-protoc@v2 with: - name: ${{ needs.build_package.outputs.package-name }} - - uses: actions/setup-node@v3 + version: "29.2" - name: Bin Check run: | - npm install protoscript.tgz touch dummy.proto npx --no protoscript diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 305daeb..2bfe8fb 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -4,7 +4,7 @@ on: types: [published] workflow_dispatch: jobs: - build: + publish: runs-on: ubuntu-latest steps: - uses: tatethurston/github-actions/publish@main diff --git a/.gitignore b/.gitignore index 5a14cde..041e3c9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ coverage node_modules todo.txt packages/well-known-types/**/*.pb.ts +packages/protoscript/LICENSE +packages/protoscript/README.md diff --git a/.husky/pre-commit b/.husky/pre-commit index 97da597..d2d1446 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,7 +1,4 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -(cd packages/protoscript && pnpm run package:build) -pnpm run regen pnpm run lint:fix -pnpm run typecheck diff --git a/CHANGELOG.md b/CHANGELOG.md index b118210..8b5b1fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v0.0.22 + +- Fix Timestamp and Duration JSON serialization. Previously, when either seconds or nanos were 0, the Timestamp / Duration was omitted from the serialized json. Thanks @martynchamberlin! + ## v0.0.21 - Fix compiler path on Windows. diff --git a/e2e/conformance/proto/google/protobuf/test_messages_proto3.pb.ts b/e2e/conformance/proto/google/protobuf/test_messages_proto3.pb.ts index 7357388..92be314 100644 --- a/e2e/conformance/proto/google/protobuf/test_messages_proto3.pb.ts +++ b/e2e/conformance/proto/google/protobuf/test_messages_proto3.pb.ts @@ -4573,8 +4573,7 @@ export const TestAllTypesProto3JSON = { } if ( msg.optionalDuration && - msg.optionalDuration.seconds && - msg.optionalDuration.nanos + (msg.optionalDuration.seconds || msg.optionalDuration.nanos) ) { json["optionalDuration"] = protoscript.serializeDuration( msg.optionalDuration, @@ -4582,8 +4581,7 @@ export const TestAllTypesProto3JSON = { } if ( msg.optionalTimestamp && - msg.optionalTimestamp.seconds && - msg.optionalTimestamp.nanos + (msg.optionalTimestamp.seconds || msg.optionalTimestamp.nanos) ) { json["optionalTimestamp"] = protoscript.serializeTimestamp( msg.optionalTimestamp, diff --git a/e2e/serialization/json.pb.ts b/e2e/serialization/json.pb.ts index 5b1d1da..de5013f 100644 --- a/e2e/serialization/json.pb.ts +++ b/e2e/serialization/json.pb.ts @@ -1042,8 +1042,7 @@ export const SampleMessageJSON = { } if ( msg.sampleTimestamp && - msg.sampleTimestamp.seconds && - msg.sampleTimestamp.nanos + (msg.sampleTimestamp.seconds || msg.sampleTimestamp.nanos) ) { json["sampleTimestamp"] = protoscript.serializeTimestamp( msg.sampleTimestamp, @@ -1051,8 +1050,7 @@ export const SampleMessageJSON = { } if ( msg.sampleDuration && - msg.sampleDuration.seconds && - msg.sampleDuration.nanos + (msg.sampleDuration.seconds || msg.sampleDuration.nanos) ) { json["sampleDuration"] = protoscript.serializeDuration( msg.sampleDuration, diff --git a/e2e/treeshaking/treeshaking.pb.ts b/e2e/treeshaking/treeshaking.pb.ts index a05c192..c4d8390 100644 --- a/e2e/treeshaking/treeshaking.pb.ts +++ b/e2e/treeshaking/treeshaking.pb.ts @@ -360,8 +360,7 @@ export const TreeshakingTestJSON = { } if ( msg.timestampField && - msg.timestampField.seconds && - msg.timestampField.nanos + (msg.timestampField.seconds || msg.timestampField.nanos) ) { json["timestampField"] = protoscript.serializeTimestamp( msg.timestampField, diff --git a/package.json b/package.json index eced704..d758b33 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "lint:fix": "prettier --write . && prettier-package-json --write '{,e2e/*,examples/*,packages/*}package.json' && eslint --fix .", "package:copy:files": "cp ./LICENSE ./README.md packages/protoscript", "prepare": "husky install", - "regen": "pnpm build:wellknowntypes && pnpm examples:regen && pnpm e2e:protoscript", + "regen": "pnpm examples:regen && pnpm e2e:protoscript && pnpm build:wellknowntypes", "test": "jest", "test:ci": "pnpm install --frozen-lockfile && pnpm run e2e:setup && pnpm run test --coverage", "typecheck": "pnpm --recursive run typecheck" diff --git a/packages/protoscript/package.json b/packages/protoscript/package.json index a339dbb..64b09ff 100644 --- a/packages/protoscript/package.json +++ b/packages/protoscript/package.json @@ -1,6 +1,6 @@ { "name": "protoscript", - "version": "0.0.21", + "version": "0.0.22", "description": "A Protobuf runtime and code generation tool for JavaScript and TypeScript", "license": "MIT", "author": "Tate ", diff --git a/packages/protoscript/src/codegen/autogenerate/index.ts b/packages/protoscript/src/codegen/autogenerate/index.ts index 31946ab..3c64f03 100644 --- a/packages/protoscript/src/codegen/autogenerate/index.ts +++ b/packages/protoscript/src/codegen/autogenerate/index.ts @@ -553,7 +553,7 @@ function writeJSONSerializers(types: ProtoTypes[], parents: string[]): string { } else if (field.read === "readEnum") { res += `if (msg.${field.name} && ${field.tsTypeJSON}._toInt(msg.${field.name})) {`; } else if ([DURATION, TIMESTAMP].includes(field.tsType)) { - res += `if (msg.${field.name} && msg.${field.name}.seconds && msg.${field.name}.nanos) {`; + res += `if (msg.${field.name} && (msg.${field.name}.seconds || msg.${field.name}.nanos)) {`; } else { res += `if (msg.${field.name}) {`; } diff --git a/packages/protoscript/src/runtime/well-known-types/any.pb.ts b/packages/protoscript/src/runtime/well-known-types/any.pb.ts index e9abba9..493e548 100644 --- a/packages/protoscript/src/runtime/well-known-types/any.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/any.pb.ts @@ -122,7 +122,8 @@ export interface Any { * * Note: this functionality is not currently available in the official * protobuf release, and it is not used for type URLs beginning with - * type.googleapis.com. + * type.googleapis.com. As of May 2023, there are no widely used type server + * implementations and no plans to implement one. * * Schemes other than `http`, `https` (or the empty scheme) might be * used with implementation specific semantics. diff --git a/packages/protoscript/src/runtime/well-known-types/api.pb.ts b/packages/protoscript/src/runtime/well-known-types/api.pb.ts index d7a20b7..80a799c 100644 --- a/packages/protoscript/src/runtime/well-known-types/api.pb.ts +++ b/packages/protoscript/src/runtime/well-known-types/api.pb.ts @@ -154,7 +154,7 @@ export interface Method { * The mixin construct implies that all methods in `AccessControl` are * also declared with same name and request/response types in * `Storage`. A documentation generator or annotation processor will - * see the effective `Storage.GetAcl` method after inherting + * see the effective `Storage.GetAcl` method after inheriting * documentation and annotations as follows: * * service Storage {