Skip to content

Commit

Permalink
Merge pull request #116 from denizzzka/empty_arrays
Browse files Browse the repository at this point in the history
empty arrays should not be treated as NULL values
  • Loading branch information
tchaloupka authored Aug 4, 2018
2 parents 61a0825 + 7fc06a3 commit 2dd33f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/dpq2/conv/arrays.d
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ if (isArrayType!T)
enum elemOid = detectOidTypeFromNative!ET;
auto arrOid = oidConvTo!("array")(elemOid); //TODO: check in CT for supported array types

// check for null value
static if (!isStaticArray!T)
{
if (v is null) return Value(ValueFormat.BINARY, arrOid);
}

// check for null element
static if (__traits(compiles, v[0] is null) || is(ET == Nullable!R,R))
{
Expand Down Expand Up @@ -166,6 +160,17 @@ if (isArrayType!T)
assert(varr[2].as!string == "baz");
}

{
string[] arr;

auto v = arr.toValue();
assert(v.oidType == OidType.TextArray);
assert(!v.isNull);

auto varr = v.asArray;
assert(varr.length == 0);
}

{
Nullable!string[] arr = [Nullable!string("foo"), Nullable!string.init, Nullable!string("baz")];

Expand Down
1 change: 1 addition & 0 deletions src/dpq2/conv/native_tests.d
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public void _integration_test( string connParam ) @system

//Arrays
C!(int[][])([[1,2],[3,4]], "int[]", "'{{1,2},{3,4}}'");
C!(int[])([], "int[]", "'{}'"); // empty array test
C!((Nullable!string)[])([Nullable!string("foo"), Nullable!string.init], "text[]", "'{foo,NULL}'");
C!(string[])(["foo","bar", "baz"], "text[]", "'{foo,bar,baz}'");
C!(PGjson[])([Json(["foo": Json(42)])], "json[]", `'{"{\"foo\":42}"}'`);
Expand Down

0 comments on commit 2dd33f0

Please sign in to comment.