All files / json-type/src/codegen AbstractCodege.ts

96.55% Statements 28/29
92.3% Branches 12/13
100% Functions 2/2
96.55% Lines 28/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                      16x                                 1437x       5862x 5862x   284x 284x   382x 382x   275x 275x   1412x 1412x   1641x 1641x   88x 88x   510x 510x   757x 757x   118x 118x   198x 198x   32x 32x   165x 165x            
import type {Codegen} from '@jsonjoy.com/codegen';
import type {JsExpression} from '@jsonjoy.com/codegen/lib/util/JsExpression';
import type {
  AnyType,
  ArrType,
  BinType,
  BoolType,
  ConType,
  MapType,
  NumType,
  ObjType,
  KeyType,
  OrType,
  RefType,
  StrType,
  Type,
} from '../type';
import type {SchemaPath} from './types';
 
export abstract class AbstractCodegen<Fn extends (...deps: any[]) => any = (...deps: unknown[]) => unknown> {
  public abstract readonly codegen: Codegen<Fn>;
 
  protected abstract onAny(path: SchemaPath, r: JsExpression, type: AnyType): void;
  protected abstract onCon(path: SchemaPath, r: JsExpression, type: ConType): void;
  protected abstract onBool(path: SchemaPath, r: JsExpression, type: BoolType): void;
  protected abstract onNum(path: SchemaPath, r: JsExpression, type: NumType): void;
  protected abstract onStr(path: SchemaPath, r: JsExpression, type: StrType): void;
  protected abstract onBin(path: SchemaPath, r: JsExpression, type: BinType): void;
  protected abstract onArr(path: SchemaPath, r: JsExpression, type: ArrType): void;
  protected abstract onObj(path: SchemaPath, r: JsExpression, type: ObjType): void;
  protected abstract onKey(path: SchemaPath, r: JsExpression, type: KeyType<any, any>): void;
  protected abstract onMap(path: SchemaPath, r: JsExpression, type: MapType): void;
  protected abstract onRef(path: SchemaPath, r: JsExpression, type: RefType): void;
  protected abstract onOr(path: SchemaPath, r: JsExpression, type: OrType): void;
 
  public compile() {
    return this.codegen.compile();
  }
 
  protected onNode(path: SchemaPath, r: JsExpression, type: Type): void {
    const kind = type.kind();
    switch (kind) {
      case 'any':
        this.onAny(path, r, type as AnyType);
        break;
      case 'con':
        this.onCon(path, r, type as ConType);
        break;
      case 'bool':
        this.onBool(path, r, type as BoolType);
        break;
      case 'num':
        this.onNum(path, r, type as NumType);
        break;
      case 'str':
        this.onStr(path, r, type as StrType);
        break;
      case 'bin':
        this.onBin(path, r, type as BinType);
        break;
      case 'arr':
        this.onArr(path, r, type as ArrType);
        break;
      case 'obj':
        this.onObj(path, r, type as ObjType);
        break;
      case 'key':
        this.onKey(path, r, type as KeyType<any, any>);
        break;
      case 'map':
        this.onMap(path, r, type as MapType);
        break;
      case 'ref':
        this.onRef(path, r, type as RefType);
        break;
      case 'or':
        this.onOr(path, r, type as OrType);
        break;
      default:
        throw new Error(`Unsupported kind: ${kind}`);
    }
  }
}