public async static Task <bool> TryMergeAsync <TSource, TTarget>(this JObject jObject, Mapping <TSource[], IList <TTarget> > mapping, Func <TSource, TTarget, Task> asyncMerge) where TTarget : class, new() { var token = jObject[mapping.Field]; if (token == null) { return(false); } var sources = token.Values <TSource>().ToArray(); mapping[sources] = await Task.WhenAll( sources .Select(async s => { var t = new TTarget(); await asyncMerge(s, t); return(t); })); return(true); }
static JTask Optional <TSource, TTarget>(this JTask jTask, Mapping <TSource, TTarget> mapping, Func <TSource, TTarget> conversion) { jTask.TryConvert(mapping, conversion); return(jTask); }
public async static Task <bool> TryMergeAsync <TSource, TTarget>(this JObject jObject, Mapping <TSource, TTarget> mapping, Func <TSource, TTarget, Task> asyncMerge) where TTarget : class { var token = jObject[mapping.Field]; if (token == null) { return(false); } var source = token.Value <TSource>(); var target = mapping[source]; if (target == null) { target = mapping[token.Value <TSource>()] = Activator.CreateInstance <TTarget>(); } await asyncMerge(source, target); return(true); }
//public static JTask Optional<T>(this JTask jTask, Expression<Func<JObject[], IList<T>>> mapping, Action<JObject, T> merge) // where T : class, new() => // jTask.Optional<JObject, T>(mapping, merge); static JTask Optional <TSource, TTarget>(this JTask jTask, Mapping <TSource[], IList <TTarget> > mapping, Action <TSource, TTarget> merge) where TTarget : class, new() { jTask.TryMerge(mapping, merge); return(jTask); }
public static async Task <bool> TryConvertAsync <TSource, TTarget>(this JObject jObject, Mapping <TSource[], IList <TTarget> > mapping, Func <TSource, Task <TTarget> > asyncConversion) { var token = jObject[mapping.Field]; if (token == null) { return(false); } mapping[token.Values <TSource>().ToArray()] = await Task.WhenAll( token.Values <TSource>() .Select(v => asyncConversion(v))); return(true); }
public static async Task <bool> TryConvertAsync <TSource, TTarget>(this JObject jObject, Mapping <TSource, TTarget> mapping, Func <TSource, Task <TTarget> > asyncConversion) { var token = jObject[mapping.Field]; if (token == null) { return(false); } mapping[token.Value <TSource>()] = await asyncConversion(token.Value <TSource>()); return(true); }
static JTask Optional <TSource, TTarget>(this JTask jTask, Mapping <TSource[], IList <TTarget> > mapping) { jTask.TryCoerce(mapping); return(jTask); }