OneOf<T>.byJson constructor

OneOf<T>.byJson(
  1. Map<bool Function(dynamic json, NyResponse response), OneOfBuilder<T>> types, {
  2. OneOfBuilder<T>? otherwise,
})

Picks the shape with your own conditions. Each condition gets the body and the whole response, and the first that returns true wins, in the order written. otherwise builds anything else.

SignUpResult: OneOf<SignUpResult>.byJson({
  (json, response) => response.statusCode == 200 && json['status'] == 'ok': SignUpComplete.fromJson,
  (json, response) => response.statusCode == 200 && json['status'] == 'verify': VerifyEmail.fromJson,
}),

Implementation

factory OneOf.byJson(
  Map<bool Function(dynamic json, NyResponse response), OneOfBuilder<T>>
  types, {
  OneOfBuilder<T>? otherwise,
}) => OneOf((json, response) {
  for (final MapEntry(key: matches, value: build) in types.entries) {
    if (matches(json, response)) return build(json);
  }
  return otherwise?.call(json);
});