Newer
Older
ServerGenerator / src / input-parser.ts
import { andThen, compose, parseArray, parseBoolean, parseNotNull, ParseObject, parseObject, parseRequiredMember, parseString, parseStringValue, resolve, Result } from "./ExampleOutput/util";
import { Collection, Description, Entity, Property } from "./types";

export function parseDescription(data: unknown): Result<Description> {
    const obj = andThen(parseObject)(parseNotNull(data));
    const collectionObj = andThen(parseRequiredMember<Description>("collections"))(obj);
    const collectionResult = andThen(parseArray(parseCollection))(collectionObj);
    if (collectionResult.isSuccessful) {
        return {
            isSuccessful: true,
            value: { collections: collectionResult.value },
        };
    }
    else {
        return collectionResult;
    }
}

function parseCollection(data: unknown): Result<Collection> {
    const obj = compose(parseNotNull, parseObject)(data);
    const result: ParseObject<Collection> = {
        name: andThen(compose(parseRequiredMember<Collection>("name"), parseString))(obj),
        entities: andThen(compose(parseRequiredMember<Collection>("entities"), parseEntity))(obj),
    }
    return resolve(result);
}

function parseEntity(data: unknown): Result<Entity> {
    const obj = compose(parseNotNull, parseObject)(data);
    const result: ParseObject<Entity> = {
        name: andThen(compose(parseRequiredMember<Entity>("name"), parseString))(obj),
        properties: andThen(compose(parseRequiredMember<Entity>("properties"), parseArray(parseProperty)))(obj),
    };
    return resolve(result);
}

function parseProperty(data: unknown): Result<Property> {
    const obj = compose(parseNotNull, parseObject)(data);
    const result: ParseObject<Property> = {
        key: andThen(compose(parseRequiredMember<Property>("key"), parseString))(obj),
        isNullable: andThen(compose(parseRequiredMember<Property>("isNullable"), parseBoolean))(obj),
        type: andThen(compose(compose(parseRequiredMember<Property>("type"), parseString), parseStringValue("string")))(obj),
    };
    return resolve(result);
}