All files / src/shared serialization.ts

32.27% Statements 51/158
0% Branches 0/48
0% Functions 0/47
32.27% Lines 51/158

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 2581x 1x 1x   1x   1x                                                                                                                       1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x           1x                                                                                               1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x     1x           1x     1x          
import {Condition} from './Condition'
import {Modification} from './Modification'
import {compareBy, DataClass, parseObject, ReifiedType} from "@lightningkite/khrysalis-runtime";
import {DataClassProperty, PartialDataClassProperty} from "./DataClassProperty";
import {SortPart} from "./SortPart";
 
(Condition as any).fromJSON = <T>(data: Record<string, any>, types: Array<ReifiedType>): Condition<T> => {
    const type = types[0]
    let key = Object.keys(data)[0]
    switch (key) {
        case "Never":
            return new Condition.Never<T>()
        case "Always":
            return new Condition.Always<T>()
        case "And":
            return new Condition.And(parseObject(data.And, [Array, [Condition, type]]))
        case "Or":
            return new Condition.Or(parseObject(data.Or, [Array, [Condition, type]]))
        case "Not":
            return new Condition.Not(parseObject(data.Not, [Condition, type]))
        case "Equal":
            return new Condition.Equal(parseObject(data.Equal, type))
        case "NotEqual":
            return new Condition.NotEqual(parseObject(data.NotEqual, type))
        case "Inside":
            return new Condition.Inside(parseObject(data.Inside, [Array, type]))
        case "NotInside":
            return new Condition.NotInside(parseObject(data.NotInside, [Array, type]))
        case "GreaterThan":
            return new Condition.GreaterThan(parseObject(data.GreaterThan, type))
        case "LessThan":
            return new Condition.LessThan(parseObject(data.LessThan, type))
        case "GreaterThanOrEqual":
            return new Condition.GreaterThanOrEqual(parseObject(data.GreaterThanOrEqual, type))
        case "LessThanOrEqual":
            return new Condition.LessThanOrEqual(parseObject(data.LessThanOrEqual, type))
        case "Search":
            return parseObject(data.Search, [Condition.Search])
        case "IntBitsClear":
            return new Condition.IntBitsClear(data.IntBitsClear as number) as unknown as Condition<T>
        case "IntBitsSet":
            return new Condition.IntBitsSet(data.IntBitsSet as number) as unknown as Condition<T>
        case "IntBitsAnyClear":
            return new Condition.IntBitsAnyClear(data.IntBitsAnyClear as number) as unknown as Condition<T>
        case "IntBitsAnySet":
            return new Condition.IntBitsAnySet(data.IntBitsAnySet as number) as unknown as Condition<T>
        case "AllElements":
            return new Condition.AllElements(parseObject(data.AllElements, [Condition, type[1]])) as unknown as Condition<T>
        case "AnyElements":
            return new Condition.AnyElements(parseObject(data.AnyElements, [Condition, type[1]])) as unknown as Condition<T>
        case "SizesEquals":
            return new Condition.SizesEquals(data.SizesEquals as number) as unknown as Condition<T>
        case "Exists":
            return new Condition.Exists(data.Exists as string) as unknown as Condition<T>
        case "OnKey":
            return parseObject(data.OnKey, [Condition.OnKey, type[2]])
        case "IfNotNull":
            return new Condition.IfNotNull(parseObject(data.IfNotNull, [Condition, type]))
        default:
            const baseType = type[0]
            const propTypes = baseType.propertyTypes(type.slice(1))
            const innerType = propTypes[key]
            return new Condition.OnField(key as DataClassProperty<T, unknown>, parseObject(data[key], [Condition, innerType]))
    }
}
 
(Condition.Never as any).prototype.toJSON = function (this: Condition.Never<any>): Record<string, any> {
    return {Never: true}
};
(Condition.Always as any).prototype.toJSON = function (this: Condition.Always<any>): Record<string, any> {
    return {Always: true}
};
(Condition.And as any).prototype.toJSON = function (this: Condition.And<any>): Record<string, any> {
    return {And: this.conditions}
};
(Condition.Or as any).prototype.toJSON = function (this: Condition.Or<any>): Record<string, any> {
    return {Or: this.conditions}
};
(Condition.Not as any).prototype.toJSON = function (this: Condition.Not<any>): Record<string, any> {
    return {Not: this.condition}
};
(Condition.Equal as any).prototype.toJSON = function (this: Condition.Equal<any>): Record<string, any> {
    return {Equal: this.value}
};
(Condition.NotEqual as any).prototype.toJSON = function (this: Condition.NotEqual<any>): Record<string, any> {
    return {NotEqual: this.value}
};
(Condition.Inside as any).prototype.toJSON = function (this: Condition.Inside<any>): Record<string, any> {
    return {Inside: this.values}
};
(Condition.NotInside as any).prototype.toJSON = function (this: Condition.NotInside<any>): Record<string, any> {
    return {NotInside: this.values}
};
(Condition.GreaterThan as any).prototype.toJSON = function (this: Condition.GreaterThan<any>): Record<string, any> {
    return {GreaterThan: this.value}
};
(Condition.LessThan as any).prototype.toJSON = function (this: Condition.LessThan<any>): Record<string, any> {
    return {LessThan: this.value}
};
(Condition.GreaterThanOrEqual as any).prototype.toJSON = function (this: Condition.GreaterThanOrEqual<any>): Record<string, any> {
    return {GreaterThanOrEqual: this.value}
};
(Condition.LessThanOrEqual as any).prototype.toJSON = function (this: Condition.LessThanOrEqual<any>): Record<string, any> {
    return {LessThanOrEqual: this.value}
};
(Condition.Search as any).prototype.toJSON = function (this: Condition.Search): Record<string, any> {
    return {Search: {value: this.value, ignoreCase: this.ignoreCase}}
};
(Condition.IntBitsClear as any).prototype.toJSON = function (this: Condition.IntBitsClear): Record<string, any> {
    return {IntBitsClear: this.mask}
};
(Condition.IntBitsSet as any).prototype.toJSON = function (this: Condition.IntBitsSet): Record<string, any> {
    return {IntBitsSet: this.mask}
};
(Condition.IntBitsAnyClear as any).prototype.toJSON = function (this: Condition.IntBitsAnyClear): Record<string, any> {
    return {IntBitsAnyClear: this.mask}
};
(Condition.IntBitsAnySet as any).prototype.toJSON = function (this: Condition.IntBitsAnySet): Record<string, any> {
    return {IntBitsAnySet: this.mask}
};
(Condition.AllElements as any).prototype.toJSON = function (this: Condition.AllElements<any>): Record<string, any> {
    return {AllElements: this.condition}
};
(Condition.AnyElements as any).prototype.toJSON = function (this: Condition.AnyElements<any>): Record<string, any> {
    return {AnyElements: this.condition}
};
(Condition.SizesEquals as any).prototype.toJSON = function (this: Condition.SizesEquals<any>): Record<string, any> {
    return {SizesEquals: this.count}
};
(Condition.Exists as any).prototype.toJSON = function (this: Condition.Exists<any>): Record<string, any> {
    return {Exists: this.key}
};
(Condition.OnKey as any).prototype.toJSON = function (this: Condition.OnKey<any>): Record<string, any> {
    return {OnKey: {key: this.key, condition: this.condition}}
};
(Condition.IfNotNull as any).prototype.toJSON = function (this: Condition.IfNotNull<any>): Record<string, any> {
    return {IfNotNull: this.condition}
};
(Condition.OnField as any).prototype.toJSON = function (this: Condition.OnField<any, any>): Record<string, any> {
    const result: Record<string, any> = {}
    result[this.key] = this.condition
    return result
};
 
(Modification as any).fromJSON = <T>(data: Record<string, any>, types: Array<ReifiedType>): Modification<T> => {
    const type = types[0]
    let key = Object.keys(data)[0]
    switch (key) {
        case "Chain":
            return new Modification.Chain(parseObject(data.Chain, [Array, [Modification, type]]))
        case "IfNotNull":
            return new Modification.IfNotNull(parseObject(data.IfNotNull, [Modification, type])) as unknown as Modification<T>
        case "Assign":
            return new Modification.Assign(parseObject<T>(data.Assign, type))
        case "CoerceAtMost":
            return new Modification.CoerceAtMost(parseObject<T>(data.CoerceAtMost, type))
        case "CoerceAtLeast":
            return new Modification.CoerceAtLeast(parseObject<T>(data.CoerceAtLeast, type))
        case "Increment":
            return new Modification.Increment(data.Increment as number) as unknown as Modification<T>
        case "Multiply":
            return new Modification.Multiply(data.Multiply as number) as unknown as Modification<T>
        case "AppendString":
            return new Modification.AppendString(data.AppendString as string) as unknown as Modification<T>
        case "AppendList":
            return new Modification.AppendList(parseObject<Array<any>>(data.AppendList, [Array, type[1]])) as unknown as Modification<T>
        case "AppendSet":
            return new Modification.AppendSet(parseObject<Array<any>>(data.AppendSet, [Array, type[1]])) as unknown as Modification<T>
        case "Remove":
            return new Modification.Remove(parseObject(data.Remove, [Condition, type[1]])) as unknown as Modification<T>
        case "RemoveInstances":
            return new Modification.RemoveInstances(parseObject<Array<any>>(data.RemoveInstances, [Array, type[1]])) as unknown as Modification<T>
        case "DropFirst":
            return new Modification.DropFirst() as unknown as Modification<T>
        case "DropLast":
            return new Modification.DropLast() as unknown as Modification<T>
        case "PerElement":
            return parseObject(data.PerElement, [Modification.PerElement, type[1]])
        case "Combine":
            return new Modification.Combine(parseObject<Map<string, any>>(data.Combine, [Map, [String], type[2]])) as unknown as Modification<T>
        case "ModifyByKey":
            return new Modification.ModifyByKey(parseObject<Map<string, Modification<any>>>(data.ModifyByKey, [Map, [String], [Modification, type[2]]])) as unknown as Modification<T>
        case "RemoveKeys":
            return new Modification.RemoveKeys(parseObject<Set<string>>(data.RemoveKeys, [Set, [String]])) as unknown as Modification<T>
        default:
            const baseType = type[0]
            const propTypes = baseType.propertyTypes(type.slice(1))
            const innerType = propTypes[key]
            return new Modification.OnField(key as DataClassProperty<T, unknown>, parseObject(data[key], [Modification, innerType]))
    }
}
 
(Modification.Chain as any).prototype.toJSON = function (this: Modification.Chain<any>): Record<string, any> {
    return {Chain: this.modifications}
};
(Modification.IfNotNull as any).prototype.toJSON = function (this: Modification.IfNotNull<any>): Record<string, any> {
    return {IfNotNull: this.modification}
};
(Modification.Assign as any).prototype.toJSON = function (this: Modification.Assign<any>): Record<string, any> {
    return {Assign: this.value}
};
(Modification.CoerceAtMost as any).prototype.toJSON = function (this: Modification.CoerceAtMost<any>): Record<string, any> {
    return {CoerceAtMost: this.value}
};
(Modification.CoerceAtLeast as any).prototype.toJSON = function (this: Modification.CoerceAtLeast<any>): Record<string, any> {
    return {CoerceAtLeast: this.value}
};
(Modification.Increment as any).prototype.toJSON = function (this: Modification.Increment<any>): Record<string, any> {
    return {Increment: this.by}
};
(Modification.Multiply as any).prototype.toJSON = function (this: Modification.Multiply<any>): Record<string, any> {
    return {Multiply: this.by}
};
(Modification.AppendString as any).prototype.toJSON = function (this: Modification.AppendString): Record<string, any> {
    return {AppendString: this.value}
};
(Modification.AppendList as any).prototype.toJSON = function (this: Modification.AppendList<any>): Record<string, any> {
    return {AppendList: this.items}
};
(Modification.AppendSet as any).prototype.toJSON = function (this: Modification.AppendSet<any>): Record<string, any> {
    return {AppendSet: this.items}
};
(Modification.Remove as any).prototype.toJSON = function (this: Modification.Remove<any>): Record<string, any> {
    return {Remove: this.condition}
};
(Modification.RemoveInstances as any).prototype.toJSON = function (this: Modification.RemoveInstances<any>): Record<string, any> {
    return {RemoveInstances: this.items}
};
(Modification.DropFirst as any).prototype.toJSON = function (this: Modification.DropFirst<any>): Record<string, any> {
    return {DropFirst: true}
};
(Modification.DropLast as any).prototype.toJSON = function (this: Modification.DropLast<any>): Record<string, any> {
    return {DropLast: true}
};
(Modification.Combine as any).prototype.toJSON = function (this: Modification.Combine<any>): Record<string, any> {
    return {Combine: this.map}
};
(Modification.ModifyByKey as any).prototype.toJSON = function (this: Modification.ModifyByKey<any>): Record<string, any> {
    return {ModifyByKey: this.map}
};
(Modification.RemoveKeys as any).prototype.toJSON = function (this: Modification.RemoveKeys<any>): Record<string, any> {
    return {RemoveKeys: this.fields}
};
(Modification.OnField as any).prototype.toJSON = function (this: Modification.OnField<any, any>): Record<string, any> {
    const result: Record<string, any> = {}
    result[this.key] = this.modification
    return result
};
 
(SortPart as any).prototype.toJSON = function (this: SortPart<any>): string {
    return this.ascending ? this.field : `-${this.field}`
};
(SortPart as any).fromJSON = <T>(value: string, types: Array<ReifiedType>): SortPart<T> => {
    const descending = value.startsWith('-')
    const realName = descending ? value.substring(1) : value
    return new SortPart<T>(realName as PartialDataClassProperty<T>, !descending)
}