chore: Add smart parse and pretty name fns for proto enums

Mainly to ease the pain of representing enums as strings in python md
and execution plugins
This commit is contained in:
John Colvin committed 2025-06-06 17:41:53 +00:00
1 parent 2cd75209d2
commit 092ac21fce
9 files changed
+205 -9

No files matched your search

+9
View File
@@ -16,6 +16,7 @@ namespace bts::api::domain {
* insensitive, and makes the 'VENUE_' prefix optional.
*/
bool venue_parse_smart(const std::string &str, Venue *result);
std::string venue_name_pretty(Venue venue);
std::ostream &operator<<(std::ostream &out, const Venue &venue) noexcept;
@@ -38,6 +39,14 @@ inline bool venue_parse_smart(const std::string &str, Venue *result) {
return Venue_Parse(s, result);
}
inline std::string venue_name_pretty(Venue venue) {
auto name = Venue_Name(venue);
if (name.starts_with("VENUE_")) {
return name.substr(strlen("VENUE_"));
}
return name;
}
PB_ENUM_FROM_JSON(Venue)
PB_ENUM_TO_JSON(Venue)
@@ -1,6 +1,7 @@
#pragma once
#include "spark/api/services/OrderService.pb.h"
#include "spark/api/services/native_order_status.h"
#include "spark/api/services/native_order_type.h"
#include "spark/api/services/order_status.h"
#include "spark/api/services/time_in_force.h"
@@ -35,9 +36,6 @@ PB_ENUM_TO_JSON(CustomerOrFirm)
void to_json(nlohmann::json &j, const RoutingRecord &r);
void from_json(const nlohmann::json &j, RoutingRecord &r);
PB_ENUM_FROM_JSON(NativeOrderStatus)
PB_ENUM_TO_JSON(NativeOrderStatus)
PB_ENUM_FROM_JSON(NativeOrderState_SubError)
PB_ENUM_TO_JSON(NativeOrderState_SubError)
@@ -70,12 +68,6 @@ FMT_IMPL(
"{}",
bts::api::orderman::SelfMatchPreventionInstruction_Name(s));
FMT_IMPL(
bts::api::orderman::NativeOrderStatus,
s,
"{}",
bts::api::orderman::NativeOrderStatus_Name(s));
FMT_IMPL(
bts::api::orderman::NativeOrderState_SubError,
s,
@@ -0,0 +1,66 @@
#pragma once
#include "spark/api/services/OrderService.pb.h"
#include "spark/core/EnumMacros.h"
#include "spark/core/FMT_IMPL.h"
#include "spark/io/JsonReader.h"
#include "spark/logging/logging.h"
#include "spark/utils/string_utils.h"
#include <nlohmann/json.hpp>
namespace bts::api::orderman {
/**
* Parses ExecType from a string. Unlike `bts::api::orderman::ExecType_Parse`,
* this implementation is case insensitive, and makes the 'EXEC_TYPE_' prefix optional.
*/
bool exec_type_parse_smart(
const std::string &str, ExecType *result);
inline std::string exec_type_name_pretty(ExecType type);
PB_ENUM_FROM_JSON(ExecType)
PB_ENUM_TO_JSON(ExecType)
std::ostream &operator<<(std::ostream &out, ExecType t) noexcept;
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline bool exec_type_parse_smart(
const std::string &str, ExecType *result) {
if (ExecType_Parse(str, result)) {
return true;
}
std::string s = bts::system::string_utils::copy_to_upper(str);
if (ExecType_Parse(s, result)) {
return true;
}
const char *prefix = "EXEC_TYPE_";
if (!s.starts_with(prefix)) {
s.insert(0, prefix);
}
return ExecType_Parse(s, result);
}
inline std::string exec_type_name_pretty(ExecType type) {
auto name = bts::api::orderman::ExecType_Name(type);
if (name.starts_with("EXEC_TYPE_")) {
return name.substr(strlen("EXEC_TYPE_"));
}
return name;
}
inline std::ostream &operator<<(std::ostream &out, ExecType t) noexcept {
out << ExecType_Name(t);
return out;
}
} // namespace bts::api::orderman
FMT_IMPL(
bts::api::orderman::ExecType,
t,
"{}",
bts::api::orderman::ExecType_Name(t));
@@ -0,0 +1,58 @@
#pragma once
#include "spark/api/services/OrderService.pb.h"
#include "spark/core/EnumMacros.h"
#include "spark/core/FMT_IMPL.h"
#include "spark/io/JsonReader.h"
#include "spark/logging/logging.h"
#include "spark/utils/string_utils.h"
#include <nlohmann/json.hpp>
namespace bts::api::orderman {
/**
* Parses NativeOrderStatus from a string. Unlike `bts::api::orderman::NativeOrderStatus_Parse`,
* this implementation is case insensitive, and makes the 'NATIVE_ORDER_STATUS_' prefix optional.
*/
bool native_order_status_parse_smart(
const std::string &str, NativeOrderStatus *result);
PB_ENUM_FROM_JSON(NativeOrderStatus)
PB_ENUM_TO_JSON(NativeOrderStatus)
std::ostream &operator<<(std::ostream &out, NativeOrderStatus s) noexcept;
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline bool native_order_status_parse_smart(
const std::string &str, NativeOrderStatus *result) {
if (NativeOrderStatus_Parse(str, result)) {
return true;
}
std::string s = bts::system::string_utils::copy_to_upper(str);
if (NativeOrderStatus_Parse(s, result)) {
return true;
}
const char *prefix = "NATIVE_ORDER_STATUS_";
if (!s.starts_with(prefix)) {
s.insert(0, prefix);
}
return NativeOrderStatus_Parse(s, result);
}
inline std::ostream &operator<<(
std::ostream &out, NativeOrderStatus s) noexcept {
out << NativeOrderStatus_Name(s);
return out;
}
} // namespace bts::api::orderman
FMT_IMPL(
bts::api::orderman::NativeOrderStatus,
s,
"{}",
bts::api::orderman::NativeOrderStatus_Name(s));
@@ -17,6 +17,7 @@ namespace bts::api::orderman {
*/
bool native_order_type_parse_smart(
const std::string &str, NativeOrderType *result);
inline std::string native_order_type_name_pretty(NativeOrderType type);
PB_ENUM_FROM_JSON(NativeOrderType)
PB_ENUM_TO_JSON(NativeOrderType)
@@ -43,6 +44,14 @@ inline bool native_order_type_parse_smart(
return NativeOrderType_Parse(s, result);
}
inline std::string native_order_type_name_pretty(NativeOrderType type) {
auto name = bts::api::orderman::NativeOrderType_Name(type);
if (name.starts_with("NATIVE_ORDER_TYPE_")) {
return name.substr(strlen("NATIVE_ORDER_TYPE_"));
}
return name;
}
inline std::ostream &operator<<(std::ostream &out, NativeOrderType t) noexcept {
out << NativeOrderType_Name(t);
return out;
@@ -16,6 +16,7 @@ namespace bts::api::orderman {
* this implementation is case insensitive, and makes the 'ORDER_STATUS_' prefix optional.
*/
bool order_status_parse_smart(const std::string &str, OrderStatus *result);
std::string order_status_name_pretty(OrderStatus result);
PB_ENUM_FROM_JSON(OrderStatus)
PB_ENUM_TO_JSON(OrderStatus)
@@ -42,6 +43,14 @@ inline bool order_status_parse_smart(
return OrderStatus_Parse(s, result);
}
inline std::string order_status_name_pretty(OrderStatus status) {
auto name = bts::api::orderman::OrderStatus_Name(status);
if (name.starts_with("ORDER_STATUS_")) {
return name.substr(strlen("ORDER_STATUS_"));
}
return name;
}
inline std::ostream &operator<<(std::ostream &out, OrderStatus s) noexcept {
out << OrderStatus_Name(s);
return out;
@@ -16,6 +16,7 @@ namespace bts::api::orderman {
* this implementation is case insensitive, and makes the 'TIME_IN_FORCE_' prefix optional.
*/
bool time_in_force_parse_smart(const std::string &str, TimeInForce *result);
std::string time_in_force_name_pretty(NativeOrderType type);
PB_ENUM_FROM_JSON(TimeInForce)
PB_ENUM_TO_JSON(TimeInForce)
@@ -42,6 +43,14 @@ inline bool time_in_force_parse_smart(
return TimeInForce_Parse(s, result);
}
inline std::string time_in_force_name_pretty(TimeInForce tif) {
auto name = bts::api::orderman::TimeInForce_Name(tif);
if (name.starts_with("TIME_IN_FORCE_")) {
return name.substr(strlen("TIME_IN_FORCE_"));
}
return name;
}
inline std::ostream &operator<<(std::ostream &out, TimeInForce tif) noexcept {
out << TimeInForce_Name(tif);
return out;
+35
View File
@@ -5,14 +5,49 @@
#include "spark/domain_model/Security.pb.h"
#include "spark/io/JsonReader.h"
#include "spark/logging/logging.h"
#include "spark/utils/string_utils.h"
#include <nlohmann/json.hpp>
namespace bts::api {
/**
* Parses Side from a string. Unlike `bts::api::Side_Parse`, this implementation is case
* insensitive, and makes the 'SIDE_' prefix optional.
*/
bool side_parse_smart(const std::string &str, Side *result);
std::string side_name_pretty(Side side);
PB_ENUM_FROM_JSON(Side)
PB_ENUM_TO_JSON(Side)
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline bool side_parse_smart(const std::string &str, Side *result) {
if (Side_Parse(str, result)) {
return true;
}
std::string s = bts::system::string_utils::copy_to_upper(str);
if (Side_Parse(s, result)) {
return true;
}
const char *prefix = "SIDE_";
if (!s.starts_with(prefix)) {
s.insert(0, prefix);
}
return Side_Parse(s, result);
}
inline std::string side_name_pretty(Side side) {
auto name = Side_Name(side);
if (name.starts_with("SIDE_")) {
return name.substr(strlen("SIDE_"));
}
return name;
}
} // namespace bts::api
FMT_IMPL(bts::api::Side, s, "{}", bts::api::Side_Name(s));
+9
View File
@@ -16,6 +16,7 @@ namespace bts::api {
* insensitive, and makes the 'VENUE_' prefix optional.
*/
bool venue_parse_smart(const std::string &str, Venue *result);
std::string venue_name_pretty(Venue venue);
PB_ENUM_FROM_JSON(Venue)
PB_ENUM_TO_JSON(Venue)
@@ -41,6 +42,14 @@ inline bool venue_parse_smart(const std::string &str, Venue *result) {
return Venue_Parse(s, result);
}
inline std::string venue_name_pretty(Venue venue) {
auto name = Venue_Name(venue);
if (name.starts_with("VENUE_")) {
return name.substr(strlen("VENUE_"));
}
return name;
}
inline std::ostream &operator<<(std::ostream &out, Venue venue) noexcept {
out << Venue_Name(venue);
return out;