Skip to content

Commit

Permalink
Transform class references in connect-migrate (#1342)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Stamm <ts@timostamm.de>
Signed-off-by: Steve Ayers <sayers@buf.build>
Co-authored-by: Steve Ayers <sayers@buf.build>
  • Loading branch information
timostamm and smaye81 authored Jan 8, 2025
1 parent 9e3eb4b commit 70a4082
Show file tree
Hide file tree
Showing 7 changed files with 993 additions and 12 deletions.
13 changes: 11 additions & 2 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ Instead of the `new` keyword, you create a message with a function call:
});
```

:white_check_mark: The `connect-migrate` tool will handle this.

Messages are now plain TypeScript types, which greatly improves compatibility
with the ecosystem. For example, messages can be passed from a server-side
component in Next.js to a client-side component without losing any data or types.
Expand All @@ -220,7 +222,10 @@ import { SayRequestSchema } from "./gen/eliza_pb";
```

The same applies to the methods `equals`, `clone`, `toJson`, and `toJsonString`,
and to the static methods `fromBinary`, `fromJson`, `fromJsonString`.
and to the static class methods.

:white_check_mark: The `connect-migrate` tool will handle the static class methods
`fromBinary`, `fromJson`, and `fromJsonString`.

> [!WARNING]
>
Expand Down Expand Up @@ -251,6 +256,8 @@ import { isMessage } from "@bufbuild/protobuf";
}
```

:white_check_mark: The `connect-migrate` tool will handle `isMessage` calls.

#### PlainMessage removed

The `PlainMessage<T>` type was used to represent just the fields of a message,
Expand Down Expand Up @@ -349,11 +356,13 @@ The well-known type `google.protobuf.Struct` is now generated as a more-convenie
All well-known types have been moved to the subpath export `@bufbuild/protobuf/wkt`.
For example, if you want to refer to `google.protobuf.Timestamp`:
```ts
```diff
- import type { Timestamp } from "@bufbuild/protobuf";
+ import type { Timestamp } from "@bufbuild/protobuf/wkt";
```
:white_check_mark: The `connect-migrate` tool will handle this.

Helpers that were previously part of the generated class are now standalone
functions, also exported from `@bufbuild/protobuf/wkt`:

Expand Down
17 changes: 11 additions & 6 deletions packages/connect-migrate/src/lib/migrate-source-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface MigrateSourceFilesResult {

export function migrateSourceFiles(
scanned: Scanned,
transform: j.Transform,
transform: j.Transform | j.Transform[],
print: PrintFn,
logger?: Logger,
updateSourceFileFn: typeof updateSourceFile = updateSourceFile,
Expand Down Expand Up @@ -68,19 +68,24 @@ interface UpdateSourceFileResult {
}

export function updateSourceFile(
transform: Transform,
transform: Transform | Transform[],
path: string,
logger?: Logger,
): UpdateSourceFileResult {
logger?.log(`transform ${path}`);
try {
const source = readFileSync(path, "utf8");
const result = updateSourceFileInMemory(transform, source, path);
if (!result.modified) {
let source = readFileSync(path, "utf8");
let modified = false;
for (const t of Array.isArray(transform) ? transform : [transform]) {
const result = updateSourceFileInMemory(t, source, path);
source = result.source;
modified = modified || result.modified;
}
if (!modified) {
logger?.log(`skipped`);
return { ok: true, modified: false };
}
writeFileSync(path, result.source, "utf-8");
writeFileSync(path, source, "utf-8");
logger?.log(`modified`);
return { ok: true, modified: true };
} catch (e) {
Expand Down
Loading

0 comments on commit 70a4082

Please sign in to comment.