denizzzka / dpq2

@@ -53,7 +53,7 @@
Loading
53 53
    Throws: AssertError if the db value is NULL and Nullable is not used to retrieve the value
54 54
*/
55 55
T as(T)(in Value v) pure @trusted
56 -
if(is(T : string))
56 +
if(is(T : const(char)[]))
57 57
{
58 58
    if(v.format == VF.BINARY)
59 59
    {
@@ -108,7 +108,7 @@
Loading
108 108
    Throws: AssertError if the db value is NULL and Nullable is not used to retrieve the value
109 109
*/
110 110
T as(T)(in Value v)
111 -
if(!is(T : string) && !is(T == Bson))
111 +
if(!is(T : const(char)[]) && !is(T == Bson))
112 112
{
113 113
    if(!(v.format == VF.BINARY))
114 114
        throw new AE(ET.NOT_BINARY,
@@ -142,9 +142,9 @@
Loading
142 142
    return binaryValueAs!T(v);
143 143
}
144 144
145 -
string valueAsString(in Value v) pure
145 +
char[] valueAsString(in Value v) pure
146 146
{
147 -
    return (cast(const(char[])) v.data).to!string;
147 +
    return (cast(const(char[])) v.data).to!(char[]);
148 148
}
149 149
150 150
/// Returns value as bytes from binary formatted field

@@ -3,7 +3,7 @@
Loading
3 3
4 4
@safe:
5 5
6 -
public import dpq2.conv.arrays : isArrayType, toValue;
6 +
public import dpq2.conv.arrays : isArrayType, toValue, isStaticArrayString;
7 7
public import dpq2.conv.geometric : toValue;
8 8
import dpq2.conv.time : POSTGRES_EPOCH_DATE, TimeStamp, TimeStampUTC;
9 9
import dpq2.oids : detectOidTypeFromNative, oidConvTo, OidType;
@@ -13,7 +13,7 @@
Loading
13 13
import std.datetime.date: Date, DateTime, TimeOfDay;
14 14
import std.datetime.systime: SysTime;
15 15
import std.datetime.timezone: LocalTime, TimeZone, UTC;
16 -
import std.traits: isImplicitlyConvertible, isNumeric, isInstanceOf, OriginalType, Unqual;
16 +
import std.traits: isImplicitlyConvertible, isNumeric, isInstanceOf, OriginalType, Unqual, isSomeString;
17 17
import std.typecons : Nullable;
18 18
import std.uuid: UUID;
19 19
import vibe.data.json: Json;
@@ -151,16 +151,25 @@
Loading
151 151
    If NULL is a desired DB value, Nullable!string can be used instead.
152 152
*/
153 153
Value toValue(T)(T v, ValueFormat valueFormat = ValueFormat.BINARY) @trusted
154 -
if(is(T : string))
154 +
if(isSomeString!T || isStaticArrayString!T)
155 155
{
156 -
    import std.string : representation;
156 +
    static if(is(T == string))
157 +
    {
158 +
        import std.string : representation;
157 159
158 -
    static assert(isImplicitlyConvertible!(T, string));
159 -
    auto buf = (cast(string) v).representation;
160 +
        static assert(isImplicitlyConvertible!(T, string));
161 +
        auto buf = (cast(string) v).representation;
160 162
161 -
    if(valueFormat == ValueFormat.TEXT) buf ~= 0; // for prepareArgs only
163 +
        if(valueFormat == ValueFormat.TEXT) buf ~= 0; // for prepareArgs only
162 164
163 -
    return Value(buf, OidType.Text, false, valueFormat);
165 +
        return Value(buf, OidType.Text, false, valueFormat);
166 +
    }
167 +
    else
168 +
    {
169 +
        // convert to a string
170 +
        import std.conv : to;
171 +
        return toValue(v.to!string, valueFormat);
172 +
    }
164 173
}
165 174
166 175
/// Constructs Value from array of bytes
@@ -519,3 +528,15 @@
Loading
519 528
    assert(!nv.as!(Nullable!Json).isNull);
520 529
    assert(nv.as!(Nullable!Json).get == j);
521 530
}
531 +
532 +
unittest
533 +
{
534 +
    import dpq2.conv.to_d_types : as;
535 +
    char[2] arr;
536 +
    auto v = arr.toValue();
537 +
    assert(v.oidType == OidType.Text);
538 +
    assert(!v.isNull);
539 +
540 +
    auto varr = v.as!string;
541 +
    assert(varr.length == 2);
542 +
}

@@ -86,7 +86,7 @@
Loading
86 86
{
87 87
    if(v.format == ValueFormat.TEXT)
88 88
    {
89 -
        const text = v.valueAsString;
89 +
        immutable text = v.valueAsString;
90 90
91 91
        if(v.oidType == OidType.Json)
92 92
        {

@@ -6,13 +6,26 @@
Loading
6 6
import dpq2.oids : OidType;
7 7
import dpq2.value;
8 8
9 -
import std.traits : isArray, isAssociativeArray;
9 +
import std.traits : isArray, isAssociativeArray, isSomeString;
10 10
import std.range : ElementType;
11 11
import std.typecons : Nullable;
12 12
import std.exception: assertThrown;
13 13
14 14
@safe:
15 15
16 +
template isStaticArrayString(T)
17 +
{
18 +
    import std.traits : isStaticArray;
19 +
    static if(isStaticArray!T)
20 +
        enum isStaticArrayString = isSomeString!(typeof(T.init[]));
21 +
    else
22 +
        enum isStaticArrayString = false;
23 +
}
24 +
25 +
static assert(isStaticArrayString!(char[2]));
26 +
static assert(!isStaticArrayString!string);
27 +
static assert(!isStaticArrayString!(ubyte[2]));
28 +
16 29
// From array to Value:
17 30
18 31
template isArrayType(T)
@@ -20,13 +33,15 @@
Loading
20 33
    import dpq2.conv.geometric : isValidPolygon;
21 34
    import std.traits : Unqual;
22 35
23 -
    enum isArrayType = isArray!T && !isAssociativeArray!T && !isValidPolygon!T && !is(Unqual!(ElementType!T) == ubyte) && !is(T : string);
36 +
    enum isArrayType = isArray!T && !isAssociativeArray!T && !isValidPolygon!T && !is(Unqual!(ElementType!T) == ubyte) && !isSomeString!T
37 +
        && !isStaticArrayString!T;
24 38
}
25 39
26 40
static assert(isArrayType!(int[]));
27 41
static assert(!isArrayType!(int[string]));
28 42
static assert(!isArrayType!(ubyte[]));
29 43
static assert(!isArrayType!(string));
44 +
static assert(!isArrayType!(char[2]));
30 45
31 46
/// Write array element into buffer
32 47
private void writeArrayElement(R, T)(ref R output, T item, ref int counter)

@@ -170,7 +170,7 @@
Loading
170 170
    import std.bitmanip : BitArray;
171 171
    import std.datetime.date : StdDate = Date, TimeOfDay, DateTime;
172 172
    import std.datetime.systime : SysTime;
173 -
    import std.traits : Unqual;
173 +
    import std.traits : Unqual, isSomeString;
174 174
    import std.uuid : StdUUID = UUID;
175 175
    static import dpq2.conv.time;
176 176
    import vibe.data.json : VibeJson = Json;
@@ -179,7 +179,7 @@
Loading
179 179
180 180
    with(OidType)
181 181
    {
182 -
        static if(is(UT == string)){ return Text; } else
182 +
        static if(isSomeString!UT){ return Text; } else
183 183
        static if(is(UT == ubyte[])){ return ByteArray; } else
184 184
        static if(is(UT == bool)){ return Bool; } else
185 185
        static if(is(UT == short)){ return Int2; } else
Files Coverage
src/dpq2 54.83%
integration_tests/integration_tests.d 0.00%
Project Totals (19 files) 54.58%
1188.17
TRAVIS_OS_NAME=linux
1187.17
TRAVIS_OS_NAME=linux
1190.17
TRAVIS_OS_NAME=linux
1189.17
TRAVIS_OS_NAME=linux
1191.17
TRAVIS_OS_NAME=linux

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading