Skip to content

Commit

Permalink
namespace methods (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
tatethurston authored Nov 18, 2022
1 parent 1832dcf commit 094a4e7
Show file tree
Hide file tree
Showing 20 changed files with 14,110 additions and 12,643 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@

- Fix intermittent EAGAIN issue encountered when compiling protos

- Use glob imports for generated messages instead of destructuring. This preserves tree shaking, but preserves module namespacing to disambiguate name collisions between protos. Previously, identically named messages in different modules could causes a name collision, eg:

```proto
// foo.proto
message Foo {}
```

```proto
// bar.proto
import "foo.proto";
message Foo {}
```

Would result in errors in the generated code. Now, this is namespaced and works correctly.

## v0.0.65

[Protocol Buffers Well-Known Types](https://developers.google.com/protocol-buffers/docs/reference/google.protobuf) are now exported from `protoscript`. References to well-known types are now imported from `protoscript` rather than being generated. This is a non breaking change. If you have well-known types in your project, you can remove the `google/protobuf` directory that was generated in previous versions alongside your other `.pb.js/ts` files.
Expand Down
24 changes: 12 additions & 12 deletions examples/authentication/src/protos/authentication.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,13 @@ export const CurrentUserJSON = {
* @private
*/
_readMessage: function (msg: CurrentUser, json: any): CurrentUser {
const _username = json["username"];
if (_username) {
msg.username = _username;
const _username_ = json["username"];
if (_username_) {
msg.username = _username_;
}
const _token = json["token"];
if (_token) {
msg.token = _token;
const _token_ = json["token"];
if (_token_) {
msg.token = _token_;
}
return msg;
},
Expand Down Expand Up @@ -343,13 +343,13 @@ export const CredentialsJSON = {
* @private
*/
_readMessage: function (msg: Credentials, json: any): Credentials {
const _username = json["username"];
if (_username) {
msg.username = _username;
const _username_ = json["username"];
if (_username_) {
msg.username = _username_;
}
const _password = json["password"];
if (_password) {
msg.password = _password;
const _password_ = json["password"];
if (_password_) {
msg.password = _password_;
}
return msg;
},
Expand Down
24 changes: 12 additions & 12 deletions examples/authentication/src/protos/haberdasher.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ export const SizeJSON = {
* @private
*/
_readMessage: function (msg: Size, json: any): Size {
const _inches = json["inches"];
if (_inches) {
msg.inches = _inches;
const _inches_ = json["inches"];
if (_inches_) {
msg.inches = _inches_;
}
return msg;
},
Expand Down Expand Up @@ -340,17 +340,17 @@ export const HatJSON = {
* @private
*/
_readMessage: function (msg: Hat, json: any): Hat {
const _inches = json["inches"];
if (_inches) {
msg.inches = _inches;
const _inches_ = json["inches"];
if (_inches_) {
msg.inches = _inches_;
}
const _color = json["color"];
if (_color) {
msg.color = _color;
const _color_ = json["color"];
if (_color_) {
msg.color = _color_;
}
const _name = json["name"];
if (_name) {
msg.name = _name;
const _name_ = json["name"];
if (_name_) {
msg.name = _name_;
}
return msg;
},
Expand Down
24 changes: 12 additions & 12 deletions examples/aws-lambda/src/haberdasher.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ export const SizeJSON = {
* @private
*/
_readMessage: function (msg: Size, json: any): Size {
const _inches = json["inches"];
if (_inches) {
msg.inches = _inches;
const _inches_ = json["inches"];
if (_inches_) {
msg.inches = _inches_;
}
return msg;
},
Expand Down Expand Up @@ -340,17 +340,17 @@ export const HatJSON = {
* @private
*/
_readMessage: function (msg: Hat, json: any): Hat {
const _inches = json["inches"];
if (_inches) {
msg.inches = _inches;
const _inches_ = json["inches"];
if (_inches_) {
msg.inches = _inches_;
}
const _color = json["color"];
if (_color) {
msg.color = _color;
const _color_ = json["color"];
if (_color_) {
msg.color = _color_;
}
const _name = json["name"];
if (_name) {
msg.name = _name;
const _name_ = json["name"];
if (_name_) {
msg.name = _name_;
}
return msg;
},
Expand Down
6 changes: 3 additions & 3 deletions examples/buf/src/A.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ export const FooJSON = {
* @private
*/
_readMessage: function (msg: Foo, json: any): Foo {
const _name = json["name"];
if (_name) {
msg.name = _name;
const _name_ = json["name"];
if (_name_) {
msg.name = _name_;
}
return msg;
},
Expand Down
26 changes: 13 additions & 13 deletions examples/buf/src/B.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import type { ByteSource } from "protoscript";
import { BinaryReader, BinaryWriter } from "protoscript";

import { Foo, FooJSON } from "./A.pb";
import * as A from "./A.pb";

//========================================//
// Types //
//========================================//

export interface Bar {
foo: Foo;
foo: A.Foo;
}

//========================================//
Expand All @@ -39,7 +39,7 @@ export const Bar = {
*/
initialize: function (): Bar {
return {
foo: Foo.initialize(),
foo: A.Foo.initialize(),
};
},

Expand All @@ -51,7 +51,7 @@ export const Bar = {
writer: BinaryWriter
): BinaryWriter {
if (msg.foo) {
writer.writeMessage(1, msg.foo, Foo._writeMessage);
writer.writeMessage(1, msg.foo, A.Foo._writeMessage);
}
return writer;
},
Expand All @@ -64,7 +64,7 @@ export const Bar = {
const field = reader.getFieldNumber();
switch (field) {
case 1: {
reader.readMessage(msg.foo, Foo._readMessage);
reader.readMessage(msg.foo, A.Foo._readMessage);
break;
}
default: {
Expand Down Expand Up @@ -101,7 +101,7 @@ export const BarJSON = {
*/
initialize: function (): Bar {
return {
foo: Foo.initialize(),
foo: A.FooJSON.initialize(),
};
},

Expand All @@ -111,9 +111,9 @@ export const BarJSON = {
_writeMessage: function (msg: Partial<Bar>): Record<string, unknown> {
const json: Record<string, unknown> = {};
if (msg.foo) {
const foo = FooJSON._writeMessage(msg.foo);
if (Object.keys(foo).length > 0) {
json["foo"] = foo;
const _foo_ = A.FooJSON._writeMessage(msg.foo);
if (Object.keys(_foo_).length > 0) {
json["foo"] = _foo_;
}
}
return json;
Expand All @@ -123,10 +123,10 @@ export const BarJSON = {
* @private
*/
_readMessage: function (msg: Bar, json: any): Bar {
const _foo = json["foo"];
if (_foo) {
const m = Foo.initialize();
FooJSON._readMessage(m, _foo);
const _foo_ = json["foo"];
if (_foo_) {
const m = A.Foo.initialize();
A.FooJSON._readMessage(m, _foo_);
msg.foo = m;
}
return msg;
Expand Down
6 changes: 3 additions & 3 deletions examples/config-dest/out/A.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ export const FooJSON = {
* @private
*/
_readMessage: function (msg: Foo, json: any): Foo {
const _name = json["name"];
if (_name) {
msg.name = _name;
const _name_ = json["name"];
if (_name_) {
msg.name = _name_;
}
return msg;
},
Expand Down
26 changes: 13 additions & 13 deletions examples/config-dest/out/B.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import type { ByteSource } from "protoscript";
import { BinaryReader, BinaryWriter } from "protoscript";

import { Foo, FooJSON } from "./A.pb";
import * as A from "./A.pb";

//========================================//
// Types //
//========================================//

export interface Bar {
foo: Foo;
foo: A.Foo;
}

//========================================//
Expand All @@ -39,7 +39,7 @@ export const Bar = {
*/
initialize: function (): Bar {
return {
foo: Foo.initialize(),
foo: A.Foo.initialize(),
};
},

Expand All @@ -51,7 +51,7 @@ export const Bar = {
writer: BinaryWriter
): BinaryWriter {
if (msg.foo) {
writer.writeMessage(1, msg.foo, Foo._writeMessage);
writer.writeMessage(1, msg.foo, A.Foo._writeMessage);
}
return writer;
},
Expand All @@ -64,7 +64,7 @@ export const Bar = {
const field = reader.getFieldNumber();
switch (field) {
case 1: {
reader.readMessage(msg.foo, Foo._readMessage);
reader.readMessage(msg.foo, A.Foo._readMessage);
break;
}
default: {
Expand Down Expand Up @@ -101,7 +101,7 @@ export const BarJSON = {
*/
initialize: function (): Bar {
return {
foo: Foo.initialize(),
foo: A.FooJSON.initialize(),
};
},

Expand All @@ -111,9 +111,9 @@ export const BarJSON = {
_writeMessage: function (msg: Partial<Bar>): Record<string, unknown> {
const json: Record<string, unknown> = {};
if (msg.foo) {
const foo = FooJSON._writeMessage(msg.foo);
if (Object.keys(foo).length > 0) {
json["foo"] = foo;
const _foo_ = A.FooJSON._writeMessage(msg.foo);
if (Object.keys(_foo_).length > 0) {
json["foo"] = _foo_;
}
}
return json;
Expand All @@ -123,10 +123,10 @@ export const BarJSON = {
* @private
*/
_readMessage: function (msg: Bar, json: any): Bar {
const _foo = json["foo"];
if (_foo) {
const m = Foo.initialize();
FooJSON._readMessage(m, _foo);
const _foo_ = json["foo"];
if (_foo_) {
const m = A.Foo.initialize();
A.FooJSON._readMessage(m, _foo_);
msg.foo = m;
}
return msg;
Expand Down
6 changes: 3 additions & 3 deletions examples/config-exclude/src/foo/A.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ export const FooJSON = {
* @private
*/
_readMessage: function (msg: Foo, json: any): Foo {
const _name = json["name"];
if (_name) {
msg.name = _name;
const _name_ = json["name"];
if (_name_) {
msg.name = _name_;
}
return msg;
},
Expand Down
12 changes: 6 additions & 6 deletions examples/config-json/src/A.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ export const FooJSON = {
* @private
*/
_readMessage: function (msg: Foo, json: any): Foo {
const _baz = json["baz"];
if (_baz) {
msg.baz = _baz;
const _baz_ = json["baz"];
if (_baz_) {
msg.baz = _baz_;
}
const _fooBars = json["fooBars"] ?? json["foo_bars"];
if (_fooBars) {
msg.fooBars = _fooBars;
const _fooBars_ = json["fooBars"] ?? json["foo_bars"];
if (_fooBars_) {
msg.fooBars = _fooBars_;
}
return msg;
},
Expand Down
6 changes: 3 additions & 3 deletions examples/config-root/src/A.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ export const FooJSON = {
* @private
*/
_readMessage: function (msg: Foo, json: any): Foo {
const _name = json["name"];
if (_name) {
msg.name = _name;
const _name_ = json["name"];
if (_name_) {
msg.name = _name_;
}
return msg;
},
Expand Down
Loading

0 comments on commit 094a4e7

Please sign in to comment.