UNPKG

@mapeo/schema

Version:

JSON schema and flow types for Mapeo

173 lines (172 loc) 6.25 kB
/* eslint-disable */ import _m0 from "protobufjs/minimal.js"; import { Timestamp } from "../google/protobuf/timestamp.js"; export const protobufPackage = "mapeo"; function createBaseCommon_1() { return { created_at: undefined, deviceId: undefined, id: Buffer.alloc(0), links: [], timestamp: undefined, userId: undefined, }; } export const Common_1 = { encode(message, writer = _m0.Writer.create()) { if (message.created_at !== undefined) { Timestamp.encode(toTimestamp(message.created_at), writer.uint32(10).fork()).ldelim(); } if (message.deviceId !== undefined) { writer.uint32(18).string(message.deviceId); } if (message.id.length !== 0) { writer.uint32(26).bytes(message.id); } for (const v of message.links) { writer.uint32(34).string(v); } if (message.timestamp !== undefined) { Timestamp.encode(toTimestamp(message.timestamp), writer.uint32(42).fork()).ldelim(); } if (message.userId !== undefined) { writer.uint32(50).string(message.userId); } return writer; }, decode(input, length) { const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input); let end = length === undefined ? reader.len : reader.pos + length; const message = createBaseCommon_1(); while (reader.pos < end) { const tag = reader.uint32(); switch (tag >>> 3) { case 1: message.created_at = fromTimestamp(Timestamp.decode(reader, reader.uint32())); break; case 2: message.deviceId = reader.string(); break; case 3: message.id = reader.bytes(); break; case 4: message.links.push(reader.string()); break; case 5: message.timestamp = fromTimestamp(Timestamp.decode(reader, reader.uint32())); break; case 6: message.userId = reader.string(); break; default: reader.skipType(tag & 7); break; } } return message; }, fromJSON(object) { return { created_at: isSet(object.created_at) ? fromJsonTimestamp(object.created_at) : undefined, deviceId: isSet(object.deviceId) ? String(object.deviceId) : undefined, id: isSet(object.id) ? Buffer.from(bytesFromBase64(object.id)) : Buffer.alloc(0), links: Array.isArray(object === null || object === void 0 ? void 0 : object.links) ? object.links.map((e) => String(e)) : [], timestamp: isSet(object.timestamp) ? fromJsonTimestamp(object.timestamp) : undefined, userId: isSet(object.userId) ? String(object.userId) : undefined, }; }, toJSON(message) { const obj = {}; message.created_at !== undefined && (obj.created_at = message.created_at.toISOString()); message.deviceId !== undefined && (obj.deviceId = message.deviceId); message.id !== undefined && (obj.id = base64FromBytes(message.id !== undefined ? message.id : Buffer.alloc(0))); if (message.links) { obj.links = message.links.map((e) => e); } else { obj.links = []; } message.timestamp !== undefined && (obj.timestamp = message.timestamp.toISOString()); message.userId !== undefined && (obj.userId = message.userId); return obj; }, create(base) { return Common_1.fromPartial(base !== null && base !== void 0 ? base : {}); }, fromPartial(object) { var _a, _b, _c, _d, _e, _f; const message = createBaseCommon_1(); message.created_at = (_a = object.created_at) !== null && _a !== void 0 ? _a : undefined; message.deviceId = (_b = object.deviceId) !== null && _b !== void 0 ? _b : undefined; message.id = (_c = object.id) !== null && _c !== void 0 ? _c : Buffer.alloc(0); message.links = ((_d = object.links) === null || _d === void 0 ? void 0 : _d.map((e) => e)) || []; message.timestamp = (_e = object.timestamp) !== null && _e !== void 0 ? _e : undefined; message.userId = (_f = object.userId) !== null && _f !== void 0 ? _f : undefined; return message; }, }; var tsProtoGlobalThis = (() => { if (typeof globalThis !== "undefined") { return globalThis; } if (typeof self !== "undefined") { return self; } if (typeof window !== "undefined") { return window; } if (typeof global !== "undefined") { return global; } throw "Unable to locate global object"; })(); function bytesFromBase64(b64) { if (tsProtoGlobalThis.Buffer) { return Uint8Array.from(tsProtoGlobalThis.Buffer.from(b64, "base64")); } else { const bin = tsProtoGlobalThis.atob(b64); const arr = new Uint8Array(bin.length); for (let i = 0; i < bin.length; ++i) { arr[i] = bin.charCodeAt(i); } return arr; } } function base64FromBytes(arr) { if (tsProtoGlobalThis.Buffer) { return tsProtoGlobalThis.Buffer.from(arr).toString("base64"); } else { const bin = []; arr.forEach((byte) => { bin.push(String.fromCharCode(byte)); }); return tsProtoGlobalThis.btoa(bin.join("")); } } function toTimestamp(date) { const seconds = date.getTime() / 1000; const nanos = (date.getTime() % 1000) * 1000000; return { seconds, nanos }; } function fromTimestamp(t) { let millis = t.seconds * 1000; millis += t.nanos / 1000000; return new Date(millis); } function fromJsonTimestamp(o) { if (o instanceof Date) { return o; } else if (typeof o === "string") { return new Date(o); } else { return fromTimestamp(Timestamp.fromJSON(o)); } } function isSet(value) { return value !== null && value !== undefined; }