Skip to content

Commit

Permalink
add string
Browse files Browse the repository at this point in the history
  • Loading branch information
candiduslynx committed Aug 18, 2023
1 parent 859a517 commit 1fe0bb9
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/src/main/java/io/cloudquery/scalar/Binary.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public Binary(Object value) throws ValidationException {
}

@Override
public String toString() {
public java.lang.String toString() {
if (this.value != null) {
return Base64.encodeBase64String(this.value);
}
Expand All @@ -40,7 +40,7 @@ public void setValue(Object value) throws ValidationException {
}

if (value instanceof char[] chars) {
this.value = Base64.decodeBase64(new String(chars));
this.value = Base64.decodeBase64(new java.lang.String(chars));
return;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/io/cloudquery/scalar/Scalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Scalar(Object value) throws ValidationException {

public abstract ArrowType dataType();

public String toString() {
public java.lang.String toString() {
if (this.value != null) {
return this.value.toString();
}
Expand Down Expand Up @@ -72,5 +72,5 @@ public final int hashCode() {
return Objects.hash(value);
}

public static final String NULL_VALUE_STRING = "(null)";
public static final java.lang.String NULL_VALUE_STRING = "(null)";
}
24 changes: 24 additions & 0 deletions lib/src/main/java/io/cloudquery/scalar/String.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.cloudquery.scalar;

import org.apache.arrow.vector.types.pojo.ArrowType;

public class String extends Scalar<java.lang.String> {

public String() {
super();
}

public String(Object value) throws ValidationException {
super(value);
}

@Override
public ArrowType dataType() {
return ArrowType.Utf8.INSTANCE;
}

@Override
public void setValue(Object value) throws ValidationException {
this.value = value.toString();
}
}
20 changes: 10 additions & 10 deletions lib/src/main/java/io/cloudquery/scalar/ValidationException.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@

public class ValidationException extends Exception {
public Throwable cause;
public String message;
public java.lang.String message;
public ArrowType type;
private final Object value;

static final String NO_CONVERSION_AVAILABLE = "no conversion available";
static final java.lang.String NO_CONVERSION_AVAILABLE = "no conversion available";


ValidationException(Throwable cause, String message, ArrowType type, Object value) {
ValidationException(Throwable cause, java.lang.String message, ArrowType type, Object value) {
super(message, cause);
this.cause = cause;
this.message = message;
this.type = type;
this.value = value;
}

ValidationException(String message, ArrowType type, Object value) {
ValidationException(java.lang.String message, ArrowType type, Object value) {
super(message);
this.message = message;
this.type = type;
this.value = value;
}

public String Error() {
public java.lang.String Error() {
if (this.cause == null) {
return String.format("cannot set `%s` with value `%s`: %s", this.type, this.value, this.message);
return java.lang.String.format("cannot set `%s` with value `%s`: %s", this.type, this.value, this.message);
}
return String.format("cannot set `%s` with value `%s`: %s (%s)", this.type, this.value, this.message, this.cause);
return java.lang.String.format("cannot set `%s` with value `%s`: %s (%s)", this.type, this.value, this.message, this.cause);
}

public String Masked() {
public java.lang.String Masked() {
if (this.cause == null) {
return String.format("cannot set `%s`: %s", this.type.toString(), this.message);
return java.lang.String.format("cannot set `%s`: %s", this.type.toString(), this.message);
}
return String.format("cannot set `%s`: %s (%s)", this.type.toString(), this.message, this.cause);
return java.lang.String.format("cannot set `%s`: %s (%s)", this.type.toString(), this.message, this.cause);
}
}
117 changes: 117 additions & 0 deletions lib/src/test/java/io/cloudquery/scalar/StringTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package io.cloudquery.scalar;

import org.apache.arrow.vector.types.pojo.ArrowType;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;


public class StringTest {
@Test
public void testNew() {
assertDoesNotThrow(() -> {
new String();
});
}

@Test
public void testNewWithValidParam() {
assertDoesNotThrow(() -> {
new String(1);
new String("PT8H6M12.345S");
new String("");

Scalar<?> s = new String(null);
new String(s);
});
}

@Test
public void testToString() {
String string = new String();
assertEquals(Scalar.NULL_VALUE_STRING, string.toString());

assertDoesNotThrow(() -> {
string.set(1);
});
assertEquals("1", string.toString());

assertDoesNotThrow(() -> {
string.set(-1L);
});
assertEquals("-1", string.toString());
}

@Test
public void testDataType() {
String string = new String();
assertEquals(ArrowType.Utf8.INSTANCE, string.dataType());
assertEquals(new ArrowType.Utf8(), string.dataType());
}

@Test
public void testIsValid() {
String string = new String();
assertFalse(string.isValid());

assertDoesNotThrow(() -> {
string.set(1L);
});
assertTrue(string.isValid());
}

@Test
public void testSet() {
String string = new String();
assertDoesNotThrow(() -> {
string.set(1);
string.set(1L);
string.set("PT8H6M12.345S");
string.set(null);

Scalar<?> s = new String("");
string.set(s);
});
}

@Test
public void testGet() {
String string = new String();
assertFalse(string.isValid());
assertNull(string.get());

assertDoesNotThrow(() -> {
string.set(-1L);
});
assertTrue(string.isValid());
assertEquals("-1", string.get());

assertDoesNotThrow(() -> {
string.set("");
});
assertTrue(string.isValid());
assertEquals("", string.get());
}

@Test
public void testEquals() {
String a = new String();
String b = new String();
assertEquals(a, b);
assertNotEquals(a, null);
assertNotEquals(a, new Bool()); // we can't cast Bool to String
assertNotEquals(null, a);

assertDoesNotThrow(() -> {
a.set(-1L);
});
assertNotEquals(a, b);

assertDoesNotThrow(() -> {
for (Object obj : new Object[]{null, 0, 0L, -1, -1L, 1, 1L, "PT8H6M12.345S", ""}) {
a.set(obj);
assertEquals(a, new String(obj));
}
});
}
}
2 changes: 1 addition & 1 deletion lib/src/test/java/io/cloudquery/scalar/TimestampTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testToString() {
});
assertEquals("1970-01-01T00:00:00.001Z", timestamp.toString());

String ts = ZonedDateTime.now(ZoneOffset.UTC).toString();
java.lang.String ts = ZonedDateTime.now(ZoneOffset.UTC).toString();
assertDoesNotThrow(() -> {
timestamp.set(ts);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/src/test/java/io/cloudquery/scalar/UUIDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void testEquals() {

@Test
public void testCorrectEndianBehaviour() {
String expectUUID = "00010203-0405-0607-0809-0a0b0c0d0e0f";
java.lang.String expectUUID = "00010203-0405-0607-0809-0a0b0c0d0e0f";

UUID uuid = new UUID();
assertDoesNotThrow(() -> {
Expand Down

0 comments on commit 1fe0bb9

Please sign in to comment.