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

Add "printf" support to Encoder #237

Merged
merged 8 commits into from
Oct 7, 2024
Merged

Conversation

snej
Copy link
Contributor

@snej snej commented Aug 19, 2024

C API additions:

    /** Writes an Array or Dict to the encoder, as per the format string and printf-style arguments.
        For details, see documentation of \ref FLValue_NewWithFormat. */
    FLEECE_PUBLIC bool FLEncoder_WriteFormatted(FLEncoder, const char* format, ...) FLAPI __printflike(2, 3);

    /** Writes an Array or Dict to the encoder, as per the format string and `va_list`.
        For details, see documentation of \ref FLValue_NewWithFormat. */
    FLEECE_PUBLIC bool FLEncoder_WriteFormattedArgs(FLEncoder, const char* format, va_list args) FLAPI;

C++ API additions:

class Encoder {
    ...
        inline bool writeFormatted(const char* format, ...) __printflike(2, 3);
        inline bool writeFormattedArgs(const char* format, va_list args);

FYI, this is a dependency of the LiteCore changes-feed PR (couchbase/couchbase-lite-core#2117)

@snej snej force-pushed the feature/encoder_with_printf branch 5 times, most recently from 41578f5 to a74d037 Compare August 23, 2024 22:51
@snej snej requested review from pasin and borrrden August 27, 2024 20:07

/** Variant of \ref Build that writes to an Encoder and takes a pre-existing `va_list`. */
void VEncode(Encoder&, slice format, va_list args);
void VEncode(JSONEncoder&, slice format, va_list args);
Copy link
Member

Choose a reason for hiding this comment

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

I hope these already exist because I can't find their definition in this PR.

Use a std::variant to hold the normal and JSON encoders.
Move implementation of FLEncoder_* methods to a separate source file.
The JSONEncoder doesn't validate it's creating correct output, but I
added a simple check for invalid nesting. It doesn't distinguish
between arrays and dicts so it won't catch `[ ... }`, but it
doesn't seem worth it to add a stack that tracks which types of
collections are open.
Simple wrapper methods that make the nesting a lot clearer when
writing a collection:

```
enc.writeArray([]{
    enc.write(1234);
    enc.write(5678);
});
```
Stop using nonstandard `__typeof`; replace with `decltype`
@snej snej force-pushed the feature/encoder_with_printf branch from 6434012 to c0c22d3 Compare September 11, 2024 18:10
@jianminzhao jianminzhao merged commit aea5c0c into master Oct 7, 2024
3 checks passed
@jianminzhao jianminzhao deleted the feature/encoder_with_printf branch October 7, 2024 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants