ドキュメント

View Categories

Formatクラス

2 min read

FormatクラスはJournaliteのデータ操作を定義するクラスです。Journaliteではデータベースから取得したデータを格納し、クライアントで操作したデータを決められた型で送受信する必要があります。

Formatクラスはstd::tuple型、Json::Value型の両方でデータを利用できるように設計しています。

Formatクラスではstd::tuple型、Json::Value型を引数とするコンストラクタを定義します。

Json::Value型で初期化する場合にバリデーションチェックを行います。Json::Value型でクライアントサーバー間の通信データの整合性を保ちます。

namespace spa::format {

  struct AccountItem
  {
    // id, year, code, name
    using Columns = 
      typename std::tuple<
        unsigned int,
        unsigned int, 
        unsigned int, 
        std::string, 
        unsigned int, 
        std::string>;
    Columns record;
    unsigned int id;
    unsigned int year;
    unsigned int code;
    std::string name;
    unsigned int category;
    std::string color;
    Json::Value json;

    AccountItem() : AccountItem(Columns{}) {}

    AccountItem(Columns record) : record{ record };

    AccountItem(Json::Value json) : json{ json };

    bool validate(unsigned int id, 
      unsigned int year,
      unsigned int code, 
      std::string name, 
      unsigned int category,
      std::string color);

    bool validate(Json::Value json);
  };

}

namespace spa::format::account_item::filter {

  using DATA = std::list<AccountItem>;
  using DecorationFc = std::function<DATA(DATA)>;
  using Specific = std::pair<std::string, DecorationFc>;

  inline Specific year(unsigned int year);

  inline Specific code(unsigned int code);

  inline Specific name(std::string name);

  inline Specific category(spa::format::FinancialCategory category);

  inline Specific color(std::string color);

}


バリデーションのほかにフィルター関数を定義します。データの抽出は様々な場面で使用します。そのため汎用的に組み合わせて必要なデータへ整形できるようにデコレーションパターンを定義します。

DecorationFc decoration_fc = spesific(…).secondはstd::list<Format>を引数にstd::list<Format>を返します。

引数と戻り値の型が同じなので組み合わせて必要なデータを抽出します。

これらをFormatクラスに定義し、実装クラスで利用することでデータ操作の共通化を図っています。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です