PropertyMapper.vala 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using Invercargill.DataStructures;
  2. namespace Invercargill.Mapping {
  3. public delegate TProp PropertyGetter<TClass, TProp>(TClass object) throws Error;
  4. public delegate bool PropertyPredicate<TClass>(TClass object);
  5. public delegate void PropertySetter<TClass, TProp>(TClass object, TProp value) throws Error;
  6. public delegate void PropertyDefaultSetter<TClass>(TClass object);
  7. public delegate TOut PropertyGetterTransformer<TIn, TOut>(TIn object);
  8. public delegate TOut PropertySetterTransformer<TIn, TOut>(TIn object) throws Error;
  9. public delegate T ObjectConstructor<T>();
  10. public class PropertyMapper<T> : Object, Mapper<T, Properties> {
  11. private Vector<PropertyMapping<T>> mappings;
  12. private ObjectConstructor<T>? constructor;
  13. public static PropertyMapper<T> build_for<T>(Func<PropertyMapperBuilder<T>> func) {
  14. int location = (int)func;
  15. return keyed_once<PropertyMapper<T>>(location, () => {
  16. var builder = new PropertyMapperBuilder<T>();
  17. func(builder);
  18. return builder.build();
  19. });
  20. }
  21. internal PropertyMapper(Vector<PropertyMapping<T>> mappings, ObjectConstructor<T>? constructor) {
  22. this.mappings = mappings;
  23. this.constructor = constructor;
  24. }
  25. public void map_into(T object, ReadOnlyAssociative<string, Element> properties) throws Error {
  26. foreach (var mapping in mappings) {
  27. var exists = properties.has(mapping.name);
  28. if(mapping.undefined_setter != null && !exists) {
  29. mapping.undefined_setter(object);
  30. continue;
  31. }
  32. if(mapping.undefined_setter == null && !exists){
  33. throw new IndexError.KEY_NOT_FOUND(@"Failed to map into $(typeof(T).name()): Property \"$(mapping.name)\" does not have an undefined setter and no matching property was found");
  34. }
  35. var element = properties[mapping.name];
  36. if(mapping.null_setter != null && element.is_null()) {
  37. mapping.null_setter(object);
  38. continue;
  39. }
  40. if(mapping.null_setter == null && element.is_null()) {
  41. throw new ElementError.INVALID_CONVERSION(@"Failed to map into $(typeof(T).name()): Property \"$(mapping.name)\" does not have a null setter and a null value was found");
  42. }
  43. mapping.setter(object, properties[mapping.name]);
  44. }
  45. }
  46. public T materialise(Properties properties) throws Error {
  47. var obj = constructor != null ? constructor() : (T) Object.new(typeof(T));
  48. map_into(obj, properties);
  49. return obj;
  50. }
  51. public Properties map_from(T object) throws Error {
  52. var properties = new PropertyDictionary();
  53. foreach (var mapping in mappings) {
  54. // Check undefined
  55. if(mapping.undefined_check != null && mapping.undefined_check(object)){
  56. continue;
  57. }
  58. // Check null
  59. if(mapping.null_check != null && mapping.null_check(object)) {
  60. properties[mapping.name] = new NullElement();
  61. continue;
  62. }
  63. // Assign value
  64. var val = mapping.getter(object);
  65. properties[mapping.name] = val;
  66. }
  67. return properties;
  68. }
  69. }
  70. private class PropertyMapping<T> {
  71. public string name;
  72. public PropertyGetter<T, Element> getter;
  73. public PropertySetter<T, Element> setter;
  74. public PropertyPredicate<T>? null_check;
  75. public PropertyPredicate<T>? undefined_check;
  76. public PropertyDefaultSetter<T>? null_setter;
  77. public PropertyDefaultSetter<T>? undefined_setter;
  78. }
  79. public class PropertyMapperBuilder<T> {
  80. private Vector<PropertyMapping<T>> mappings;
  81. private ObjectConstructor<T> constructor;
  82. public PropertyMapperBuilder() {
  83. mappings = new Vector<PropertyMapping<T>>();
  84. // No default constructor closure: a lambda capturing `this` would
  85. // dangle once the (usually transient) builder is finalized while
  86. // the built PropertyMapper lives on. materialise() falls back to
  87. // the mapper's own T when no constructor was set.
  88. constructor = null;
  89. }
  90. public virtual PropertyMappingBuilder<T> map<TProp>(string name, owned PropertyGetter<T, TProp> getter, owned PropertySetter<T, TProp> setter) {
  91. PropertyMapping<T> mapping;
  92. if(typeof(TProp).is_a(typeof(Element))) {
  93. mapping = new PropertyMapping<T>() {
  94. name = name,
  95. getter = (owned)getter,
  96. setter = (owned)setter,
  97. };
  98. }
  99. else {
  100. mapping = new PropertyMapping<T>() {
  101. name = name,
  102. getter = (o) => new NativeElement<TProp>(getter(o)),
  103. setter = (o,d) => setter(o, d.as<TProp>())
  104. };
  105. }
  106. add_mapping(mapping);
  107. return new PropertyMappingBuilder<T>(this, mapping);
  108. }
  109. public virtual PropertyMappingBuilder<T> map_properties_with<TObj>(string name, owned PropertyGetter<T, TObj> getter, owned PropertySetter<T, TObj> setter, PropertyMapper<TObj> mapper) {
  110. return map_with<TObj, Properties>(name, getter, setter, mapper);
  111. }
  112. public virtual PropertyMappingBuilder<T> map_with<TNative, TElement>(string name, owned PropertyGetter<T, TNative> getter, owned PropertySetter<T, TNative> setter, Mapper<TNative, TElement> mapper) {
  113. var mapping = new PropertyMapping<T>() {
  114. name = name,
  115. getter = (o) => new NativeElement<TElement>(mapper.map_from(getter(o))),
  116. setter = (o,d) => setter(o, mapper.materialise(d.as<TElement>()))
  117. };
  118. add_mapping(mapping);
  119. return new PropertyMappingBuilder<T>(this, mapping);
  120. }
  121. public virtual PropertyMappingBuilder<T> map_many<TObj>(string name, owned PropertyGetter<T, Enumerable<TObj>> getter, owned PropertySetter<T, Enumerable<TObj>> setter) {
  122. var mapping = new PropertyMapping<T>() {
  123. name = name,
  124. getter = (o) => new NativeElement<Elements>(getter(o).to_elements()),
  125. setter = (o,d) => setter(o, d.as<Elements>().elements_as<TObj>())
  126. };
  127. add_mapping(mapping);
  128. return new PropertyMappingBuilder<T>(this, mapping);;
  129. }
  130. public virtual PropertyMappingBuilder<T> map_property_groups_with<TObj>(string name, owned PropertyGetter<T, Enumerable<TObj>> getter, owned PropertySetter<T, Enumerable<TObj>> setter, PropertyMapper<TObj> mapper) {
  131. return map_many_with<TObj, Properties>(name, getter, setter, mapper);
  132. }
  133. public virtual PropertyMappingBuilder<T> map_many_with<TNative, TElement>(string name, owned PropertyGetter<T, Enumerable<TNative>> getter, owned PropertySetter<T, Enumerable<TNative>> setter, Mapper<TNative, TElement> mapper) {
  134. var mapping = new PropertyMapping<T>() {
  135. name = name,
  136. getter = (o) => new NativeElement<Elements>(getter(o).attempt_select<TElement>(i => mapper.map_from(i)).to_elements()),
  137. setter = (o,d) => {
  138. var collection = new Series<TNative>();
  139. foreach (var properties in d.as<Elements>().elements_as<TElement>()) {
  140. collection.add(mapper.materialise(properties));
  141. }
  142. setter(o, collection.seal());
  143. }
  144. };
  145. add_mapping(mapping);
  146. return new PropertyMappingBuilder<T>(this, mapping);
  147. }
  148. public virtual PropertyMapperBuilder<T> set_constructor(ObjectConstructor<T> constructor) {
  149. this.constructor = constructor;
  150. return this;
  151. }
  152. public virtual PropertyMapper<T> build() {
  153. return new PropertyMapper<T>(mappings, constructor);
  154. }
  155. private void add_mapping(PropertyMapping<T> mapping) {
  156. mappings.add(mapping);
  157. }
  158. }
  159. public class PropertyMappingBuilder<T> : PropertyMapperBuilder<T> {
  160. private PropertyMapperBuilder<T> inner;
  161. private PropertyMapping<T> mapping;
  162. internal PropertyMappingBuilder(PropertyMapperBuilder<T> builder, PropertyMapping<T> mapping) {
  163. inner = builder;
  164. this.mapping = mapping;
  165. }
  166. public PropertyMappingBuilder<T> null_when(owned PropertyPredicate<T> predicate) {
  167. mapping.null_check = predicate;
  168. return this;
  169. }
  170. public PropertyMappingBuilder<T> undefined_when(owned PropertyPredicate<T> predicate) {
  171. mapping.undefined_check = predicate;
  172. return this;
  173. }
  174. public PropertyMappingBuilder<T> when_null(owned PropertyDefaultSetter<T> setter) {
  175. mapping.null_setter = setter;
  176. return this;
  177. }
  178. public PropertyMappingBuilder<T> when_undefined(owned PropertyDefaultSetter<T> setter) {
  179. mapping.undefined_setter = setter;
  180. return this;
  181. }
  182. public override PropertyMappingBuilder<T> map<TProp>(string name, owned PropertyGetter<T, TProp> getter, owned PropertySetter<T, TProp> setter) {
  183. return inner.map<TProp>(name, (owned)getter, (owned)setter);
  184. }
  185. public override PropertyMappingBuilder<T> map_properties_with<TObj>(string name, owned PropertyGetter<T, TObj> getter, owned PropertySetter<T, TObj> setter, PropertyMapper<TObj> mapper) {
  186. return inner.map_properties_with<TObj>(name, (owned)getter, (owned)setter, mapper);
  187. }
  188. public override PropertyMappingBuilder<T> map_with<TNative, TElement>(string name, owned PropertyGetter<T, TNative> getter, owned PropertySetter<T, TNative> setter, Mapper<TNative, TElement> mapper) {
  189. return inner.map_with<TNative, TElement>(name, (owned)getter, (owned)setter, mapper);
  190. }
  191. public override PropertyMappingBuilder<T> map_many<TObj>(string name, owned PropertyGetter<T, Enumerable<TObj>> getter, owned PropertySetter<T, Enumerable<TObj>> setter) {
  192. return inner.map_many<TObj>(name, (owned)getter, (owned)setter);
  193. }
  194. public override PropertyMappingBuilder<T> map_property_groups_with<TObj>(string name, owned PropertyGetter<T, Enumerable<TObj>> getter, owned PropertySetter<T, Enumerable<TObj>> setter, PropertyMapper<TObj> mapper) {
  195. return inner.map_property_groups_with<TObj>(name, getter, setter, mapper);
  196. }
  197. public override PropertyMappingBuilder<T> map_many_with<TNative, TElement>(string name, owned PropertyGetter<T, Enumerable<TNative>> getter, owned PropertySetter<T, Enumerable<TNative>> setter, Mapper<TNative, TElement> mapper) {
  198. return inner.map_many_with<TNative, TElement>(name, (owned)getter, (owned)setter, mapper);
  199. }
  200. public override PropertyMapperBuilder<T> set_constructor(ObjectConstructor<T> constructor) {
  201. return inner.set_constructor(constructor);
  202. }
  203. public override PropertyMapper<T> build() {
  204. return inner.build();
  205. }
  206. }
  207. }