Skip to content

Commit

Permalink
fix: Struct wrap/unwrap snake case stephenh#579
Browse files Browse the repository at this point in the history
  • Loading branch information
TJT committed Jun 3, 2022
1 parent 8abae49 commit 90593b6
Showing 1 changed file with 55 additions and 38 deletions.
93 changes: 55 additions & 38 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,16 @@ export function generateFile(ctx: Context, fileDesc: FileDescriptorProto): [stri
staticMembers.push(generateFromPartial(ctx, fullName, message));
}

staticMembers.push(...generateWrap(ctx, fullTypeName));
staticMembers.push(...generateUnwrap(ctx, fullTypeName));
const structFieldNames = {
nullValue: maybeSnakeToCamel('null_value', ctx.options),
numberValue: maybeSnakeToCamel('number_value', ctx.options),
stringValue: maybeSnakeToCamel('string_value', ctx.options),
boolValue: maybeSnakeToCamel('bool_value', ctx.options),
structValue: maybeSnakeToCamel('struct_value', ctx.options),
listValue: maybeSnakeToCamel('list_value', ctx.options),
}
staticMembers.push(...generateWrap(ctx, fullTypeName, structFieldNames));
staticMembers.push(...generateUnwrap(ctx, fullTypeName, structFieldNames));

chunks.push(code`
export const ${def(fullName)} = {
Expand Down Expand Up @@ -1591,7 +1599,16 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri
return joinCode(chunks, { on: '\n' });
}

function generateWrap(ctx: Context, fullProtoTypeName: string): Code[] {
type StructFieldNames = {
nullValue: string,
numberValue: string,
stringValue: string,
boolValue: string,
structValue: string,
listValue: string,
}

function generateWrap(ctx: Context, fullProtoTypeName: string, fieldNames: StructFieldNames): Code[] {
const chunks: Code[] = [];
if (isStructTypeName(fullProtoTypeName)) {
chunks.push(code`wrap(object: {[key: string]: any} | undefined): Struct {
Expand All @@ -1611,17 +1628,17 @@ function generateWrap(ctx: Context, fullProtoTypeName: string): Code[] {
const result = createBaseValue();
if (value === null) {
result.kind = {$case: 'nullValue', nullValue: NullValue.NULL_VALUE};
result.kind = {$case: '${fieldNames.nullValue}', ${fieldNames.nullValue}: NullValue.NULL_VALUE};
} else if (typeof value === 'boolean') {
result.kind = {$case: 'boolValue', boolValue: value};
result.kind = {$case: '${fieldNames.boolValue}', ${fieldNames.boolValue}: value};
} else if (typeof value === 'number') {
result.kind = {$case: 'numberValue', numberValue: value};
result.kind = {$case: '${fieldNames.numberValue}', ${fieldNames.numberValue}: value};
} else if (typeof value === 'string') {
result.kind = {$case: 'stringValue', stringValue: value};
result.kind = {$case: '${fieldNames.stringValue}', ${fieldNames.stringValue}: value};
} else if (Array.isArray(value)) {
result.kind = {$case: 'listValue', listValue: value};
result.kind = {$case: '${fieldNames.listValue}', ${fieldNames.listValue}: value};
} else if (typeof value === 'object') {
result.kind = {$case: 'structValue', structValue: value};
result.kind = {$case: '${fieldNames.structValue}', ${fieldNames.structValue}: value};
} else if (typeof value !== 'undefined') {
throw new Error('Unsupported any value type: ' + typeof value);
}
Expand All @@ -1633,17 +1650,17 @@ function generateWrap(ctx: Context, fullProtoTypeName: string): Code[] {
const result = createBaseValue();
if (value === null) {
result.nullValue = NullValue.NULL_VALUE;
result.${fieldNames.nullValue} = NullValue.NULL_VALUE;
} else if (typeof value === 'boolean') {
result.boolValue = value;
result.${fieldNames.boolValue} = value;
} else if (typeof value === 'number') {
result.numberValue = value;
result.${fieldNames.numberValue} = value;
} else if (typeof value === 'string') {
result.stringValue = value;
result.${fieldNames.stringValue} = value;
} else if (Array.isArray(value)) {
result.listValue = value;
result.${fieldNames.listValue} = value;
} else if (typeof value === 'object') {
result.structValue = value;
result.${fieldNames.structValue} = value;
} else if (typeof value !== 'undefined') {
throw new Error('Unsupported any value type: ' + typeof value);
}
Expand Down Expand Up @@ -1672,7 +1689,7 @@ function generateWrap(ctx: Context, fullProtoTypeName: string): Code[] {
return chunks;
}

function generateUnwrap(ctx: Context, fullProtoTypeName: string): Code[] {
function generateUnwrap(ctx: Context, fullProtoTypeName: string, fieldNames: StructFieldNames): Code[] {
const chunks: Code[] = [];
if (isStructTypeName(fullProtoTypeName)) {
chunks.push(code`unwrap(message: Struct): {[key: string]: any} {
Expand All @@ -1687,35 +1704,35 @@ function generateUnwrap(ctx: Context, fullProtoTypeName: string): Code[] {
if (isAnyValueTypeName(fullProtoTypeName)) {
if (ctx.options.oneof === OneofOption.UNIONS) {
chunks.push(code`unwrap(message: Value): string | number | boolean | Object | null | Array<any> | undefined {
if (message.kind?.$case === 'nullValue') {
if (message.kind?.$case === '${fieldNames.nullValue}') {
return null;
} else if (message.kind?.$case === 'numberValue') {
return message.kind?.numberValue;
} else if (message.kind?.$case === 'stringValue') {
return message.kind?.stringValue;
} else if (message.kind?.$case === 'boolValue') {
return message.kind?.boolValue;
} else if (message.kind?.$case === 'structValue') {
return message.kind?.structValue;
} else if (message.kind?.$case === 'listValue') {
return message.kind?.listValue;
} else if (message.kind?.$case === '${fieldNames.numberValue}') {
return message.kind?.${fieldNames.numberValue};
} else if (message.kind?.$case === '${fieldNames.stringValue}') {
return message.kind?.${fieldNames.stringValue};
} else if (message.kind?.$case === '${fieldNames.boolValue}') {
return message.kind?.${fieldNames.boolValue};
} else if (message.kind?.$case === '${fieldNames.structValue}') {
return message.kind?.${fieldNames.structValue};
} else if (message.kind?.$case === '${fieldNames.listValue}') {
return message.kind?.${fieldNames.listValue};
} else {
return undefined;
}
}`);
} else {
chunks.push(code`unwrap(message: Value): string | number | boolean | Object | null | Array<any> | undefined {
if (message?.stringValue !== undefined) {
return message.stringValue;
} else if (message?.numberValue !== undefined) {
return message.numberValue;
} else if (message?.boolValue !== undefined) {
return message.boolValue;
} else if (message?.structValue !== undefined) {
return message.structValue;
} else if (message?.listValue !== undefined) {
return message.listValue;
} else if (message?.nullValue !== undefined) {
if (message?.${fieldNames.stringValue} !== undefined) {
return message.${fieldNames.stringValue};
} else if (message?.${fieldNames.numberValue} !== undefined) {
return message.${fieldNames.numberValue};
} else if (message?.${fieldNames.boolValue} !== undefined) {
return message.${fieldNames.boolValue};
} else if (message?.${fieldNames.structValue} !== undefined) {
return message.${fieldNames.structValue};
} else if (message?.${fieldNames.listValue} !== undefined) {
return message.${fieldNames.listValue};
} else if (message?.${fieldNames.nullValue} !== undefined) {
return null;
}
return undefined;
Expand Down

0 comments on commit 90593b6

Please sign in to comment.