|
| 1 | +import { Rule, rulesRegistry } from './mapping.highlevel' |
| 2 | +import { rule } from './mapping.rulesfactories' |
| 3 | + |
| 4 | +function booleanProperty (config?: Rule) { |
| 5 | + return (_: any, context: any) => { |
| 6 | + context.addInitializer(function () { |
| 7 | + const constructorName = this.constructor.name |
| 8 | + if (rulesRegistry[constructorName] === undefined) { |
| 9 | + rulesRegistry[constructorName] = {} |
| 10 | + } |
| 11 | + const rules = rulesRegistry[constructorName] |
| 12 | + rules[context.name] = rule.asBoolean(config) |
| 13 | + }) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +function stringProperty (config?: Rule) { |
| 18 | + return (_: any, context: any) => { |
| 19 | + context.addInitializer(function () { |
| 20 | + const constructorName = this.constructor.name |
| 21 | + if (rulesRegistry[constructorName] === undefined) { |
| 22 | + rulesRegistry[constructorName] = {} |
| 23 | + } |
| 24 | + const rules = rulesRegistry[constructorName] |
| 25 | + rules[context.name] = rule.asString(config) |
| 26 | + }) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function numberProperty (config?: Rule & { acceptBigInt?: boolean }) { |
| 31 | + return (_: any, context: any) => { |
| 32 | + context.addInitializer(function () { |
| 33 | + const constructorName = this.constructor.name |
| 34 | + if (rulesRegistry[constructorName] === undefined) { |
| 35 | + rulesRegistry[constructorName] = {} |
| 36 | + } |
| 37 | + const rules = rulesRegistry[constructorName] |
| 38 | + rules[context.name] = rule.asNumber(config) |
| 39 | + }) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function bigIntProperty (config?: Rule & { acceptNumber?: boolean }) { |
| 44 | + return (_: any, context: any) => { |
| 45 | + context.addInitializer(function () { |
| 46 | + const constructorName = this.constructor.name |
| 47 | + if (rulesRegistry[constructorName] === undefined) { |
| 48 | + rulesRegistry[constructorName] = {} |
| 49 | + } |
| 50 | + const rules = rulesRegistry[constructorName] |
| 51 | + rules[context.name] = rule.asBigInt(config) |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function nodeProperty (config?: Rule) { |
| 57 | + return (_: any, context: any) => { |
| 58 | + context.addInitializer(function () { |
| 59 | + const constructorName = this.constructor.name |
| 60 | + if (rulesRegistry[constructorName] === undefined) { |
| 61 | + rulesRegistry[constructorName] = {} |
| 62 | + } |
| 63 | + const rules = rulesRegistry[constructorName] |
| 64 | + rules[context.name] = rule.asNode(config) |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function relationshipProperty (config?: Rule) { |
| 70 | + return (_: any, context: any) => { |
| 71 | + context.addInitializer(function () { |
| 72 | + const constructorName = this.constructor.name |
| 73 | + if (rulesRegistry[constructorName] === undefined) { |
| 74 | + rulesRegistry[constructorName] = {} |
| 75 | + } |
| 76 | + const rules = rulesRegistry[constructorName] |
| 77 | + rules[context.name] = rule.asRelationship(config) |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function pathProperty (config?: Rule) { |
| 83 | + return (_: any, context: any) => { |
| 84 | + context.addInitializer(function () { |
| 85 | + const constructorName = this.constructor.name |
| 86 | + if (rulesRegistry[constructorName] === undefined) { |
| 87 | + rulesRegistry[constructorName] = {} |
| 88 | + } |
| 89 | + const rules = rulesRegistry[constructorName] |
| 90 | + rules[context.name] = rule.asPath(config) |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +function pointProperty (config?: Rule) { |
| 96 | + return (_: any, context: any) => { |
| 97 | + context.addInitializer(function () { |
| 98 | + const constructorName = this.constructor.name |
| 99 | + if (rulesRegistry[constructorName] === undefined) { |
| 100 | + rulesRegistry[constructorName] = {} |
| 101 | + } |
| 102 | + const rules = rulesRegistry[constructorName] |
| 103 | + rules[context.name] = rule.asPoint(config) |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +function durationProperty (config?: Rule & { stringify?: boolean }) { |
| 109 | + return (_: any, context: any) => { |
| 110 | + context.addInitializer(function () { |
| 111 | + const constructorName = this.constructor.name |
| 112 | + if (rulesRegistry[constructorName] === undefined) { |
| 113 | + rulesRegistry[constructorName] = {} |
| 114 | + } |
| 115 | + const rules = rulesRegistry[constructorName] |
| 116 | + rules[context.name] = rule.asDuration(config) |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function listProperty (config?: Rule & { apply?: Rule }) { |
| 122 | + return (_: any, context: any) => { |
| 123 | + context.addInitializer(function () { |
| 124 | + const constructorName = this.constructor.name |
| 125 | + if (rulesRegistry[constructorName] === undefined) { |
| 126 | + rulesRegistry[constructorName] = {} |
| 127 | + } |
| 128 | + const rules = rulesRegistry[constructorName] |
| 129 | + rules[context.name] = rule.asList({ apply: { ...rules[context.name] }, ...config }) |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +function optionalProperty () { |
| 135 | + return (_: any, context: any) => { |
| 136 | + context.addInitializer(function () { |
| 137 | + const constructorName = this.constructor.name |
| 138 | + if (rulesRegistry[constructorName] === undefined) { |
| 139 | + rulesRegistry[constructorName] = {} |
| 140 | + } |
| 141 | + const rules = rulesRegistry[constructorName] |
| 142 | + rules[context.name] = { optional: true, ...rules[context.name] } |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +function mapPropertyFromName (name: string) { |
| 148 | + return (_: any, context: any) => { |
| 149 | + context.addInitializer(function () { |
| 150 | + const constructorName = this.constructor.name |
| 151 | + if (rulesRegistry[constructorName] === undefined) { |
| 152 | + rulesRegistry[constructorName] = {} |
| 153 | + } |
| 154 | + const rules = rulesRegistry[constructorName] |
| 155 | + rules[context.name] = { from: name, ...rules[context.name] } |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function convertPropertyToType (type: any) { |
| 161 | + return (_: any, context: any) => { |
| 162 | + context.addInitializer(function () { |
| 163 | + const constructorName = this.constructor.name |
| 164 | + if (rulesRegistry[constructorName] === undefined) { |
| 165 | + rulesRegistry[constructorName] = {} |
| 166 | + } |
| 167 | + const rules = rulesRegistry[constructorName] |
| 168 | + rules[context.name] = { convert: (node) => node.as(type), ...rules[context.name] } |
| 169 | + }) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +const forExport = { |
| 174 | + booleanProperty, |
| 175 | + stringProperty, |
| 176 | + numberProperty, |
| 177 | + bigIntProperty, |
| 178 | + nodeProperty, |
| 179 | + relationshipProperty, |
| 180 | + pathProperty, |
| 181 | + pointProperty, |
| 182 | + durationProperty, |
| 183 | + listProperty, |
| 184 | + optionalProperty, |
| 185 | + mapPropertyFromName, |
| 186 | + convertPropertyToType |
| 187 | +} |
| 188 | + |
| 189 | +export default forExport |
0 commit comments