All files / json-type/src/jtd converter.ts

63.93% Statements 39/61
62.85% Branches 22/35
100% Functions 1/1
70.37% Lines 38/54

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 1271x     1x                           1x 11x   11x   1x 1x     1x 1x     2x 2x 2x 2x     1x   1x 1x 1x                             2x 2x         3x     1x 1x 1x               1x 1x   1x 1x 1x   1x 2x 2x   2x 2x   2x 1x   1x             1x       1x                                                
import {type ArrType, KeyOptType, type ObjType, type RefType, type Type} from '../type';
import type * as jtd from './types';
 
const NUMS_TYPE_MAPPING = new Map<string, jtd.JtdType>([
  ['u8', 'uint8'],
  ['u16', 'uint16'],
  ['u32', 'uint32'],
  ['i8', 'int8'],
  ['i16', 'int16'],
  ['i32', 'int32'],
  ['f32', 'float32'],
]);
 
/**
 * Main router function that converts any Schema to JTD form.
 * Uses a switch statement to route to the appropriate converter logic.
 */
export function toJtdForm(type: Type): jtd.JtdForm {
  const typeName = type.kind();
 
  switch (typeName) {
    case 'any': {
      const form: jtd.JtdEmptyForm = {nullable: true};
      return form;
    }
    case 'bool': {
      const form: jtd.JtdTypeForm = {type: 'boolean'};
      return form;
    }
    case 'con': {
      const constSchema = type.getSchema();
      const value = constSchema.value;
      const valueType = typeof value;
      switch (valueType) {
        case 'boolean':
        case 'string':
          return {type: valueType};
        case 'number': {
          Iif (value !== Math.round(value)) return {type: 'float64'};
          if (value >= 0) {
            if (value <= 255) return {type: 'uint8'};
            Iif (value <= 65535) return {type: 'uint16'};
            Iif (value <= 4294967295) return {type: 'uint32'};
          } else E{
            Iif (value >= -128) return {type: 'int8'};
            Iif (value >= -32768) return {type: 'int16'};
            Iif (value >= -2147483648) return {type: 'int32'};
          }
          return {type: 'float64'};
        }
      }
      const form: jtd.JtdEmptyForm = {nullable: false};
      return form;
    }
    case 'num': {
      const numSchema = type.getSchema();
      return {
        type: (NUMS_TYPE_MAPPING.get(numSchema.format || '') ?? 'float64') as jtd.JtdType,
      };
    }
    case 'str': {
      return {type: 'string'};
    }
    case 'arr': {
      const arr = type as ArrType;
      if (arr._type) {
        return {
          elements: toJtdForm(arr._type),
        };
      } else E{
        return {nullable: true};
      }
    }
    case 'obj': {
      const obj = type as ObjType;
      const form: jtd.JtdPropertiesForm = {};
 
      if (obj.keys && obj.keys.length > 0) {
        form.properties = {};
        form.optionalProperties = {};
 
        for (const field of obj.keys) {
          const fieldName = field.key;
          const fieldType = field.val;
 
          if (fieldType) {
            const fieldJtd = toJtdForm(fieldType);
            // Check if field is optional
            if (field instanceof KeyOptType) {
              form.optionalProperties[fieldName] = fieldJtd;
            } else {
              form.properties[fieldName] = fieldJtd;
            }
          }
        }
      }
 
      // Handle additional properties - check the schema for unknownFields
      Iif (obj.schema.decodeUnknownKeys === false) {
        form.additionalProperties = false;
      }
 
      return form;
    }
    case 'map': {
      const mapSchema = type.getSchema();
      return {
        values: toJtdForm(mapSchema.value),
      };
    }
    case 'ref': {
      const ref = type as RefType;
      return {
        ref: ref.ref(),
      };
    }
    // case 'or':
    // case 'bin':
    // case 'fn':
    // case 'fn$':
    default: {
      const form: jtd.JtdEmptyForm = {nullable: false};
      return form;
    }
  }
}