glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL
https://app.quicktype.io
Apache License 2.0
11.76k stars 1.04k forks source link

[FEATURE]: C++: add equality operators to classes #2585

Open beku-epitome opened 2 months ago

beku-epitome commented 2 months ago

Currently the C++ output of quicktype is asymmetrical: boolean, integer, string and array[^1] types are comparable for equality, but the classes generated for JSON objects aren't. Equality comparison is important to detect if a message's contents are different from a previous one.

[^1]: if the contained type is also comparable

Context (Input, Language)

Output Language: C++

Current Behaviour / Output

    struct Payload {
        std::optional<bool> aFlag;
        std::vector<int64_t> indices;
        std::string description;
    };

Proposed Behaviour / Output

Add equality operators to each class (or struct, depending on the configuration), the equality should simply compare each member and perform logical AND on their results:

    struct Payload {
        std::optional<bool> flag;
        std::vector<int64_t> indices;
        std::string name;

        bool operator==(const Payload & other) const {
            return (flag == other.flag) && (indices == other.indices) && (name == other.name);
        }
        bool operator!=(const Payload & other) const {
            return !(*this == other);
        }
    };