Skip to content

Instantly share code, notes, and snippets.

@jgke
Created February 24, 2024 12:04
Show Gist options
  • Save jgke/51f250f796e256125f279a915390eebf to your computer and use it in GitHub Desktop.
Save jgke/51f250f796e256125f279a915390eebf to your computer and use it in GitHub Desktop.
Generated code from ts-proto
/* eslint-disable */
import _m0 from "protobufjs/minimal";
export const protobufPackage = "levelformat";
export enum NullValue {
/** NULL_VALUE - Null value. */
NULL_VALUE = 0,
UNRECOGNIZED = -1,
}
export function nullValueFromJSON(object: any): NullValue {
switch (object) {
case 0:
case "NULL_VALUE":
return NullValue.NULL_VALUE;
case -1:
case "UNRECOGNIZED":
default:
return NullValue.UNRECOGNIZED;
}
}
export function nullValueToJSON(object: NullValue): string {
switch (object) {
case NullValue.NULL_VALUE:
return "NULL_VALUE";
case NullValue.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
}
}
/** These are adapted from Google's well-known Struct type */
export interface Struct {
fields: { [key: string]: Value };
}
export interface Struct_FieldsEntry {
key: string;
value: Value | undefined;
}
export interface Value {
nullValue?: NullValue | undefined;
floatValue?: number | undefined;
stringValue?: string | undefined;
boolValue?: boolean | undefined;
structValue?: Struct | undefined;
listValue?: ListValue | undefined;
vec2Value?: ProtoVector2 | undefined;
i32Value?: number | undefined;
}
export interface ProtoVector2 {
x: number;
y: number;
}
export interface ListValue {
values: Value[];
}
function createBaseStruct(): Struct {
return { fields: {} };
}
export const Struct = {
encode(message: Struct, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
Object.entries(message.fields).forEach(([key, value]) => {
Struct_FieldsEntry.encode({ key: key as any, value }, writer.uint32(10).fork()).ldelim();
});
return writer;
},
decode(input: _m0.Reader | Uint8Array, length?: number): Struct {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseStruct();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 10) {
break;
}
const entry1 = Struct_FieldsEntry.decode(reader, reader.uint32());
if (entry1.value !== undefined) {
message.fields[entry1.key] = entry1.value;
}
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},
fromJSON(object: any): Struct {
return {
fields: isObject(object.fields)
? Object.entries(object.fields).reduce<{ [key: string]: Value }>((acc, [key, value]) => {
acc[key] = Value.fromJSON(value);
return acc;
}, {})
: {},
};
},
toJSON(message: Struct): unknown {
const obj: any = {};
if (message.fields) {
const entries = Object.entries(message.fields);
if (entries.length > 0) {
obj.fields = {};
entries.forEach(([k, v]) => {
obj.fields[k] = Value.toJSON(v);
});
}
}
return obj;
},
create<I extends Exact<DeepPartial<Struct>, I>>(base?: I): Struct {
return Struct.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<Struct>, I>>(object: I): Struct {
const message = createBaseStruct();
message.fields = Object.entries(object.fields ?? {}).reduce<{ [key: string]: Value }>((acc, [key, value]) => {
if (value !== undefined) {
acc[key] = Value.fromPartial(value);
}
return acc;
}, {});
return message;
},
};
function createBaseStruct_FieldsEntry(): Struct_FieldsEntry {
return { key: "", value: undefined };
}
export const Struct_FieldsEntry = {
encode(message: Struct_FieldsEntry, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.key !== "") {
writer.uint32(10).string(message.key);
}
if (message.value !== undefined) {
Value.encode(message.value, writer.uint32(18).fork()).ldelim();
}
return writer;
},
decode(input: _m0.Reader | Uint8Array, length?: number): Struct_FieldsEntry {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseStruct_FieldsEntry();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 10) {
break;
}
message.key = reader.string();
continue;
case 2:
if (tag !== 18) {
break;
}
message.value = Value.decode(reader, reader.uint32());
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},
fromJSON(object: any): Struct_FieldsEntry {
return {
key: isSet(object.key) ? String(object.key) : "",
value: isSet(object.value) ? Value.fromJSON(object.value) : undefined,
};
},
toJSON(message: Struct_FieldsEntry): unknown {
const obj: any = {};
if (message.key !== "") {
obj.key = message.key;
}
if (message.value !== undefined) {
obj.value = Value.toJSON(message.value);
}
return obj;
},
create<I extends Exact<DeepPartial<Struct_FieldsEntry>, I>>(base?: I): Struct_FieldsEntry {
return Struct_FieldsEntry.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<Struct_FieldsEntry>, I>>(object: I): Struct_FieldsEntry {
const message = createBaseStruct_FieldsEntry();
message.key = object.key ?? "";
message.value = (object.value !== undefined && object.value !== null) ? Value.fromPartial(object.value) : undefined;
return message;
},
};
function createBaseValue(): Value {
return {
nullValue: undefined,
floatValue: undefined,
stringValue: undefined,
boolValue: undefined,
structValue: undefined,
listValue: undefined,
vec2Value: undefined,
i32Value: undefined,
};
}
export const Value = {
encode(message: Value, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.nullValue !== undefined) {
writer.uint32(8).int32(message.nullValue);
}
if (message.floatValue !== undefined) {
writer.uint32(21).float(message.floatValue);
}
if (message.stringValue !== undefined) {
writer.uint32(26).string(message.stringValue);
}
if (message.boolValue !== undefined) {
writer.uint32(32).bool(message.boolValue);
}
if (message.structValue !== undefined) {
Struct.encode(message.structValue, writer.uint32(42).fork()).ldelim();
}
if (message.listValue !== undefined) {
ListValue.encode(message.listValue, writer.uint32(50).fork()).ldelim();
}
if (message.vec2Value !== undefined) {
ProtoVector2.encode(message.vec2Value, writer.uint32(58).fork()).ldelim();
}
if (message.i32Value !== undefined) {
writer.uint32(64).int32(message.i32Value);
}
return writer;
},
decode(input: _m0.Reader | Uint8Array, length?: number): Value {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseValue();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 8) {
break;
}
message.nullValue = reader.int32() as any;
continue;
case 2:
if (tag !== 21) {
break;
}
message.floatValue = reader.float();
continue;
case 3:
if (tag !== 26) {
break;
}
message.stringValue = reader.string();
continue;
case 4:
if (tag !== 32) {
break;
}
message.boolValue = reader.bool();
continue;
case 5:
if (tag !== 42) {
break;
}
message.structValue = Struct.decode(reader, reader.uint32());
continue;
case 6:
if (tag !== 50) {
break;
}
message.listValue = ListValue.decode(reader, reader.uint32());
continue;
case 7:
if (tag !== 58) {
break;
}
message.vec2Value = ProtoVector2.decode(reader, reader.uint32());
continue;
case 8:
if (tag !== 64) {
break;
}
message.i32Value = reader.int32();
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},
fromJSON(object: any): Value {
return {
nullValue: isSet(object.nullValue) ? nullValueFromJSON(object.nullValue) : undefined,
floatValue: isSet(object.floatValue) ? Number(object.floatValue) : undefined,
stringValue: isSet(object.stringValue) ? String(object.stringValue) : undefined,
boolValue: isSet(object.boolValue) ? Boolean(object.boolValue) : undefined,
structValue: isSet(object.structValue) ? Struct.fromJSON(object.structValue) : undefined,
listValue: isSet(object.listValue) ? ListValue.fromJSON(object.listValue) : undefined,
vec2Value: isSet(object.vec2Value) ? ProtoVector2.fromJSON(object.vec2Value) : undefined,
i32Value: isSet(object.i32Value) ? Number(object.i32Value) : undefined,
};
},
toJSON(message: Value): unknown {
const obj: any = {};
if (message.nullValue !== undefined) {
obj.nullValue = nullValueToJSON(message.nullValue);
}
if (message.floatValue !== undefined) {
obj.floatValue = message.floatValue;
}
if (message.stringValue !== undefined) {
obj.stringValue = message.stringValue;
}
if (message.boolValue !== undefined) {
obj.boolValue = message.boolValue;
}
if (message.structValue !== undefined) {
obj.structValue = Struct.toJSON(message.structValue);
}
if (message.listValue !== undefined) {
obj.listValue = ListValue.toJSON(message.listValue);
}
if (message.vec2Value !== undefined) {
obj.vec2Value = ProtoVector2.toJSON(message.vec2Value);
}
if (message.i32Value !== undefined) {
obj.i32Value = Math.round(message.i32Value);
}
return obj;
},
create<I extends Exact<DeepPartial<Value>, I>>(base?: I): Value {
return Value.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<Value>, I>>(object: I): Value {
const message = createBaseValue();
message.nullValue = object.nullValue ?? undefined;
message.floatValue = object.floatValue ?? undefined;
message.stringValue = object.stringValue ?? undefined;
message.boolValue = object.boolValue ?? undefined;
message.structValue = (object.structValue !== undefined && object.structValue !== null)
? Struct.fromPartial(object.structValue)
: undefined;
message.listValue = (object.listValue !== undefined && object.listValue !== null)
? ListValue.fromPartial(object.listValue)
: undefined;
message.vec2Value = (object.vec2Value !== undefined && object.vec2Value !== null)
? ProtoVector2.fromPartial(object.vec2Value)
: undefined;
message.i32Value = object.i32Value ?? undefined;
return message;
},
};
function createBaseProtoVector2(): ProtoVector2 {
return { x: 0, y: 0 };
}
export const ProtoVector2 = {
encode(message: ProtoVector2, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.x !== 0) {
writer.uint32(21).float(message.x);
}
if (message.y !== 0) {
writer.uint32(29).float(message.y);
}
return writer;
},
decode(input: _m0.Reader | Uint8Array, length?: number): ProtoVector2 {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseProtoVector2();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 2:
if (tag !== 21) {
break;
}
message.x = reader.float();
continue;
case 3:
if (tag !== 29) {
break;
}
message.y = reader.float();
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},
fromJSON(object: any): ProtoVector2 {
return { x: isSet(object.x) ? Number(object.x) : 0, y: isSet(object.y) ? Number(object.y) : 0 };
},
toJSON(message: ProtoVector2): unknown {
const obj: any = {};
if (message.x !== 0) {
obj.x = message.x;
}
if (message.y !== 0) {
obj.y = message.y;
}
return obj;
},
create<I extends Exact<DeepPartial<ProtoVector2>, I>>(base?: I): ProtoVector2 {
return ProtoVector2.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<ProtoVector2>, I>>(object: I): ProtoVector2 {
const message = createBaseProtoVector2();
message.x = object.x ?? 0;
message.y = object.y ?? 0;
return message;
},
};
function createBaseListValue(): ListValue {
return { values: [] };
}
export const ListValue = {
encode(message: ListValue, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
for (const v of message.values) {
Value.encode(v!, writer.uint32(10).fork()).ldelim();
}
return writer;
},
decode(input: _m0.Reader | Uint8Array, length?: number): ListValue {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseListValue();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 10) {
break;
}
message.values.push(Value.decode(reader, reader.uint32()));
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},
fromJSON(object: any): ListValue {
return { values: Array.isArray(object?.values) ? object.values.map((e: any) => Value.fromJSON(e)) : [] };
},
toJSON(message: ListValue): unknown {
const obj: any = {};
if (message.values?.length) {
obj.values = message.values.map((e) => Value.toJSON(e));
}
return obj;
},
create<I extends Exact<DeepPartial<ListValue>, I>>(base?: I): ListValue {
return ListValue.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<ListValue>, I>>(object: I): ListValue {
const message = createBaseListValue();
message.values = object.values?.map((e) => Value.fromPartial(e)) || [];
return message;
},
};
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
export type DeepPartial<T> = T extends Builtin ? T
: T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;
type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
function isObject(value: any): boolean {
return typeof value === "object" && value !== null;
}
function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
@jgke
Copy link
Author

jgke commented Feb 24, 2024

Original protobuf:

syntax = "proto3";
package levelformat;

option csharp_namespace = "GameImpl.Levels.Proto";
/* These are adapted from Google's well-known Struct type */
message Struct {
  map<string, Value> fields = 1;
}

message Value {
	oneof kind {
		NullValue null_value = 1;
		float float_value = 2;
		string string_value = 3;
		bool bool_value = 4;
		Struct struct_value = 5;
		ListValue list_value = 6;
		ProtoVector2 vec2_value = 7;
		int32 i32_value = 8;
	}
}

message ProtoVector2 {
	float x = 2;
	float y = 3;
}

enum NullValue {
  // Null value.
  NULL_VALUE = 0;
}

message ListValue {
  repeated Value values = 1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment