Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_<type> Fix conditionals. #247

Merged
merged 12 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,40 @@ _Also aliased as [`is_contained_in`](#is_contained_in)._

_Alias for [`in`](#in)._

#### `is_array`

Executes the functions if/unless the field value is an array.

#### `is_empty`

Executes the functions if/unless the field value is empty.

#### `is_false`

Executes the functions if/unless the field value equals `false` or `0`.

#### `is_hash`

_Alias for [`is_object`](#is_object)._

#### `is_number`

Executes the functions if/unless the field value is a number.

#### `is_object`

Executes the functions if/unless the field value is a hash (object).

_Also aliased as [`is_hash`](#is_hash)._

#### `is_string`

Executes the functions if/unless the field value is a string (and not a number).

#### `is_true`

Executes the functions if/unless the field value equals `true` or `1`.

#### `match`

##### `all_match`
Expand Down
49 changes: 49 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/FixConditional.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,55 @@ public boolean test(final Metafix metafix, final Record record, final List<Strin
}
},

is_array {
@Override
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
return testConditional(record, params, Value::isArray);
}
},
is_empty {
@Override
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
return testConditional(record, params, IS_EMPTY);
}
},
is_false {
@Override
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
return testStringConditional(record, params, IS_FALSE); // TODO: strict=false
}
},
is_hash {
@Override
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
return is_object.test(metafix, record, params, options);
}
},
is_number {
@Override
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
return testStringConditional(record, params, IS_NUMBER);
}
},
is_object {
@Override
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
return testConditional(record, params, Value::isHash);
}
},
is_string {
@Override
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
return testConditional(record, params, Value::isString) && !is_number.test(metafix, record, params, options);
}
},
is_true {
@Override
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
return testStringConditional(record, params, IS_TRUE); // TODO: strict=false
}
},

all_match {
@Override
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
Expand Down
2 changes: 1 addition & 1 deletion metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public void add(final Value value) {
}
}

private boolean isEmpty() {
public boolean isEmpty() {
return list.isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.metafacture.metafix.Record;
import org.metafacture.metafix.Value;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
Expand All @@ -36,6 +37,26 @@ public interface FixPredicate {
BiPredicate<String, String> EQUALS = String::equals;
BiPredicate<String, String> MATCHES = String::matches;

Predicate<String> IS_TRUE = s -> "true".equals(s) || "1".equals(s);
Predicate<String> IS_FALSE = s -> "false".equals(s) || "0".equals(s);

Predicate<String> IS_NUMBER = s -> {
try {
new BigDecimal(s);
return true;
}
catch (final NumberFormatException e) {
return false;
}
};

Predicate<Value> IS_EMPTY = v -> v.extractType((m, c) -> m
.ifArray(a -> c.accept(a.isEmpty()))
.ifHash(h -> c.accept(h.isEmpty()))
// TODO: Catmandu considers whitespace-only strings empty (`$v !~ /\S/`)
.ifString(s -> c.accept(s.isEmpty()))
);

boolean test(Metafix metafix, Record record, List<String> params, Map<String, String> options);

default boolean testConditional(final Record record, final List<String> params, final BiPredicate<Stream<Value>, Predicate<Value>> qualifier, final BiPredicate<String, String> conditional) {
Expand All @@ -49,8 +70,22 @@ default boolean testConditional(final Record record, final List<String> params,
));
}

default boolean testConditional(final Record record, final List<String> params, final Predicate<Value> conditional) {
final String field = params.get(0);

final Value value = record.get(field);
return value != null && conditional.test(value);
}

default boolean testConditional(final List<String> params, final BiPredicate<String, String> conditional) {
return conditional.test(params.get(0), params.get(1));
}

default boolean testStringConditional(final Record record, final List<String> params, final Predicate<String> conditional) {
return testConditional(record, params, v -> v.extractType((m, c) -> m
.ifString(s -> c.accept(conditional.test(s)))
.orElse(w -> c.accept(false))
));
}

}
Loading