Skip to content

Commit

Permalink
edited encode
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Nov 16, 2023
1 parent 5957cbf commit e011744
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export const Grant = {
},
encode(message: Grant, writer: BinaryWriter = BinaryWriter.create()): BinaryWriter {
if (message.authorization !== undefined) {
Any.encode((message.authorization as Any), writer.uint32(10).fork()).ldelim();
Any.encode(GlobalDecoderRegistry.wrapAny(message.authorization), writer.uint32(10).fork()).ldelim();
}
if (message.expiration !== undefined) {
Timestamp.encode(toTimestamp(message.expiration), writer.uint32(18).fork()).ldelim();
Expand Down Expand Up @@ -360,7 +360,7 @@ export const GrantAuthorization = {
writer.uint32(18).string(message.grantee);
}
if (message.authorization !== undefined) {
Any.encode((message.authorization as Any), writer.uint32(26).fork()).ldelim();
Any.encode(GlobalDecoderRegistry.wrapAny(message.authorization), writer.uint32(26).fork()).ldelim();
}
if (message.expiration !== undefined) {
Timestamp.encode(toTimestamp(message.expiration), writer.uint32(34).fork()).ldelim();
Expand Down
3 changes: 3 additions & 0 deletions packages/ast/src/encoding/__tests__/misc.overall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ cases(
context.options.prototypes!.typingsFormat!.timestamp = opts.name;
context.options.interfaces!.enabled = true;
context.options.interfaces!.useGlobalDecoderRegistry = true;
context.options.aminoEncoding = {
enabled: true
}

expectCode(
createObjectWithMethods(
Expand Down
41 changes: 29 additions & 12 deletions packages/ast/src/encoding/proto/encode/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ export const encode = {
isAnyType = true;
}

return types.type(num, prop, name, isAnyType);
const isGlobalRegistry = args.context.options.interfaces?.enabled && args.context.options.interfaces?.useGlobalDecoderRegistry;

return types.type(num, prop, name, isAnyType, isGlobalRegistry);
},

enum(args: EncodeMethod) {
Expand Down Expand Up @@ -337,7 +339,9 @@ export const encode = {
isAnyType = true;
}

return types.typeArray(num, prop, name, isAnyType);
const isGlobalRegistry = args.context.options.interfaces?.enabled && args.context.options.interfaces?.useGlobalDecoderRegistry;

return types.typeArray(num, prop, name, isAnyType, isGlobalRegistry);
},

keyHash(args: EncodeMethod) {
Expand Down Expand Up @@ -507,20 +511,26 @@ export const types = {
)
},

type(num: number, prop: string, name: string, isAnyType: boolean) {
type(num: number, prop: string, name: string, isAnyType: boolean, isGlobalRegistry: boolean) {

let messageProp: t.MemberExpression | t.TSAsExpression = t.memberExpression(
let messageProp: t.MemberExpression | t.TSAsExpression | t.CallExpression = t.memberExpression(
t.identifier('message'),
t.identifier(prop)
);

if (isAnyType) {
if(isGlobalRegistry){
messageProp = t.callExpression(t.memberExpression(t.identifier("GlobalDecoderRegistry"), t.identifier("wrapAny")),[
messageProp
])
} else {
messageProp = t.tsAsExpression(
messageProp,
t.tsTypeReference(
t.identifier('Any')
)
)
}
}

return t.ifStatement(
Expand Down Expand Up @@ -819,18 +829,25 @@ export const types = {
];
},

typeArray(num: number, prop: string, name: string, isAnyType: boolean) {
typeArray(num: number, prop: string, name: string, isAnyType: boolean, isGlobalRegistry: boolean) {
// "v!" just means it's NOT NULLABLE
let nestedProp: t.TSNonNullExpression | t.TSAsExpression = t.tsNonNullExpression(
let nestedProp: t.TSNonNullExpression | t.TSAsExpression | t.CallExpression = t.tsNonNullExpression(
t.identifier('v')
);

if (isAnyType) {
nestedProp = t.tsAsExpression(
nestedProp,
t.tsTypeReference(
t.identifier('Any')
)
)
if(isGlobalRegistry){
nestedProp = t.callExpression(t.memberExpression(t.identifier("GlobalDecoderRegistry"), t.identifier("wrapAny")),[
nestedProp
])
} else {
nestedProp = t.tsAsExpression(
nestedProp,
t.tsTypeReference(
t.identifier('Any')
)
)
}
}


Expand Down
4 changes: 2 additions & 2 deletions packages/ast/types/encoding/proto/encode/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ export declare const types: {
fixed64(num: number, prop: string, isOptional: boolean, args: EncodeMethod): t.IfStatement;
sfixed64(num: number, prop: string, isOptional: boolean, args: EncodeMethod): t.IfStatement;
bool(num: number, prop: string, isOptional: boolean, args?: EncodeMethod): t.IfStatement;
type(num: number, prop: string, name: string, isAnyType: boolean): t.IfStatement;
type(num: number, prop: string, name: string, isAnyType: boolean, isGlobalRegistry: boolean): t.IfStatement;
enum(context: ProtoParseContext, num: number, field: ProtoField, isOptional: boolean, isOneOf: boolean): t.IfStatement;
bytes(num: number, prop: string, isOptional: boolean): t.IfStatement;
timestamp(num: number, prop: string): t.Statement;
timestampDate(num: number, prop: string): t.Statement;
duration(num: number, prop: string): t.IfStatement;
forkDelimArray(num: number, prop: string, expr: t.Statement): (t.ExpressionStatement | t.ForOfStatement)[];
array(num: number, prop: string, expr: t.Statement): t.ForOfStatement[];
typeArray(num: number, prop: string, name: string, isAnyType: boolean): t.ForOfStatement[];
typeArray(num: number, prop: string, name: string, isAnyType: boolean, isGlobalRegistry: boolean): t.ForOfStatement[];
keyHash(num: number, prop: string, name: string): t.ExpressionStatement;
};
export declare const arrayTypes: {
Expand Down

0 comments on commit e011744

Please sign in to comment.