Skip to content

Commit

Permalink
new fromJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangdv2429 committed Jun 30, 2023
1 parent fa60f13 commit 29a9320
Showing 1 changed file with 88 additions and 7 deletions.
95 changes: 88 additions & 7 deletions packages/ast/src/encoding/proto/from-json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface FromJSONMethod {
isOptional: boolean;
}

export const fromJSONMethodFields = (context: ProtoParseContext, name: string, proto: ProtoType) => {
export const _fromJSONMethodFields = (context: ProtoParseContext, name: string, proto: ProtoType) => {
const oneOfs = getOneOfs(proto);
const fields = Object.keys(proto.fields ?? {}).map(fieldName => {
const field = {
Expand Down Expand Up @@ -145,14 +145,80 @@ export const fromJSONMethodFields = (context: ProtoParseContext, name: string, p
return fields;
};

export const fromJSONMethodFields = (context: ProtoParseContext, name: string, proto: ProtoType) => {
// const oneOfs = getOneOfs(proto);
let fields: t.IfStatement[];
fields = Object.keys(proto.fields ?? {}).map(fieldName =>
t.ifStatement(
t.callExpression(
t.identifier('isSet'),
[
t.memberExpression(
t.identifier('object'),
t.identifier(fieldName)
)
]
),
t.expressionStatement(
t.assignmentExpression(
'=',
t.memberExpression(
t.identifier('obj'),
t.identifier(fieldName)
),
t.callExpression(
t.identifier('fromJSON'),
[
t.memberExpression(
t.identifier('object'),
t.identifier(fieldName)
)
],

))),
));

return fields;
}

export const fromJSONMethod = (context: ProtoParseContext, name: string, proto: ProtoType) => {
const fields = fromJSONMethodFields(context, name, proto);
let varName = 'object';
if (!fields.length) {
varName = '_';
}

return objectMethod('method',
//scaffold block statement
let statements: t.Statement[] = [
t.variableDeclaration(
'const',
[
t.variableDeclarator(
t.identifier('obj'),
t.callExpression(
t.identifier('createBase' + name),
[]
)
)
]
),
// fields,
// t.returnStatement(
// t.identifier('obj')
// )
]

for (let i = 0; i < fields.length; i++) {
statements.push(fields[i]);
}

statements.push(
t.returnStatement(
t.identifier('obj')
));

return objectMethod(
'method',
t.identifier('fromJSON'),
[
identifier(varName,
Expand All @@ -164,15 +230,30 @@ export const fromJSONMethod = (context: ProtoParseContext, name: string, proto:

],
t.blockStatement(
[
t.returnStatement(
t.objectExpression(fields)
)
]
// [
// t.variableDeclaration(
// 'const',
// [
// t.variableDeclarator(
// t.identifier('obj'),
// t.callExpression(
// t.identifier('createBase' + name),
// []
// )
// )
// ]
// ),
// fields,
// t.returnStatement(
// t.identifier('obj')
// )
// ]
statements
),
false,
false,
false,
// returnType
t.tsTypeAnnotation(
t.tsTypeReference(
t.identifier(name)
Expand Down

0 comments on commit 29a9320

Please sign in to comment.