Reflect optional by Brandon Tolsch

This commit is contained in:
Fangrui Song 2018-05-07 20:59:08 -07:00
parent 984c6367d1
commit a4a07120a1
6 changed files with 38 additions and 25 deletions

View File

@ -79,7 +79,10 @@ MAKE_REFLECT_STRUCT(Out_CclsCallHierarchy::Entry,
callType, callType,
numChildren, numChildren,
children); children);
MAKE_REFLECT_STRUCT(Out_CclsCallHierarchy, jsonrpc, id, result); MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_CclsCallHierarchy,
jsonrpc,
id,
result);
bool Expand(MessageHandler* m, bool Expand(MessageHandler* m,
Out_CclsCallHierarchy::Entry* entry, Out_CclsCallHierarchy::Entry* entry,

View File

@ -60,7 +60,10 @@ MAKE_REFLECT_STRUCT(Out_CclsInheritanceHierarchy::Entry,
location, location,
numChildren, numChildren,
children); children);
MAKE_REFLECT_STRUCT(Out_CclsInheritanceHierarchy, jsonrpc, id, result); MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_CclsInheritanceHierarchy,
jsonrpc,
id,
result);
bool Expand(MessageHandler* m, bool Expand(MessageHandler* m,
Out_CclsInheritanceHierarchy::Entry* entry, Out_CclsInheritanceHierarchy::Entry* entry,

View File

@ -59,7 +59,10 @@ MAKE_REFLECT_STRUCT(Out_CclsMemberHierarchy::Entry,
location, location,
numChildren, numChildren,
children); children);
MAKE_REFLECT_STRUCT(Out_CclsMemberHierarchy, jsonrpc, id, result); MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_CclsMemberHierarchy,
jsonrpc,
id,
result);
bool Expand(MessageHandler* m, bool Expand(MessageHandler* m,
Out_CclsMemberHierarchy::Entry* entry, Out_CclsMemberHierarchy::Entry* entry,

View File

@ -57,20 +57,10 @@ struct Out_TextDocumentHover : public lsOutMessage<Out_TextDocumentHover> {
std::optional<Result> result; std::optional<Result> result;
}; };
MAKE_REFLECT_STRUCT(Out_TextDocumentHover::Result, contents, range); MAKE_REFLECT_STRUCT(Out_TextDocumentHover::Result, contents, range);
void Reflect(Writer& visitor, Out_TextDocumentHover& value) { MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(Out_TextDocumentHover,
REFLECT_MEMBER_START(); jsonrpc,
REFLECT_MEMBER(jsonrpc); id,
REFLECT_MEMBER(id); result);
if (value.result)
REFLECT_MEMBER(result);
else {
// Empty optional<> is elided by the default serializer, we need to write
// |null| to be compliant with the LSP.
visitor.Key("result");
visitor.Null();
}
REFLECT_MEMBER_END();
}
struct Handler_TextDocumentHover : BaseMessageHandler<In_TextDocumentHover> { struct Handler_TextDocumentHover : BaseMessageHandler<In_TextDocumentHover> {
MethodType GetMethodType() const override { return kMethodType; } MethodType GetMethodType() const override { return kMethodType; }

View File

@ -20,14 +20,6 @@ struct Out_WorkspaceExecuteCommand
}; };
MAKE_REFLECT_STRUCT(Out_WorkspaceExecuteCommand, jsonrpc, id, result); MAKE_REFLECT_STRUCT(Out_WorkspaceExecuteCommand, jsonrpc, id, result);
void Reflect(Writer& visitor, Out_WorkspaceExecuteCommand& value) {
REFLECT_MEMBER_START();
REFLECT_MEMBER(jsonrpc);
REFLECT_MEMBER(id);
REFLECT_MEMBER(result);
REFLECT_MEMBER_END();
}
struct Handler_WorkspaceExecuteCommand struct Handler_WorkspaceExecuteCommand
: BaseMessageHandler<In_WorkspaceExecuteCommand> { : BaseMessageHandler<In_WorkspaceExecuteCommand> {
MethodType GetMethodType() const override { return kMethodType; } MethodType GetMethodType() const override { return kMethodType; }

View File

@ -19,6 +19,7 @@
enum class SerializeFormat { Binary, Json }; enum class SerializeFormat { Binary, Json };
struct JsonNull {}; struct JsonNull {};
struct mandatory_optional_tag {};
class Reader { class Reader {
public: public:
@ -77,6 +78,8 @@ struct IndexFile;
#define REFLECT_MEMBER_START() ReflectMemberStart(visitor, value) #define REFLECT_MEMBER_START() ReflectMemberStart(visitor, value)
#define REFLECT_MEMBER_END() ReflectMemberEnd(visitor, value); #define REFLECT_MEMBER_END() ReflectMemberEnd(visitor, value);
#define REFLECT_MEMBER(name) ReflectMember(visitor, #name, value.name) #define REFLECT_MEMBER(name) ReflectMember(visitor, #name, value.name)
#define REFLECT_MEMBER_MANDATORY_OPTIONAL(name) \
ReflectMember(visitor, #name, value.name, mandatory_optional_tag{})
#define REFLECT_MEMBER2(name, value) ReflectMember(visitor, name, value) #define REFLECT_MEMBER2(name, value) ReflectMember(visitor, name, value)
#define MAKE_REFLECT_TYPE_PROXY(type_name) \ #define MAKE_REFLECT_TYPE_PROXY(type_name) \
@ -93,6 +96,8 @@ struct IndexFile;
} }
#define _MAPPABLE_REFLECT_MEMBER(name) REFLECT_MEMBER(name); #define _MAPPABLE_REFLECT_MEMBER(name) REFLECT_MEMBER(name);
#define _MAPPABLE_REFLECT_MEMBER_MANDATORY_OPTIONAL(name) \
REFLECT_MEMBER_MANDATORY_OPTIONAL(name);
#define MAKE_REFLECT_EMPTY_STRUCT(type, ...) \ #define MAKE_REFLECT_EMPTY_STRUCT(type, ...) \
template <typename TVisitor> \ template <typename TVisitor> \
@ -109,6 +114,14 @@ struct IndexFile;
REFLECT_MEMBER_END(); \ REFLECT_MEMBER_END(); \
} }
#define MAKE_REFLECT_STRUCT_MANDATORY_OPTIONAL(type, ...) \
template <typename TVisitor> \
void Reflect(TVisitor& visitor, type& value) { \
REFLECT_MEMBER_START(); \
MACRO_MAP(_MAPPABLE_REFLECT_MEMBER_MANDATORY_OPTIONAL, __VA_ARGS__) \
REFLECT_MEMBER_END(); \
}
// clang-format off // clang-format off
// Config has many fields, we need to support at least its number of fields. // Config has many fields, we need to support at least its number of fields.
#define NUM_VA_ARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,N,...) N #define NUM_VA_ARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,N,...) N
@ -243,6 +256,15 @@ void ReflectMember(Writer& visitor, const char* name, Maybe<T>& value) {
} }
} }
template <typename T>
void ReflectMember(Writer& visitor,
const char* name,
T& value,
mandatory_optional_tag) {
visitor.Key(name);
Reflect(visitor, value);
}
// std::vector // std::vector
template <typename T> template <typename T>
void Reflect(Reader& visitor, std::vector<T>& values) { void Reflect(Reader& visitor, std::vector<T>& values) {