Skip to content

Commit

Permalink
fix: handle default values and multiple packed fields
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-oles committed Oct 21, 2022
1 parent affa739 commit 186e110
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
41 changes: 34 additions & 7 deletions packages/as-proto-gen/src/generate/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ export function generateFieldDecodeInstruction(
if (isRepeated && isPacked) {
return `
case ${fieldNumber}:
const repeatedEnd: usize = reader.ptr + reader.uint32();
while (reader.ptr < repeatedEnd) {
for (const end: usize = reader.ptr + reader.uint32(); reader.ptr < end;) {
message.${fieldName}.push(${Message}.decode(reader, reader.uint32()));
}
break;
Expand All @@ -137,8 +136,7 @@ export function generateFieldDecodeInstruction(
if (isRepeated && isPacked) {
return `
case ${fieldNumber}:
const repeatedEnd: usize = reader.ptr + reader.uint32();
while (reader.ptr < repeatedEnd) {
for (const end: usize = reader.ptr + reader.uint32(); reader.ptr < end;) {
message.${fieldName}.push(reader.${fieldTypeInstruction}());
}
break;
Expand Down Expand Up @@ -225,15 +223,44 @@ export function generateFieldType(
}

export function generateFieldDefaultValue(
fieldDescriptor: FieldDescriptorProto
fieldDescriptor: FieldDescriptorProto,
fileContext: FileContext
): string {
const isRepeated = fieldDescriptor.getLabel() === Label.LABEL_REPEATED;
const defaultValue = fieldDescriptor.getDefaultValue();

if (isRepeated) {
return "[]";
} else if (defaultValue) {
return defaultValue;
switch (fieldDescriptor.getType()) {
case Type.TYPE_INT32:
case Type.TYPE_SINT32:
case Type.TYPE_FIXED32:
case Type.TYPE_SFIXED32:
case Type.TYPE_UINT32:
case Type.TYPE_INT64:
case Type.TYPE_SINT64:
case Type.TYPE_FIXED64:
case Type.TYPE_SFIXED64:
case Type.TYPE_UINT64:
case Type.TYPE_BOOL:
case Type.TYPE_FLOAT:
case Type.TYPE_DOUBLE:
return defaultValue;
case Type.TYPE_ENUM:
return `${generateRef(fieldDescriptor, fileContext)}.${defaultValue}`;
case Type.TYPE_STRING:
return JSON.stringify(defaultValue);
case Type.TYPE_BYTES:
// TODO: handle default value for bytes
return "new Uint8Array(0)";
case Type.TYPE_MESSAGE:
return "null";
default:
throw new Error(
`Type "${fieldDescriptor.getTypeName()}" (${fieldDescriptor.getType()}) is not supported by as-proto-gen`
);
}
} else {
switch (fieldDescriptor.getType()) {
case Type.TYPE_INT32:
Expand Down Expand Up @@ -261,7 +288,7 @@ export function generateFieldDefaultValue(
return "null";
default:
throw new Error(
`Type "${fieldDescriptor.getTypeName()}" is not supported by as-proto-gen`
`Type "${fieldDescriptor.getTypeName()}" (${fieldDescriptor.getType()}) is not supported by as-proto-gen`
);
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/as-proto-gen/src/generate/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function generateMessageConstructor(
)}: ${generateFieldType(
fieldDescriptor,
fileContext
)} = ${generateFieldDefaultValue(fieldDescriptor)}`
)} = ${generateFieldDefaultValue(fieldDescriptor, fileContext)}`
)
.join(",\n");
const fieldsAssignments = fields
Expand Down Expand Up @@ -242,7 +242,10 @@ function canMessageByUnmanaged(
});
}

function generateHelperMethods(message: string, fileContext: FileContext): string {
function generateHelperMethods(
message: string,
fileContext: FileContext
): string {
const Protobuf = fileContext.registerImport("Protobuf", "as-proto");

const encodeHelper = fileContext.registerDefinition(`encode${message}`);
Expand Down

0 comments on commit 186e110

Please sign in to comment.