Skip to content

Commit

Permalink
Start using ProtoType and remove CustomType behaviours
Browse files Browse the repository at this point in the history
  • Loading branch information
Eitu33 committed Aug 23, 2023
1 parent fafb1dd commit 7655b11
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 53 deletions.
2 changes: 1 addition & 1 deletion packages/massa-proto-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"type": "module",
"dependencies": {
"@massalabs/as-transformer": "^0.2.2-dev.20230810150029",
"@massalabs/as-transformer": "file:../../../as/packages/as-transformer",
"@massalabs/as-types": "^1.0.1",
"@massalabs/massa-as-sdk": "^2.4.0",
"@massalabs/massa-web3": "^2.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/massa-proto-cli/src/as-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { spawnSync } from 'child_process';
import path from 'path';
import { ProtoFile } from './protobuf.js';
import { default as asProtoTypes } from './asProtoTypes.json' assert { type: 'json' };
import { MassaCustomType } from '@massalabs/as-transformer';
import { ProtoType } from '@massalabs/as-transformer';

/**
* Creates a contract function caller with the given proto file and address.
Expand Down
22 changes: 5 additions & 17 deletions packages/massa-proto-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { generateAsCallers } from './as-gen.js';
import { generateTsCallers } from './ts-gen.js';
import { ProtoFile, getProtoFiles, getProtoFunction } from './protobuf.js';
import { Command } from 'commander';
import { MassaCustomType, extractTypes } from '@massalabs/as-transformer';
import { ProtoType, fetchCustomTypes } from '@massalabs/as-transformer';
import * as dotenv from 'dotenv';
import { existsSync, mkdirSync } from 'fs';
import path from 'path';
Expand Down Expand Up @@ -126,24 +126,12 @@ async function run() {
console.warn(
`For now, we are only using the following custom types because the fetching as issues: u128, u256`,
);
const bignumTypes = `- type:
name: u256
proto: bytes
import: "as-bignum/assembly"
serialize: "\\\\1.toUint8Array()"
deserialize: "u256.fromUint8ArrayLE(\\\\1)"
- type:
name: u128
proto: bytes
import: "as-bignum/assembly"
serialize: "\\\\1.toUint8Array()"
deserialize: "u128.fromUint8ArrayLE(\\\\1)"
`;
// call proto parser with fetched files
const customTypes: MassaCustomType[] = extractTypes(bignumTypes);

// recover any accessible and defined custom protobuf types
const types: Map<string, ProtoType> = fetchCustomTypes();

for (const mpfile of mpFiles) {
let protoFile = await getProtoFunction(mpfile.filePath, customTypes);
let protoFile = await getProtoFunction(mpfile.filePath, types);
files.push(protoFile);
}

Expand Down
50 changes: 16 additions & 34 deletions packages/massa-proto-cli/src/protobuf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
PROTO_FILE_SEPARATOR,
strToBytes,
} from '@massalabs/massa-web3';
import { MassaCustomType } from '@massalabs/as-transformer';
import { ProtoType } from '@massalabs/as-transformer';
import { bytesArrayToString } from './utils/bytesArrayToString.js';
import * as fs from 'fs';
import { MassaProtoFile } from './MassaProtoFile.js';
Expand Down Expand Up @@ -44,8 +44,7 @@ export interface ProtoFile {
*/
export interface FunctionArgument {
name: string;
type: string;
ctype?: MassaCustomType;
type?: ProtoType;
}

/**
Expand All @@ -57,7 +56,6 @@ export interface FunctionArgument {
*/
export async function getProtoFunction(
protoPath: string,
customTypes: MassaCustomType[],
): Promise<ProtoFile> {
// check if the protofile exists contains 'import "google/protobuf/descriptor.proto";'
const descriptorProtoPath = path.join(
Expand Down Expand Up @@ -125,27 +123,19 @@ export async function getProtoFunction(

// get the arguments of the Helper
return Object.entries(helper.fields)
.filter(([, value]) => value)
.map(([name, field]) => {
const fieldType = (field as { type: string; id: number }).type;
const fieldRule =
(field as { rule: string; type: string; id: number }).rule ===
'repeated'
? '[]'
: '';
let ctype: MassaCustomType | undefined = undefined;
if (field.options && field.options['(custom_type)']) {
const customType = field.options['(custom_type)'] as string;
const customTypeObj = customTypes.find(
(ct) => ct.name === customType,
);
assert(customTypeObj);
ctype = customTypeObj;
}
'repeated'
? true
: false;
return {
name: name,
type: (ctype ? ctype.name : fieldType) + (fieldRule ? '[]' : ''),
ctype: ctype,
type: {
name: fieldType,
repeated: fieldRule,
} as ProtoType,
} as FunctionArgument;
});
}
Expand All @@ -158,34 +148,26 @@ export async function getProtoFunction(
const rHelperKeys = Object.keys(rHelper.fields);

if (rHelperKeys.length === 1) {
const options = rHelper.fields['value'].options;
let ctype: MassaCustomType | undefined = undefined;
if (options && options['(custom_type)']) {
const customType = options['(custom_type)'] as string;
const customTypeObj = customTypes.find(
(ct) => ct.name === customType,
);
assert(customTypeObj);
ctype = customTypeObj;
}
const key = rHelperKeys[0];
assert(key);
const field = rHelper.fields[key];
assert(field);

return {
name: 'value',
type:
(ctype !== undefined ? ctype.name : field.type) +
(field.rule ? '[]' : ''),
ctype: ctype,
type: {
name: field.type,
repeated: (field.rule ? true : false)
} as ProtoType,
} as FunctionArgument;
}
}
}
return {
name: 'void',
type: 'void',
type: {
name: 'void',
} as ProtoType,
} as FunctionArgument;
}
}
Expand Down

0 comments on commit 7655b11

Please sign in to comment.