Skip to content

Commit

Permalink
Use protoDefaultValue instead of more probable user-defined defaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarp committed Jan 15, 2018
1 parent bcc3b27 commit 0f478ea
Show file tree
Hide file tree
Showing 18 changed files with 303 additions and 301 deletions.
26 changes: 13 additions & 13 deletions protoc-gen-d/google/protobuf/compiler/plugin.d
Expand Up @@ -5,29 +5,29 @@ import google.protobuf.descriptor;

class Version
{
@Proto(1) int major = defaultValue!int;
@Proto(2) int minor = defaultValue!int;
@Proto(3) int patch = defaultValue!int;
@Proto(4) string suffix = defaultValue!string;
@Proto(1) int major = protoDefaultValue!int;
@Proto(2) int minor = protoDefaultValue!int;
@Proto(3) int patch = protoDefaultValue!int;
@Proto(4) string suffix = protoDefaultValue!string;
}

class CodeGeneratorRequest
{
@Proto(1) string[] filesToGenerate = defaultValue!(string[]);
@Proto(2) string parameter = defaultValue!string;
@Proto(3) Version compilerVersion = defaultValue!Version;
@Proto(15) FileDescriptorProto[] protoFiles = defaultValue!(FileDescriptorProto[]);
@Proto(1) string[] filesToGenerate = protoDefaultValue!(string[]);
@Proto(2) string parameter = protoDefaultValue!string;
@Proto(3) Version compilerVersion = protoDefaultValue!Version;
@Proto(15) FileDescriptorProto[] protoFiles = protoDefaultValue!(FileDescriptorProto[]);
}

class CodeGeneratorResponse
{
@Proto(1) string error = defaultValue!string;
@Proto(15) File[] files = defaultValue!(File[]);
@Proto(1) string error = protoDefaultValue!string;
@Proto(15) File[] files = protoDefaultValue!(File[]);

static class File
{
@Proto(1) string name = defaultValue!string;
@Proto(2) string insertionPoint = defaultValue!string;
@Proto(15) string content = defaultValue!string;
@Proto(1) string name = protoDefaultValue!string;
@Proto(2) string insertionPoint = protoDefaultValue!string;
@Proto(15) string content = protoDefaultValue!string;
}
}
242 changes: 121 additions & 121 deletions protoc-gen-d/google/protobuf/descriptor.d

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions protoc-gen-d/protoc-gen-d.d
Expand Up @@ -369,9 +369,9 @@ class CodeGenerator

auto fieldTypeName = typeName(field);
if (fieldTypeName.endsWith("]"))
return " = defaultValue!(%s)".format(fieldTypeName);
return " = protoDefaultValue!(%s)".format(fieldTypeName);
else
return " = defaultValue!%s".format(fieldTypeName);
return " = protoDefaultValue!%s".format(fieldTypeName);
}

private string protocVersion;
Expand Down
8 changes: 4 additions & 4 deletions src/google/protobuf/any.d
Expand Up @@ -10,8 +10,8 @@ struct Any
{
private struct _Message
{
@Proto(1) string typeUrl = defaultValue!string;
@Proto(2) bytes value = defaultValue!bytes;
@Proto(1) string typeUrl = protoDefaultValue!string;
@Proto(2) bytes value = protoDefaultValue!bytes;
}

string typeUrl;
Expand Down Expand Up @@ -216,8 +216,8 @@ unittest
{
enum messageTypeFullName = "Foo";

@Proto(1) int intField = defaultValue!int;
@Proto(2) string stringField = defaultValue!string;
@Proto(1) int intField = protoDefaultValue!int;
@Proto(2) string stringField = protoDefaultValue!string;
}
auto foo1 = Foo(42, "abc");
Any anyFoo;
Expand Down
32 changes: 16 additions & 16 deletions src/google/protobuf/api.d
Expand Up @@ -9,28 +9,28 @@ import google.protobuf.type;

class Api
{
@Proto(1) string name = defaultValue!string;
@Proto(2) Method[] methods = defaultValue!(Method[]);
@Proto(3) Option[] options = defaultValue!(Option[]);
@Proto(4) string version_ = defaultValue!string;
@Proto(5) SourceContext sourceContext = defaultValue!SourceContext;
@Proto(6) Mixin[] mixins = defaultValue!(Mixin[]);
@Proto(7) Syntax syntax = defaultValue!Syntax;
@Proto(1) string name = protoDefaultValue!string;
@Proto(2) Method[] methods = protoDefaultValue!(Method[]);
@Proto(3) Option[] options = protoDefaultValue!(Option[]);
@Proto(4) string version_ = protoDefaultValue!string;
@Proto(5) SourceContext sourceContext = protoDefaultValue!SourceContext;
@Proto(6) Mixin[] mixins = protoDefaultValue!(Mixin[]);
@Proto(7) Syntax syntax = protoDefaultValue!Syntax;
}

class Method
{
@Proto(1) string name = defaultValue!string;
@Proto(2) string requestTypeUrl = defaultValue!string;
@Proto(3) bool requestStreaming = defaultValue!bool;
@Proto(4) string responseTypeUrl = defaultValue!string;
@Proto(5) bool responseStreaming = defaultValue!bool;
@Proto(6) Option[] options = defaultValue!(Option[]);
@Proto(7) Syntax syntax = defaultValue!Syntax;
@Proto(1) string name = protoDefaultValue!string;
@Proto(2) string requestTypeUrl = protoDefaultValue!string;
@Proto(3) bool requestStreaming = protoDefaultValue!bool;
@Proto(4) string responseTypeUrl = protoDefaultValue!string;
@Proto(5) bool responseStreaming = protoDefaultValue!bool;
@Proto(6) Option[] options = protoDefaultValue!(Option[]);
@Proto(7) Syntax syntax = protoDefaultValue!Syntax;
}

class Mixin
{
@Proto(1) string name = defaultValue!string;
@Proto(2) string root = defaultValue!string;
@Proto(1) string name = protoDefaultValue!string;
@Proto(2) string root = protoDefaultValue!string;
}
8 changes: 5 additions & 3 deletions src/google/protobuf/common.d
Expand Up @@ -2,12 +2,14 @@ module google.protobuf.common;

alias bytes = ubyte[];

auto defaultValue(T)()
auto protoDefaultValue(T)()
{
import std.traits : isFloatingPoint;
import std.traits : hasMember, isFloatingPoint;

static if (isFloatingPoint!T)
return cast(T) 0.0;
else static if ((is(T == class) || is(T == struct)) && hasMember!(T, "protoDefaultValue"))
return T.protoDefaultValue;
else
return T.init;
}
Expand Down Expand Up @@ -127,7 +129,7 @@ enum string oneofAccessors(alias field) = {
enum accessorName = oneofAccessorName!field;

return "
@property %1$s %2$s() { return %3$s == typeof(%3$s).%2$s ? _%2$s : defaultValue!(%1$s); }
@property %1$s %2$s() { return %3$s == typeof(%3$s).%2$s ? _%2$s : protoDefaultValue!(%1$s); }
@property void %2$s(%1$s _) { _%2$s = _; %3$s = typeof(%3$s).%2$s; }
".format(typeof(field).stringof, accessorName, oneofCaseFieldName!field);
}();
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/decoding.d
Expand Up @@ -150,7 +150,7 @@ if (isInputRange!R && isArray!T && !is(T == string) && !is(T == bytes))
return result.data;
}

T fromProtobuf(T, R)(ref R inputRange, T result = defaultValue!T)
T fromProtobuf(T, R)(ref R inputRange, T result = protoDefaultValue!T)
if (isInputRange!R && (is(T == class) || is(T == struct)))
{
import std.format : format;
Expand Down Expand Up @@ -270,7 +270,7 @@ if (isInputRange!R && isArray!T && !is(T == string) && !is(T == bytes) && !proto
static assert(is(ElementType!R == ubyte), "Input range should be an ubyte range");
static assert(validateProto!(proto, T));

auto newElement = defaultValue!(ElementType!T);
auto newElement = protoDefaultValue!(ElementType!T);
inputRange.fromProtobufByProto!proto(newElement);
field ~= newElement;
}
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/duration.d
Expand Up @@ -10,8 +10,8 @@ struct Duration
{
private struct _Message
{
@Proto(1) long seconds = defaultValue!long;
@Proto(2) int nanos = defaultValue!int;
@Proto(1) long seconds = protoDefaultValue!long;
@Proto(2) int nanos = protoDefaultValue!int;
}

StdDuration duration;
Expand Down
14 changes: 7 additions & 7 deletions src/google/protobuf/encoding.d
Expand Up @@ -170,12 +170,12 @@ unittest

class Foo
{
@Proto(1) int bar = defaultValue!int;
@Proto(3) bool qux = defaultValue!bool;
@Proto(2, Wire.fixed) long baz = defaultValue!long;
@Proto(4) string quux = defaultValue!string;
@Proto(1) int bar = protoDefaultValue!int;
@Proto(3) bool qux = protoDefaultValue!bool;
@Proto(2, Wire.fixed) long baz = protoDefaultValue!long;
@Proto(4) string quux = protoDefaultValue!string;

@Proto(5) Foo recursion = defaultValue!Foo;
@Proto(5) Foo recursion = protoDefaultValue!Foo;
}

Foo foo;
Expand All @@ -192,7 +192,7 @@ unittest
{
struct Foo
{
@Proto(1) int bar = defaultValue!int;
@Proto(1) int bar = protoDefaultValue!int;

auto toProtobuf()
{
Expand Down Expand Up @@ -224,7 +224,7 @@ private static auto toProtobufByField(alias field, T)(T message)
}
else
{
if (mixin(fieldInstanceName) == defaultValue!(typeof(field)))
if (mixin(fieldInstanceName) == protoDefaultValue!(typeof(field)))
return emptySizedRange!(typeof(mixin(fieldInstanceName).toProtobufByProto!proto));
}

Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/field_mask.d
Expand Up @@ -5,7 +5,7 @@ import google.protobuf;

struct FieldMask
{
@Proto(1) string[] paths = defaultValue!(string[]);
@Proto(1) string[] paths = protoDefaultValue!(string[]);

JSONValue toJSONValue()()
{
Expand All @@ -25,7 +25,7 @@ struct FieldMask

if (value.type == JSON_TYPE.NULL)
{
paths = defaultValue!(string[]);
paths = protoDefaultValue!(string[]);
return this;
}

Expand Down
18 changes: 9 additions & 9 deletions src/google/protobuf/json_decoding.d
Expand Up @@ -10,7 +10,7 @@ if (isBoolean!T)
switch (value.type)
{
case JSON_TYPE.NULL:
return defaultValue!T;
return protoDefaultValue!T;
case JSON_TYPE.TRUE:
return true;
case JSON_TYPE.FALSE:
Expand All @@ -32,7 +32,7 @@ if (isIntegral!T)
switch (value.type)
{
case JSON_TYPE.NULL:
return defaultValue!T;
return protoDefaultValue!T;
case JSON_TYPE.STRING:
return value.str.to!T;
case JSON_TYPE.INTEGER:
Expand Down Expand Up @@ -72,7 +72,7 @@ if (isFloatingPoint!T)
switch (value.type)
{
case JSON_TYPE.NULL:
return defaultValue!T;
return protoDefaultValue!T;
case JSON_TYPE.STRING:
switch (value.str)
{
Expand Down Expand Up @@ -107,7 +107,7 @@ if (is(T == string))
import std.exception : enforce;

if (value.isNull)
return defaultValue!T;
return protoDefaultValue!T;

enforce!ProtobufException(value.type == JSON_TYPE.STRING, "JSON string expected");
return value.str;
Expand All @@ -121,7 +121,7 @@ if (is(T == bytes))
import std.json : JSON_TYPE;

if (value.isNull)
return defaultValue!T;
return protoDefaultValue!T;

enforce!ProtobufException(value.type == JSON_TYPE.STRING, "JSON base64 encoded binary expected");
return Base64.decode(value.str);
Expand All @@ -136,7 +136,7 @@ if (isArray!T && !is(T == string) && !is(T == bytes))
import std.range : ElementType;

if (value.isNull)
return defaultValue!T;
return protoDefaultValue!T;

enforce!ProtobufException(value.type == JSON_TYPE.ARRAY, "JSON array expected");
return value.array.map!(a => a.fromJSONValue!(ElementType!T)).array;
Expand All @@ -150,7 +150,7 @@ if (isAssociativeArray!T)
import std.traits : KeyType, ValueType;

if (value.isNull)
return defaultValue!T;
return protoDefaultValue!T;

enforce!ProtobufException(value.type == JSON_TYPE.OBJECT, "JSON object expected");
foreach (k, v; value.object)
Expand Down Expand Up @@ -204,7 +204,7 @@ unittest
assertThrown!ProtobufException(fromJSONValue!(bool[int])(parseJSON(`{"foo": false, "2": true}`)));
}

T fromJSONValue(T)(JSONValue value, T result = defaultValue!T)
T fromJSONValue(T)(JSONValue value, T result = protoDefaultValue!T)
if (is(T == class) || is(T == struct))
{
import std.algorithm : findAmong;
Expand Down Expand Up @@ -240,7 +240,7 @@ if (is(T == class) || is(T == struct))
}();

if (value.isNull)
return defaultValue!T;
return protoDefaultValue!T;

enforce!ProtobufException(value.type == JSON_TYPE.OBJECT, "JSON object expected");

Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/json_encoding.d
Expand Up @@ -112,7 +112,7 @@ if (is(T == class) || is(T == struct))
}
else
{
if (mixin("value." ~ fieldName) != defaultValue!(typeof(mixin("T." ~ fieldName))))
if (mixin("value." ~ fieldName) != protoDefaultValue!(typeof(mixin("T." ~ fieldName))))
members[fieldName] = mixin("value." ~ fieldName).toJSONValue;
}
}
Expand Down Expand Up @@ -156,7 +156,7 @@ unittest
void clearMeterOrInchCase() { _meterOrInchCase = MeterOrInchCase.meterOrInchNotSet; }
@Oneof("_meterOrInchCase") union
{
@Proto(3) int _meter = defaultValue!int; mixin(oneofAccessors!_meter);
@Proto(3) int _meter = protoDefaultValue!int; mixin(oneofAccessors!_meter);
@Proto(5) int _inch; mixin(oneofAccessors!_inch);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/source_context.d
Expand Up @@ -7,5 +7,5 @@ import google.protobuf;

class SourceContext
{
@Proto(1) string fileName = defaultValue!string;
@Proto(1) string fileName = protoDefaultValue!string;
}
10 changes: 5 additions & 5 deletions src/google/protobuf/struct_.d
Expand Up @@ -7,7 +7,7 @@ import google.protobuf;

class Struct
{
@Proto(1) Value[string] fields = defaultValue!(Value[string]);
@Proto(1) Value[string] fields = protoDefaultValue!(Value[string]);

this()
{
Expand Down Expand Up @@ -37,7 +37,7 @@ class Struct

if (value.type == JSON_TYPE.NULL)
{
fields = defaultValue!(Value[string]);
fields = protoDefaultValue!(Value[string]);
return this;
}

Expand Down Expand Up @@ -65,7 +65,7 @@ class Value
void clearKind() { _kindCase = KindCase.kindNotSet; }
@Oneof("_kindCase") union
{
@Proto(1) NullValue _nullValue = defaultValue!NullValue; mixin(oneofAccessors!_nullValue);
@Proto(1) NullValue _nullValue = protoDefaultValue!NullValue; mixin(oneofAccessors!_nullValue);
@Proto(2) double _numberValue; mixin(oneofAccessors!_numberValue);
@Proto(3) string _stringValue; mixin(oneofAccessors!_stringValue);
@Proto(4) bool _boolValue; mixin(oneofAccessors!_boolValue);
Expand Down Expand Up @@ -190,7 +190,7 @@ enum NullValue

class ListValue
{
@Proto(1) Value[] values = defaultValue!(Value[]);
@Proto(1) Value[] values = protoDefaultValue!(Value[]);

this()
{
Expand Down Expand Up @@ -218,7 +218,7 @@ class ListValue

if (value.type == JSON_TYPE.NULL)
{
values = defaultValue!(Value[]);
values = protoDefaultValue!(Value[]);
return this;
}

Expand Down

0 comments on commit 0f478ea

Please sign in to comment.