Skip to content

Commit 2a91c92

Browse files
authored
feat: io.cloudquery.scalar.Bool (#27)
Closes #26
1 parent cc41103 commit 2a91c92

File tree

4 files changed

+223
-11
lines changed

4 files changed

+223
-11
lines changed

Diff for: lib/src/main/java/io/cloudquery/scalar/Bool.java

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.cloudquery.scalar;
2+
3+
import org.apache.arrow.vector.types.pojo.ArrowType;
4+
5+
public class Bool implements Scalar {
6+
protected boolean value;
7+
protected boolean valid;
8+
9+
public Bool() {
10+
}
11+
12+
public Bool(Object value) throws ValidationException {
13+
this.set(value);
14+
}
15+
16+
@Override
17+
public String toString() {
18+
if (this.valid) {
19+
return Boolean.toString(this.value);
20+
}
21+
return NULL_VALUE_STRING;
22+
}
23+
24+
@Override
25+
public boolean isValid() {
26+
return this.valid;
27+
}
28+
29+
@Override
30+
public ArrowType dataType() {
31+
return ArrowType.Bool.INSTANCE;
32+
}
33+
34+
@Override
35+
public void set(Object value) throws ValidationException {
36+
if (value == null) {
37+
this.valid = false;
38+
this.value = false;
39+
return;
40+
}
41+
42+
if (value instanceof Scalar scalar) {
43+
if (!scalar.isValid()) {
44+
this.valid = false;
45+
this.value = false;
46+
return;
47+
}
48+
49+
this.set(scalar.get());
50+
return;
51+
}
52+
53+
if (value instanceof Boolean b) {
54+
this.valid = true;
55+
this.value = b;
56+
return;
57+
}
58+
59+
if (value instanceof String string) {
60+
this.valid = true;
61+
this.value = Boolean.parseBoolean(string);
62+
return;
63+
}
64+
65+
throw new ValidationException(ValidationException.NO_CONVERSION_AVAILABLE, this.dataType(), value);
66+
}
67+
68+
@Override
69+
public Object get() {
70+
if (this.valid) {
71+
return this.value;
72+
}
73+
return null;
74+
}
75+
76+
@Override
77+
public boolean equals(Object other) {
78+
if (other == null) {
79+
return false;
80+
}
81+
82+
if (!(other instanceof Bool o)) {
83+
return false;
84+
}
85+
86+
return (this.valid == o.valid) && (this.value == o.value);
87+
}
88+
}

Diff for: lib/src/test/java/io/cloudquery/scalar/BinaryTest.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
package io.cloudquery.scalar;
22

3-
import io.cloudquery.scalar.Binary;
4-
import io.cloudquery.scalar.ValidationException;
5-
63
import org.apache.arrow.vector.types.pojo.ArrowType;
7-
import org.junit.Assert;
84
import org.junit.jupiter.api.Test;
95

10-
import java.util.Arrays;
11-
126
import static org.junit.jupiter.api.Assertions.*;
137

148

@@ -139,7 +133,7 @@ public void testEquals() {
139133
Binary b = new Binary();
140134
assertEquals(a, b);
141135
assertNotEquals(a, null);
142-
assertEquals(a, new LargeBinary()); // we can cast LargeBinary to Binary
136+
assertNotEquals(a, new Bool()); // we can't cast Bool to Binary
143137
assertNotEquals(null, a);
144138

145139
assertDoesNotThrow(() -> {

Diff for: lib/src/test/java/io/cloudquery/scalar/BoolTest.java

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package io.cloudquery.scalar;
2+
3+
import org.apache.arrow.vector.types.pojo.ArrowType;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
9+
public class BoolTest {
10+
@Test
11+
public void testNew() {
12+
assertDoesNotThrow(() -> {
13+
new Bool();
14+
});
15+
}
16+
17+
@Test
18+
public void testNewWithValidParam() {
19+
assertDoesNotThrow(() -> {
20+
new Bool(true);
21+
new Bool("true");
22+
23+
Scalar s = new Bool(true);
24+
new Bool(s);
25+
});
26+
}
27+
28+
@Test
29+
public void testNewWithInvalidParam() {
30+
assertThrows(ValidationException.class, () -> {
31+
new Bool(new char[]{'1'});
32+
});
33+
}
34+
35+
@Test
36+
public void testToString() {
37+
Bool b = new Bool();
38+
assertEquals(Scalar.NULL_VALUE_STRING, b.toString());
39+
40+
assertDoesNotThrow(() -> {
41+
b.set(true);
42+
});
43+
assertEquals("true", b.toString());
44+
45+
assertDoesNotThrow(() -> {
46+
b.set(false);
47+
});
48+
assertEquals("false", b.toString());
49+
}
50+
51+
@Test
52+
public void testDataType() {
53+
Bool b = new Bool();
54+
assertEquals(ArrowType.Bool.INSTANCE, b.dataType());
55+
assertEquals(new ArrowType.Bool(), b.dataType());
56+
}
57+
58+
@Test
59+
public void testIsValid() {
60+
Bool b = new Bool();
61+
assertFalse(b.isValid());
62+
63+
assertDoesNotThrow(() -> {
64+
b.set("true");
65+
});
66+
assertTrue(b.isValid());
67+
}
68+
69+
@Test
70+
public void testSet() {
71+
Bool b = new Bool();
72+
assertDoesNotThrow(() -> {
73+
new Bool(true);
74+
new Bool("true");
75+
76+
Scalar s = new Bool(true);
77+
b.set(s);
78+
});
79+
}
80+
81+
@Test
82+
public void testSetWithInvalidParam() {
83+
Bool b = new Bool();
84+
assertThrows(ValidationException.class, () -> {
85+
b.set(new char[]{});
86+
});
87+
}
88+
89+
@Test
90+
public void testGet() {
91+
Bool b = new Bool();
92+
assertFalse(b.isValid());
93+
assertNull(b.get());
94+
95+
assertDoesNotThrow(() -> {
96+
b.set(true);
97+
});
98+
assertTrue(b.isValid());
99+
assertEquals(true, b.get());
100+
101+
assertDoesNotThrow(() -> {
102+
b.set("abc");
103+
});
104+
assertTrue(b.isValid());
105+
assertEquals(false, b.get());
106+
}
107+
108+
@Test
109+
public void testEquals() {
110+
Bool a = new Bool();
111+
Bool b = new Bool();
112+
assertEquals(a, b);
113+
assertNotEquals(a, null);
114+
assertNotEquals(a, new Binary()); // we can't cast Binary to Bool
115+
assertNotEquals(null, a);
116+
117+
assertDoesNotThrow(() -> {
118+
a.set(true);
119+
});
120+
assertNotEquals(a, b);
121+
122+
assertDoesNotThrow(() -> {
123+
for (Object obj : new Object[]{null, true, false, "abc", "true", "false"}) {
124+
a.set(obj);
125+
assertEquals(a, new Bool(obj));
126+
}
127+
});
128+
}
129+
}

Diff for: lib/src/test/java/io/cloudquery/scalar/LargeBinaryTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,23 @@ public void testGet() {
126126
assertTrue(b.isValid());
127127
assertArrayEquals(new byte[]{'a', 'b', 'c'}, (byte[]) b.get());
128128
}
129+
129130
@Test
130131
public void testEquals() {
131132
LargeBinary a = new LargeBinary();
132133
LargeBinary b = new LargeBinary();
133134
assertEquals(a, b);
134-
assertNotEquals(a,null);
135-
assertNotEquals(a,new Binary()); // we can't cast Binary to LargeBinary
135+
assertNotEquals(a, null);
136+
assertNotEquals(a, new Bool()); // we can't cast Bool to LargeBinary
136137
assertNotEquals(null, a);
137138

138139
assertDoesNotThrow(() -> {
139140
a.set(new byte[]{'a', 'b', 'c'});
140141
});
141-
assertNotEquals(a,b);
142+
assertNotEquals(a, b);
142143

143144
assertDoesNotThrow(() -> {
144-
for (Object obj: new Object[]{
145+
for (Object obj : new Object[]{
145146
null,
146147
new byte[]{'a', 'b', 'c'},
147148
new char[]{'a', 'b', 'c'},

0 commit comments

Comments
 (0)