Skip to content

Commit 8c90701

Browse files
authored
Merge pull request #247 from metafacture/addIsTypeConditionals
Add `is_<type>` Fix conditionals.
2 parents 2bad499 + 9e27c9d commit 8c90701

File tree

38 files changed

+1247
-2
lines changed

38 files changed

+1247
-2
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,40 @@ _Also aliased as [`is_contained_in`](#is_contained_in)._
783783

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

786+
#### `is_array`
787+
788+
Executes the functions if/unless the field value is an array.
789+
790+
#### `is_empty`
791+
792+
Executes the functions if/unless the field value is empty.
793+
794+
#### `is_false`
795+
796+
Executes the functions if/unless the field value equals `false` or `0`.
797+
798+
#### `is_hash`
799+
800+
_Alias for [`is_object`](#is_object)._
801+
802+
#### `is_number`
803+
804+
Executes the functions if/unless the field value is a number.
805+
806+
#### `is_object`
807+
808+
Executes the functions if/unless the field value is a hash (object).
809+
810+
_Also aliased as [`is_hash`](#is_hash)._
811+
812+
#### `is_string`
813+
814+
Executes the functions if/unless the field value is a string (and not a number).
815+
816+
#### `is_true`
817+
818+
Executes the functions if/unless the field value equals `true` or `1`.
819+
786820
#### `match`
787821

788822
##### `all_match`

metafix/src/main/java/org/metafacture/metafix/FixConditional.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,55 @@ public boolean test(final Metafix metafix, final Record record, final List<Strin
108108
}
109109
},
110110

111+
is_array {
112+
@Override
113+
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
114+
return testConditional(record, params, Value::isArray);
115+
}
116+
},
117+
is_empty {
118+
@Override
119+
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
120+
return testConditional(record, params, IS_EMPTY);
121+
}
122+
},
123+
is_false {
124+
@Override
125+
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
126+
return testStringConditional(record, params, IS_FALSE); // TODO: strict=false
127+
}
128+
},
129+
is_hash {
130+
@Override
131+
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
132+
return is_object.test(metafix, record, params, options);
133+
}
134+
},
135+
is_number {
136+
@Override
137+
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
138+
return testStringConditional(record, params, IS_NUMBER);
139+
}
140+
},
141+
is_object {
142+
@Override
143+
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
144+
return testConditional(record, params, Value::isHash);
145+
}
146+
},
147+
is_string {
148+
@Override
149+
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
150+
return testConditional(record, params, Value::isString) && !is_number.test(metafix, record, params, options);
151+
}
152+
},
153+
is_true {
154+
@Override
155+
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
156+
return testStringConditional(record, params, IS_TRUE); // TODO: strict=false
157+
}
158+
},
159+
111160
all_match {
112161
@Override
113162
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {

metafix/src/main/java/org/metafacture/metafix/Value.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public void add(final Value value) {
410410
}
411411
}
412412

413-
private boolean isEmpty() {
413+
public boolean isEmpty() {
414414
return list.isEmpty();
415415
}
416416

metafix/src/main/java/org/metafacture/metafix/api/FixPredicate.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.metafacture.metafix.Record;
2121
import org.metafacture.metafix.Value;
2222

23+
import java.math.BigDecimal;
2324
import java.util.List;
2425
import java.util.Map;
2526
import java.util.function.BiPredicate;
@@ -36,6 +37,26 @@ public interface FixPredicate {
3637
BiPredicate<String, String> EQUALS = String::equals;
3738
BiPredicate<String, String> MATCHES = String::matches;
3839

40+
Predicate<String> IS_TRUE = s -> "true".equals(s) || "1".equals(s);
41+
Predicate<String> IS_FALSE = s -> "false".equals(s) || "0".equals(s);
42+
43+
Predicate<String> IS_NUMBER = s -> {
44+
try {
45+
new BigDecimal(s);
46+
return true;
47+
}
48+
catch (final NumberFormatException e) {
49+
return false;
50+
}
51+
};
52+
53+
Predicate<Value> IS_EMPTY = v -> v.extractType((m, c) -> m
54+
.ifArray(a -> c.accept(a.isEmpty()))
55+
.ifHash(h -> c.accept(h.isEmpty()))
56+
// TODO: Catmandu considers whitespace-only strings empty (`$v !~ /\S/`)
57+
.ifString(s -> c.accept(s.isEmpty()))
58+
);
59+
3960
boolean test(Metafix metafix, Record record, List<String> params, Map<String, String> options);
4061

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

73+
default boolean testConditional(final Record record, final List<String> params, final Predicate<Value> conditional) {
74+
final String field = params.get(0);
75+
76+
final Value value = record.get(field);
77+
return value != null && conditional.test(value);
78+
}
79+
5280
default boolean testConditional(final List<String> params, final BiPredicate<String, String> conditional) {
5381
return conditional.test(params.get(0), params.get(1));
5482
}
5583

84+
default boolean testStringConditional(final Record record, final List<String> params, final Predicate<String> conditional) {
85+
return testConditional(record, params, v -> v.extractType((m, c) -> m
86+
.ifString(s -> c.accept(conditional.test(s)))
87+
.orElse(w -> c.accept(false))
88+
));
89+
}
90+
5691
}

0 commit comments

Comments
 (0)