Skip to content

Commit

Permalink
feat: add example of using a Timestamp typce
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Stewart <christian@paral.in>
  • Loading branch information
paralin committed Jul 8, 2022
1 parent ee31a1c commit 8b5c813
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 31 deletions.
37 changes: 28 additions & 9 deletions example/other/other.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 59 additions & 7 deletions example/other/other.pb.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
/* eslint-disable */
import { Timestamp } from '@aperturerobotics/ts-proto-common-types/google/protobuf/timestamp.pb.js'
import Long from 'long'
import * as _m0 from 'protobufjs/minimal'

export const protobufPackage = 'other'

export interface OtherMessage {}
export interface OtherMessage {
/** Timestamp is an example of a Date encoding. */
timestamp: Date | undefined
}

function createBaseOtherMessage(): OtherMessage {
return {}
return { timestamp: undefined }
}

export const OtherMessage = {
encode(
_: OtherMessage,
message: OtherMessage,
writer: _m0.Writer = _m0.Writer.create()
): _m0.Writer {
if (message.timestamp !== undefined) {
Timestamp.encode(
toTimestamp(message.timestamp),
writer.uint32(10).fork()
).ldelim()
}
return writer
},

Expand All @@ -25,6 +35,11 @@ export const OtherMessage = {
while (reader.pos < end) {
const tag = reader.uint32()
switch (tag >>> 3) {
case 1:
message.timestamp = fromTimestamp(
Timestamp.decode(reader, reader.uint32())
)
break
default:
reader.skipType(tag & 7)
break
Expand Down Expand Up @@ -69,19 +84,26 @@ export const OtherMessage = {
}
},

fromJSON(_: any): OtherMessage {
return {}
fromJSON(object: any): OtherMessage {
return {
timestamp: isSet(object.timestamp)
? fromJsonTimestamp(object.timestamp)
: undefined,
}
},

toJSON(_: OtherMessage): unknown {
toJSON(message: OtherMessage): unknown {
const obj: any = {}
message.timestamp !== undefined &&
(obj.timestamp = message.timestamp.toISOString())
return obj
},

fromPartial<I extends Exact<DeepPartial<OtherMessage>, I>>(
_: I
object: I
): OtherMessage {
const message = createBaseOtherMessage()
message.timestamp = object.timestamp ?? undefined
return message
},
}
Expand Down Expand Up @@ -119,7 +141,37 @@ export type Exact<P, I extends P> = P extends Builtin
never
>

function toTimestamp(date: Date): Timestamp {
const seconds = numberToLong(date.getTime() / 1_000)
const nanos = (date.getTime() % 1_000) * 1_000_000
return { seconds, nanos }
}

function fromTimestamp(t: Timestamp): Date {
let millis = t.seconds.toNumber() * 1_000
millis += t.nanos / 1_000_000
return new Date(millis)
}

function fromJsonTimestamp(o: any): Date {
if (o instanceof Date) {
return o
} else if (typeof o === 'string') {
return new Date(o)
} else {
return fromTimestamp(Timestamp.fromJSON(o))
}
}

function numberToLong(number: number) {
return Long.fromNumber(number)
}

if (_m0.util.Long !== Long) {
_m0.util.Long = Long as any
_m0.configure()
}

function isSet(value: any): boolean {
return value !== null && value !== undefined
}
7 changes: 6 additions & 1 deletion example/other/other.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
syntax = "proto3";
package other;

message OtherMessage {}
import "google/protobuf/timestamp.proto";

message OtherMessage {
// Timestamp is an example of a Date encoding.
.google.protobuf.Timestamp timestamp = 1;
}
87 changes: 87 additions & 0 deletions example/other/other_vtproto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"typescript": "^4.7.4"
},
"dependencies": {
"@aperturerobotics/ts-proto-common-types": "^0.1.0",
"long": "^5.2.0",
"protobufjs": "^6.11.3"
}
Expand Down
33 changes: 19 additions & 14 deletions patches/ts-poet+4.15.0.patch
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
diff --git a/node_modules/ts-poet/build/Import.js b/node_modules/ts-poet/build/Import.js
index b92bea3..d35c845 100644
index b92bea3..0464ac9 100644
--- a/node_modules/ts-poet/build/Import.js
+++ b/node_modules/ts-poet/build/Import.js
@@ -26,6 +26,7 @@ exports.sameModule = exports.maybeRelativePath = exports.emitImports = exports.S
@@ -26,6 +26,6 @@ exports.sameModule = exports.maybeRelativePath = exports.emitImports = exports.S
const lodash_1 = __importDefault(require("lodash"));
const path = __importStar(require("path"));
const Node_1 = require("./Node");
+const fs = require('fs');
const typeImportMarker = '(?:t:)?';
const fileNamePattern = '(?:[a-zA-Z0-9._-]+)';
const modulePattern = `@?(?:(?:${fileNamePattern}(?:/${fileNamePattern})*))`;
@@ -293,6 +294,16 @@ function emitImports(imports, ourModulePath, importMappings) {
@@ -293,6 +294,18 @@ function emitImports(imports, ourModulePath, importMappings) {
return '';
}
let result = '';
Expand All @@ -19,24 +18,30 @@ index b92bea3..d35c845 100644
+ let ourModuleImportPath = path.normalize(ourModulePath);
+ // HACK: if this is an import from our project, set the path accordingly
+ // github.com/aperturerobotics/protobuf-project/example/example -> ./example/example
+ if (thisProject && ourModuleImportPath.startsWith(thisProject)) {
+ ourModuleImportPath = './' + ourModuleImportPath.substr(thisProject.length + 1);
+ } else {
+ ourModuleImportPath = './vendor/' + ourModuleImportPath;
+ if (thisProject) {
+ if (ourModuleImportPath.startsWith(thisProject)) {
+ ourModuleImportPath = './' + ourModuleImportPath.substr(thisProject.length + 1);
+ } else {
+ ourModuleImportPath = './vendor/' + ourModuleImportPath;
+ }
+ }
const augmentImports = lodash_1.default.groupBy(filterInstances(imports, Augmented), (a) => a.augmented);
// Group the imports by source module they're imported from
const importsByModule = lodash_1.default.groupBy(imports.filter((it) => it.source !== undefined &&
@@ -307,7 +318,14 @@ function emitImports(imports, ourModulePath, importMappings) {
@@ -307,7 +320,18 @@ function emitImports(imports, ourModulePath, importMappings) {
if (modulePath in importMappings) {
modulePath = importMappings[modulePath];
}
- const importPath = maybeRelativePath(ourModulePath, modulePath);
+ if (thisProject && modulePath.startsWith('./')) {
+ if (!modulePath.substr(2).startsWith(thisProject)) {
+ modulePath = './vendor/' + path.normalize(modulePath);
+ } else {
+ modulePath = './' + modulePath.substr(3 + thisProject.length);
+ if (thisProject) {
+ if (modulePath.startsWith('./')) {
+ if (modulePath.substring(2).startsWith(thisProject)) {
+ modulePath = './' + modulePath.substring(3 + thisProject.length);
+ } else if (modulePath.startsWith('./google/protobuf')) {
+ modulePath = '@aperturerobotics/ts-proto-common-types/' + path.normalize(modulePath);
+ } else {
+ modulePath = './vendor/' + path.normalize(modulePath);
+ }
+ }
+ }
+ const importPath = maybeRelativePath(ourModuleImportPath, modulePath);
Expand Down
Loading

0 comments on commit 8b5c813

Please sign in to comment.