Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++][Ue4] various bus fixes #6539

Merged
merged 4 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,20 @@ public String toVarName(String name) {
name = name.toLowerCase(Locale.ROOT);
}

//Unreal variable names are CamelCase
String camelCaseName = camelize(name, false);

//Avoid empty variable name at all costs
if(!camelCaseName.isEmpty()) {
name = camelCaseName;
}

// for reserved word or word starting with number, append _
if (isReservedWord(name) || name.matches("^\\d.*")) {
name = escapeReservedWord(name);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the special handling of the reserved words may not work here (before and after this PR).

I'll file another PR to address it later.


//Unreal variable names are CamelCase
return camelize(name, false);

return name;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class {{unrealModuleName}} : ModuleRules
"Json",
}
);
PCHUsage = PCHUsageMode.NoPCHs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public:
{{#responses.0}}
void SetHttpResponseCode(EHttpResponseCodes::Type InHttpResponseCode) final;
{{/responses.0}}
bool FromJson(const TSharedPtr<FJsonValue>& JsonObject) final;
bool FromJson(const TSharedPtr<FJsonValue>& JsonValue) final;

{{#returnType}}{{{returnType}}} Content;{{/returnType}}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Serialization/JsonSerializer.h"
#include "Dom/JsonObject.h"
#include "Misc/Base64.h"
#include "PlatformHttp.h"

class IHttpRequest;

Expand Down Expand Up @@ -196,10 +197,22 @@ inline FString CollectionToUrlString_multi(const TArray<T>& Collection, const TC

//////////////////////////////////////////////////////////////////////////

template<typename T, typename std::enable_if<!std::is_base_of<Model, T>::value, int>::type = 0>
inline void WriteJsonValue(JsonWriter& Writer, const T& Value)
inline void WriteJsonValue(JsonWriter& Writer, const TSharedPtr<FJsonObject>& Value)
{
Writer->WriteValue(Value);
if (Value.IsValid())
{
FJsonSerializer::Serialize(Value.ToSharedRef(), Writer, false);
}
else
{
Writer->WriteObjectStart();
Writer->WriteObjectEnd();
}
}

inline void WriteJsonValue(JsonWriter& Writer, const TArray<uint8>& Value)
{
Writer->WriteValue(ToString(Value));
}

inline void WriteJsonValue(JsonWriter& Writer, const FDateTime& Value)
Expand All @@ -212,6 +225,12 @@ inline void WriteJsonValue(JsonWriter& Writer, const Model& Value)
Value.WriteJson(Writer);
}

template<typename T, typename std::enable_if<!std::is_base_of<Model, T>::value, int>::type = 0>
inline void WriteJsonValue(JsonWriter& Writer, const T& Value)
{
Writer->WriteValue(Value);
}

template<typename T>
inline void WriteJsonValue(JsonWriter& Writer, const TArray<T>& Value)
{
Expand All @@ -235,54 +254,8 @@ inline void WriteJsonValue(JsonWriter& Writer, const TMap<FString, T>& Value)
Writer->WriteObjectEnd();
}

inline void WriteJsonValue(JsonWriter& Writer, const TSharedPtr<FJsonObject>& Value)
{
if (Value.IsValid())
{
FJsonSerializer::Serialize(Value.ToSharedRef(), Writer, false);
}
else
{
Writer->WriteObjectStart();
Writer->WriteObjectEnd();
}
}

inline void WriteJsonValue(JsonWriter& Writer, const TArray<uint8>& Value)
{
Writer->WriteValue(ToString(Value));
}

//////////////////////////////////////////////////////////////////////////

template<typename T>
inline bool TryGetJsonValue(const TSharedPtr<FJsonObject>& JsonObject, const FString& Key, T& Value)
{
const TSharedPtr<FJsonValue> JsonValue = JsonObject->TryGetField(Key);
if (JsonValue.IsValid() && !JsonValue->IsNull())
{
return TryGetJsonValue(JsonValue, Value);
}
return false;
}

template<typename T>
inline bool TryGetJsonValue(const TSharedPtr<FJsonObject>& JsonObject, const FString& Key, TOptional<T>& OptionalValue)
{
if(JsonObject->HasField(Key))
{
T Value;
if (TryGetJsonValue(JsonObject, Key, Value))
{
OptionalValue = Value;
return true;
}
else
return false;
}
return true; // Absence of optional value is not a parsing error
}

inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, FString& Value)
{
FString TmpValue;
Expand Down Expand Up @@ -316,13 +289,23 @@ inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, bool& Value
return false;
}

template<typename T, typename std::enable_if<!std::is_base_of<Model, T>::value, int>::type = 0>
inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, T& Value)
inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, TSharedPtr<FJsonObject>& JsonObjectValue)
{
T TmpValue;
if (JsonValue->TryGetNumber(TmpValue))
const TSharedPtr<FJsonObject>* Object;
if (JsonValue->TryGetObject(Object))
{
Value = TmpValue;
JsonObjectValue = *Object;
return true;
}
return false;
}

inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, TArray<uint8>& Value)
{
FString TmpValue;
if (JsonValue->TryGetString(TmpValue))
{
Base64UrlDecode(TmpValue, Value);
return true;
}
else
Expand All @@ -331,9 +314,18 @@ inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, T& Value)

inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, Model& Value)
{
const TSharedPtr<FJsonObject>* Object;
if (JsonValue->TryGetObject(Object))
return Value.FromJson(*Object);
return Value.FromJson(JsonValue);
}

template<typename T, typename std::enable_if<!std::is_base_of<Model, T>::value, int>::type = 0>
inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, T& Value)
{
T TmpValue;
if (JsonValue->TryGetNumber(TmpValue))
{
Value = TmpValue;
return true;
}
else
return false;
}
Expand Down Expand Up @@ -377,27 +369,32 @@ inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, TMap<FStrin
return false;
}

inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, TSharedPtr<FJsonObject>& JsonObjectValue)
template<typename T>
inline bool TryGetJsonValue(const TSharedPtr<FJsonObject>& JsonObject, const FString& Key, T& Value)
{
const TSharedPtr<FJsonObject>* Object;
if (JsonValue->TryGetObject(Object))
const TSharedPtr<FJsonValue> JsonValue = JsonObject->TryGetField(Key);
if (JsonValue.IsValid() && !JsonValue->IsNull())
{
JsonObjectValue = *Object;
return true;
return TryGetJsonValue(JsonValue, Value);
}
return false;
}

inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, TArray<uint8>& Value)
template<typename T>
inline bool TryGetJsonValue(const TSharedPtr<FJsonObject>& JsonObject, const FString& Key, TOptional<T>& OptionalValue)
{
FString TmpValue;
if (JsonValue->TryGetString(TmpValue))
if(JsonObject->HasField(Key))
{
Base64UrlDecode(TmpValue, Value);
return true;
T Value;
if (TryGetJsonValue(JsonObject, Key, Value))
{
OptionalValue = Value;
return true;
}
else
return false;
}
else
return false;
return true; // Absence of optional value is not a parsing error
}

{{#cppNamespaceDeclarations}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Interfaces/IHttpRequest.h"
#include "PlatformHttp.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"

{{#cppNamespaceDeclarations}}
namespace {{this}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class {{dllapi}} Model
public:
virtual ~Model() {}
virtual void WriteJson(JsonWriter& Writer) const = 0;
virtual bool FromJson(const TSharedPtr<FJsonObject>& JsonObject) = 0;
virtual bool FromJson(const TSharedPtr<FJsonValue>& JsonValue) = 0;
};

class {{dllapi}} Request
Expand All @@ -33,7 +33,7 @@ class {{dllapi}} Response
{
public:
virtual ~Response() {}
virtual bool FromJson(const TSharedPtr<FJsonValue>& JsonObject) = 0;
virtual bool FromJson(const TSharedPtr<FJsonValue>& JsonValue) = 0;

void SetSuccessful(bool InSuccessful) { Successful = InSuccessful; }
bool IsSuccessful() const { return Successful; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,26 @@ class {{dllapi}} {{classname}} : public Model
{
public:
virtual ~{{classname}}() {}
bool FromJson(const TSharedPtr<FJsonObject>& JsonObject) final;
bool FromJson(const TSharedPtr<FJsonValue>& JsonValue) final;
void WriteJson(JsonWriter& Writer) const final;

{{#isString}}
{{#isEnum}}
{{#allowableValues}}
enum class Values
{
{{#enumVars}}
{{name}},
{{/enumVars}}
};

Values Value{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}};
{{/allowableValues}}
{{/isEnum}}
{{^isEnum}}
FString Value{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}};
{{/isEnum}}
{{/isString}}
{{#vars}}
{{#isEnum}}
{{#allowableValues}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,54 @@ namespace {{this}}
{
{{/cppNamespaceDeclarations}}
{{#models}}{{#model}}
{{#isEnum}}
inline FString ToString(const {{classname}}::Values& Value)
{
{{#allowableValues}}
switch (Value)
{
{{#enumVars}}
case {{classname}}::Values::{{name}}:
return TEXT({{{value}}});
{{/enumVars}}
}
{{/allowableValues}}

UE_LOG(Log{{unrealModuleName}}, Error, TEXT("Invalid {{classname}}::Values Value (%d)"), (int)Value);
return TEXT("");
}

inline FStringFormatArg ToStringFormatArg(const {{classname}}::Values& Value)
{
return FStringFormatArg(ToString(Value));
}

inline void WriteJsonValue(JsonWriter& Writer, const {{classname}}::Values& Value)
{
WriteJsonValue(Writer, ToString(Value));
}

inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, {{classname}}::Values& Value)
{
{{#allowableValues}}
FString TmpValue;
if (JsonValue->TryGetString(TmpValue))
{
static TMap<FString, {{classname}}::Values> StringToEnum = { {{#enumVars}}
{ TEXT({{{value}}}), {{classname}}::Values::{{name}} },{{/enumVars}} };

const auto Found = StringToEnum.Find(TmpValue);
if(Found)
{
Value = *Found;
return true;
}
}
{{/allowableValues}}
return false;
}

{{/isEnum}}
{{#hasEnums}}
{{#vars}}
{{#isEnum}}
Expand Down Expand Up @@ -65,9 +113,10 @@ inline bool TryGetJsonValue(const TSharedPtr<FJsonValue>& JsonValue, {{classname
{{/hasEnums}}
void {{classname}}::WriteJson(JsonWriter& Writer) const
{
{{#parent}}
#error inheritance not handled right now
{{/parent}}
{{#isString}}
WriteJsonValue(Writer, Value);
{{/isString}}
{{^isString}}
Writer->WriteObjectStart();
{{#vars}}
{{#required}}
Expand All @@ -81,18 +130,29 @@ void {{classname}}::WriteJson(JsonWriter& Writer) const
{{/required}}
{{/vars}}
Writer->WriteObjectEnd();
{{/isString}}
}

bool {{classname}}::FromJson(const TSharedPtr<FJsonObject>& JsonObject)
bool {{classname}}::FromJson(const TSharedPtr<FJsonValue>& JsonValue)
{
{{#isString}}
return TryGetJsonValue(JsonValue, Value);
{{/isString}}
{{^isString}}
const TSharedPtr<FJsonObject>* Object;
if (!JsonValue->TryGetObject(Object))
return false;

bool ParseSuccess = true;

{{#vars}}
ParseSuccess &= TryGetJsonValue(JsonObject, TEXT("{{baseName}}"), {{name}});
ParseSuccess &= TryGetJsonValue(*Object, TEXT("{{baseName}}"), {{name}});
{{/vars}}

return ParseSuccess;
{{/isString}}
}

{{/model}}
{{/models}}
{{#cppNamespaceDeclarations}}
Expand Down
Loading