Skip to content

Commit

Permalink
Merge pull request #118 from polkadot-cloud/rb-get-scraper-class-by-key
Browse files Browse the repository at this point in the history
feat: Get scraper class by key
  • Loading branch information
rossbulat authored Jun 19, 2024
2 parents 1f2ae2f + 8d7bda9 commit e21a644
Show file tree
Hide file tree
Showing 23 changed files with 246 additions and 148 deletions.
31 changes: 22 additions & 9 deletions src/model/Scraper/CallSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
import type { AnyJson } from '@w3ux/types';
import type { PalletItemScraped } from './types';
import { verifyOption } from './Utils';
import type { MetadataScraper } from '.';

export class FormatCallSignature {
// The scraper associated with this formatter.
scraper: MetadataScraper;

// The raw input config to format.
#rawConfig: PalletItemScraped;

// Type labels to ignore when formatting call signatures.
#ignoreLabels = ['BoundedVec', 'WeakBoundedVec'];

constructor(rawConfig: PalletItemScraped) {
constructor(rawConfig: PalletItemScraped, scraper: MetadataScraper) {
this.#rawConfig = rawConfig;
this.scraper = scraper;
}

// ------------------------------------------------------
Expand Down Expand Up @@ -79,11 +84,17 @@ export class FormatCallSignature {
// A recursive function that formats a call signature by formatting its arguments and return
// types.
getTypeString = (arg: AnyJson) => {
const type = arg?.class?.type;

let str = '';

switch (type) {
// Defensive. If class is not indexed, return empty string.
if (!arg?.indexKey) {
return '';
}

const { indexKey } = arg;
const typeClass = this.scraper.getClass(indexKey);

switch (typeClass.type) {
case 'array':
str = this.getTypeString(arg.array.type);
break;
Expand All @@ -97,11 +108,11 @@ export class FormatCallSignature {
break;

case 'primitive':
str = arg.class.label();
str = typeClass.label();
break;

case 'bitSequence':
str = arg.class.label();
str = typeClass.label();
break;

case 'sequence':
Expand All @@ -125,9 +136,10 @@ export class FormatCallSignature {

// Formats a string from a composite type.
getCompositeString = (arg: AnyJson) => {
let str = '';
const label = arg.class.label();
const typeClass = this.scraper.getClass(arg.indexKey);
const label = typeClass.label();

let str = '';
// Expand type if short label is not defined, or if they've been defined in ignore list.
if (['', ...this.#ignoreLabels].includes(label)) {
str += arg.composite.reduce(
Expand All @@ -153,7 +165,8 @@ export class FormatCallSignature {

// Formats a string from a variant type.
getVariantType = (arg: AnyJson) => {
const label = arg.class.label();
const typeClass = this.scraper.getClass(arg.indexKey);
const label = typeClass.label();

let str = `${label}`;

Expand Down
25 changes: 19 additions & 6 deletions src/model/Scraper/Pallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export class PalletScraper extends MetadataScraper {
// NOTE: Check if storage items are defined for this pallet as there may be none defined.
const items = pallet.storage?.items;
return items
? items.map((item) => this.startStorageScrape(item, options))
? items.map((item, i) =>
this.startStorageScrape(item, { ...options, indexPrefix: String(i) })
)
: [];
}

Expand Down Expand Up @@ -130,9 +132,21 @@ export class PalletScraper extends MetadataScraper {
} else {
const { key, value } = (type as PalleStorageMap).map;

// Ensure arg types and return types have unique index prefixes.
const currentIndexPrefix = options?.indexPrefix || null;
const argIndexPrefix = currentIndexPrefix
? `${currentIndexPrefix}_0`
: '0';
const returnIndexPrefix = currentIndexPrefix
? `${currentIndexPrefix}_1`
: '1';

scrapedType = {
argTypes: this.start(key, options),
returnType: this.start(value, options),
argTypes: this.start(key, { ...options, indexPrefix: argIndexPrefix }),
returnType: this.start(value, {
...options,
indexPrefix: returnIndexPrefix,
}),
};
}

Expand Down Expand Up @@ -281,12 +295,11 @@ export class PalletScraper extends MetadataScraper {
const items = pallet.constants;

if (items) {
result = items.map((item) => {
result = items.map((item, i) => {
const { name, docs, type, value } = item;

const scrapedType = {
argTypes: undefined,
returnType: this.start(type),
returnType: this.start(type, { indexPrefix: String(i) }),
};

return {
Expand Down
8 changes: 4 additions & 4 deletions src/model/Scraper/Types/BitSequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

import type { MetadataScraper } from '..';
import type { TypeParams } from '../types';
import type { BaseParams, BitSequenceType, MetadataType } from './types';
import type { BaseParams, IBitSequenceType, MetadataType } from './types';
import { Base } from './Common/Base';

// Class to hold a bit sequence type.
export class BitSequence extends Base implements MetadataType {
export class BitSequenceType extends Base implements MetadataType {
type = 'bitSequence';

// The type of this bit sequence.
bitSequence: BitSequenceType;
bitSequence: IBitSequenceType;

constructor(bitSequence: BitSequenceType, base: BaseParams) {
constructor(bitSequence: IBitSequenceType, base: BaseParams) {
super(base);
this.bitSequence = bitSequence;
}
Expand Down
6 changes: 3 additions & 3 deletions src/model/Scraper/Types/Compact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

import type { MetadataScraper } from '..';
import type { TypeParams } from '../types';
import type { BaseParams, CompactType, MetadataType } from './types';
import type { BaseParams, ICompactType, MetadataType } from './types';
import { Base } from './Common/Base';

// Class to hold a compact type.
export class Compact extends Base implements MetadataType {
export class CompactType extends Base implements MetadataType {
type = 'compact';

// The inner type of this compact type.
innerType: number;

constructor(compact: CompactType, base: BaseParams) {
constructor(compact: ICompactType, base: BaseParams) {
super(base);
this.innerType = compact.type;
}
Expand Down
10 changes: 5 additions & 5 deletions src/model/Scraper/Types/Composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import type { MetadataScraper } from '..';
import type { TypeParams } from '../types';
import type {
BaseParams,
CompositeField,
CompositeType,
ICompositeField,
ICompositeType,
MetadataType,
} from './types';
import { compositeIsBytes, getCustomInput } from '../Utils';
import { Base } from './Common/Base';

// Class to hold a composite type.
export class Composite extends Base implements MetadataType {
export class CompositeType extends Base implements MetadataType {
type = 'composite';

// The fields of this composite.
fields: CompositeField[];
fields: ICompositeField[];

constructor(composite: CompositeType, base: BaseParams) {
constructor(composite: ICompositeType, base: BaseParams) {
super(base);
this.fields = composite.fields;
}
Expand Down
2 changes: 1 addition & 1 deletion src/model/Scraper/Types/Primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Base } from './Common/Base';
import type { BaseParams, MetadataType } from './types';

// Class to hold a primitive type.
export class Primitive extends Base implements MetadataType {
export class PrimitiveType extends Base implements MetadataType {
type = 'primitive';

// The type of this primitive.
Expand Down
6 changes: 3 additions & 3 deletions src/model/Scraper/Types/Sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
// SPDX-License-Identifier: GPL-3.0-only

import type { MetadataScraper } from '..';
import type { BaseParams, MetadataType, SequenceType } from './types';
import type { BaseParams, MetadataType, ISequenceType } from './types';
import type { TypeParams } from '../types';
import { Base } from './Common/Base';
import { sequenceIsBytes } from '../Utils';

// Class to hold a sequence type.
export class Sequence extends Base implements MetadataType {
export class SequenceType extends Base implements MetadataType {
type = 'sequence';

// The type of this sequence.
innerType: number;

constructor(sequence: SequenceType, base: BaseParams) {
constructor(sequence: ISequenceType, base: BaseParams) {
super(base);
this.innerType = sequence.type;
}
Expand Down
8 changes: 4 additions & 4 deletions src/model/Scraper/Types/Tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
// SPDX-License-Identifier: GPL-3.0-only

import type { MetadataScraper } from '..';
import type { BaseParams, MetadataType, TupleType } from './types';
import type { BaseParams, MetadataType, ITupleType } from './types';
import type { TypeParams } from '../types';
import { Base } from './Common/Base';

// Class to hold a tuple type.
export class Tuple extends Base implements MetadataType {
export class TupleType extends Base implements MetadataType {
type = 'tuple';

// The types of this tuple.
tuple: TupleType;
tuple: ITupleType;

constructor(tuple: TupleType, base: BaseParams) {
constructor(tuple: ITupleType, base: BaseParams) {
super(base);
this.tuple = tuple;
}
Expand Down
16 changes: 8 additions & 8 deletions src/model/Scraper/Types/Variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
// SPDX-License-Identifier: GPL-3.0-only

import type { MetadataScraper } from '..';
import type { BaseParams, MetadataType, VariantItem } from './types';
import type { BaseParams, MetadataType, IVariantItem } from './types';
import type { TypeParams } from '../types';
import { Base } from './Common/Base';

// Class to hold a variant type.
export class Variant extends Base implements MetadataType {
export class VariantType extends Base implements MetadataType {
type = 'variant';

// The variants of this variant type.
items: VariantItem[];
items: IVariantItem[];

constructor(variants: VariantItem[], base: BaseParams) {
constructor(variants: IVariantItem[], base: BaseParams) {
super(base);
this.items = variants;
}
Expand All @@ -25,14 +25,14 @@ export class Variant extends Base implements MetadataType {

// Scrape variant fields. Overwrites `fields` with scraped fields.
scrape(scraper: MetadataScraper, { trailId }: TypeParams) {
return [...this.items].map(({ index, name, fields }) => {
const itemKey = `${this.indexKey}_${index}`;
return [...this.items].map(({ index, name, fields }, i) => {
const itemKey = `${this.indexKey}_${i}`;

return {
index,
name,
fields: fields.map(({ type, name: fieldName, typeName }, i) => {
const indexKey = `${itemKey}_${i}`;
fields: fields.map(({ type, name: fieldName, typeName }, j) => {
const indexKey = `${itemKey}_${j}`;

return {
name: fieldName,
Expand Down
32 changes: 19 additions & 13 deletions src/model/Scraper/Types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import type { LookupItem } from '../Lookup/types';
import type { MetadataScraper } from '..';
import type { TrailId, TypeParams, TrailParentId } from '../types';

/*
NOTES:
- These types are prefixed with `I` as to not get confused with type classes.
- Type classes are suffixed with `Type`, as to not get confused with UI components.
*/

// Primitive
export type PrimitiveType = string;
export type IPrimitiveType = string;

// Array
export interface IArrayType {
Expand All @@ -16,48 +22,48 @@ export interface IArrayType {
}

// Bit Sequence
export interface BitSequenceType {
export interface IBitSequenceType {
bitStoreType: AnyJson;
bitOrderType: AnyJson;
}

// Compact
export interface CompactType {
export interface ICompactType {
type: AnyJson;
}

// Composite
export interface CompositeType {
fields: CompositeField[];
export interface ICompositeType {
fields: ICompositeField[];
}

export interface CompositeField {
export interface ICompositeField {
docs: string[];
name: string;
type: AnyJson;
typeName: string;
}

// Sequence
export interface SequenceType {
export interface ISequenceType {
type: AnyJson;
}

// Tuple
export type TupleType = number[];
export type ITupleType = number[];

// Variant
export interface VariantType {
variants: VariantItem[];
export interface IVariantType {
variants: IVariantItem[];
}
export interface VariantItem {
export interface IVariantItem {
name: string | null;
fields: VariantField[];
fields: IVariantField[];
index: number;
docs: string[];
}

export interface VariantField {
export interface IVariantField {
docs: string[];
name: string;
type: AnyJson;
Expand Down
Loading

0 comments on commit e21a644

Please sign in to comment.