Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

C API options to allow skipping message body (schema) generation #781

Merged
merged 7 commits into from
Apr 6, 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
4 changes: 3 additions & 1 deletion drafter.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,9 @@
[ 'libdrafter_type=="static_library"', { 'defines' : [ 'DRAFTER_BUILD_STATIC' ] }],
],
"sources": [
"test/test-CAPI.c"
"test/test-CAPI.c",
"test/ctesting.h",
"test/ctesting.c"
],
"dependencies": [
"libdrafter",
Expand Down
8 changes: 7 additions & 1 deletion src/ConversionContext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

using namespace drafter;

ConversionContext::ConversionContext(const char* src, bool expandMson) noexcept
ConversionContext::ConversionContext(const char* src, const drafter_parse_options* opts, bool expandMson) noexcept
: newline_indices_(GetLinesEndIndex(src)),
expand_mson_{ expandMson },
options_{ opts },
registry_{},
warnings_{}
{
Expand Down Expand Up @@ -70,3 +71,8 @@ const ConversionContext::Warnings& ConversionContext::warnings() const noexcept
{
return warnings_;
}

const drafter_parse_options* ConversionContext::options() const noexcept
{
return options_;
}
10 changes: 9 additions & 1 deletion src/ConversionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "refract/Registry.h"
#include "SourceMapUtils.h"
#include "options.h"

namespace snowcrash
{
Expand All @@ -28,12 +29,17 @@ namespace drafter
private:
const NewLinesIndex newline_indices_;
const bool expand_mson_;
const drafter_parse_options* const options_;

refract::Registry registry_;
Warnings warnings_;

public:
explicit ConversionContext(const char*, bool expandMson = false) noexcept;
explicit ConversionContext( //
const char*,
const drafter_parse_options* opts = nullptr,
bool expandMson = false // TODO avoid, only used in unit tests
) noexcept;

const NewLinesIndex& newlineIndices() const noexcept;

Expand All @@ -44,6 +50,8 @@ namespace drafter

const Warnings& warnings() const noexcept;
void warn(const snowcrash::Warning& warning);

const drafter_parse_options* options() const noexcept;
};
}
#endif
20 changes: 11 additions & 9 deletions src/RefractAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,23 +395,25 @@ std::unique_ptr<IElement> PayloadToRefract( //
// Push Body Asset
if (!payload.node->body.empty()) {
content.push_back(make_asset_element( //
payload.node->body, //
SerializeKey::MessageBody, //
serialize(mediaType), //
payload.node->body,
SerializeKey::MessageBody,
serialize(mediaType),
&payload.sourceMap->body.sourceMap));
} else if (dataStructureExpanded) {

} else if (dataStructureExpanded && !is_skip_gen_bodies(context.options())) {
// otherwise, generate one from attributes
generateValueAsset(content, context, *dataStructureExpanded, mediaType);
}

// Push Schema Asset
if (!payload.node->schema.empty()) {
content.push_back(make_asset_element( //
payload.node->schema, //
SerializeKey::MessageBodySchema, //
serialize(IsAnyJSONContentType(mediaType) ? jsonSchemaType() : textPlainType()), //
content.push_back(make_asset_element( //
payload.node->schema,
SerializeKey::MessageBodySchema,
serialize(IsAnyJSONContentType(mediaType) ? jsonSchemaType() : textPlainType()),
&payload.sourceMap->schema.sourceMap));
} else if (dataStructureExpanded) {

} else if (dataStructureExpanded && !is_skip_gen_body_schemas(context.options())) {
// otherwise, generate one from attributes
generateSchemaAsset(content, context, *dataStructureExpanded, mediaType);
}
Expand Down
18 changes: 15 additions & 3 deletions src/drafter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ DRAFTER_API drafter_error drafter_parse_blueprint(
sc::ParseResult<sc::Blueprint> blueprint;
sc::parse(source, scOptions, blueprint);

drafter::ConversionContext context(source);
drafter::ConversionContext context(source, parse_opts);
auto result = WrapRefract(blueprint, context);

if (out) {
Expand Down Expand Up @@ -181,7 +181,19 @@ DRAFTER_API void drafter_free_parse_options(drafter_parse_options* opts)
DRAFTER_API void drafter_set_name_required(drafter_parse_options* opts)
{
assert(opts);
opts->requireBlueprintName = true;
opts->flags.set(drafter_parse_options::NAME_REQUIRED);
}

DRAFTER_API void drafter_set_skip_gen_bodies(drafter_parse_options* opts)
{
assert(opts);
opts->flags.set(drafter_parse_options::SKIP_GEN_BODIES);
}

DRAFTER_API void drafter_set_skip_gen_body_schemas(drafter_parse_options* opts)
{
assert(opts);
opts->flags.set(drafter_parse_options::SKIP_GEN_BODY_SCHEMAS);
}

DRAFTER_API drafter_serialize_options* drafter_init_serialize_options()
Expand All @@ -197,7 +209,7 @@ DRAFTER_API void drafter_free_serialize_options(drafter_serialize_options* opts)
DRAFTER_API void drafter_set_sourcemaps_included(drafter_serialize_options* opts)
{
assert(opts);
opts->sourcemap = true;
opts->flags.set(drafter_serialize_options::SOURCEMAPS_INCLUDED);
}

DRAFTER_API void drafter_set_format(drafter_serialize_options* opts, drafter_format fmt)
Expand Down
10 changes: 10 additions & 0 deletions src/drafter.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ DRAFTER_API void drafter_free_parse_options(drafter_parse_options*);
*/
DRAFTER_API void drafter_set_name_required(drafter_parse_options*);

/* Set skip_gen_bodies option
* @remark skip_gen_bodies: skip generating message body payloads
*/
DRAFTER_API void drafter_set_skip_gen_bodies(drafter_parse_options*);

/* Set skip_gen_body_schemas option
* @remark skip_gen_body_schemas: skip generating message body schema payloads
*/
DRAFTER_API void drafter_set_skip_gen_body_schemas(drafter_parse_options*);

/* Serialisation options
*/
typedef struct drafter_serialize_options drafter_serialize_options;
Expand Down
14 changes: 12 additions & 2 deletions src/options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@

bool drafter::is_name_required(const drafter_parse_options* opts) noexcept
{
return opts && opts->requireBlueprintName;
return opts && opts->flags.test(drafter_parse_options::NAME_REQUIRED);
}

bool drafter::are_sourcemaps_included(const drafter_serialize_options* opts) noexcept
{
return opts && opts->sourcemap;
return opts && opts->flags.test(drafter_serialize_options::SOURCEMAPS_INCLUDED);
}

drafter_format drafter::get_format(const drafter_serialize_options* opts) noexcept
{
return opts ? opts->format : DRAFTER_SERIALIZE_YAML;
}

bool drafter::is_skip_gen_bodies(const drafter_parse_options* opts) noexcept
{
return opts && opts->flags.test(drafter_parse_options::SKIP_GEN_BODIES);
}

bool drafter::is_skip_gen_body_schemas(const drafter_parse_options* opts) noexcept
{
return opts && opts->flags.test(drafter_parse_options::SKIP_GEN_BODY_SCHEMAS);
}
26 changes: 24 additions & 2 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@

#include "drafter.h"

#include <bitset>

struct drafter_parse_options {
bool requireBlueprintName = false;
using flags_type = std::bitset<3>;

static constexpr std::size_t NAME_REQUIRED = 0;
static constexpr std::size_t SKIP_GEN_BODIES = 1;
static constexpr std::size_t SKIP_GEN_BODY_SCHEMAS = 2;

flags_type flags = 0;
};

struct drafter_serialize_options {
bool sourcemap = false;
using flags_type = std::bitset<1>;

static constexpr std::size_t SOURCEMAPS_INCLUDED = 0;

flags_type flags = 0;
drafter_format format = DRAFTER_SERIALIZE_YAML;
};

Expand All @@ -32,6 +44,16 @@ namespace drafter
*/
bool are_sourcemaps_included(const drafter_serialize_options*) noexcept;

/* Access skip_gen_bodies option
* @remark skip_gen_bodies: skip generating message body payloads
*/
bool is_skip_gen_bodies(const drafter_parse_options*) noexcept;

/* Access skip_gen_body_schemas option
* @remark skip_gen_body_schemas: skip generating message body schema payloads
*/
bool is_skip_gen_body_schemas(const drafter_parse_options*) noexcept;

/* Access format option
* @remark format: API Elements serialisation format (YAML|JSON)
*/
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ file(

add_executable(drafter-ctest
test-CAPI.c
ctesting.c
)

target_link_libraries(drafter-ctest
Expand Down
55 changes: 55 additions & 0 deletions test/ctesting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "ctesting.h"

#include <string.h>
#include <stdio.h>

void drafter_ctest_fail(const char* expr, const char* file, size_t line)
{
fprintf(stderr, "Assertion `%s` failed at %s:%d\n", expr, file, line);
abort();
}

void drafter_ctest_assert(int a, const char* expr, const char* file, size_t line)
{
if (!a)
drafter_ctest_fail(expr, file, line);
}

void drafter_ctest_includes(const char* searched, const char* actual, const char* file, size_t line)
{
const char* where = strstr(actual, searched);
if (!where) {
fprintf( //
stderr,
"Didn't find sub string\n"
"\x1b[32m%s\x1b[0m\n"
"in\n"
"\x1b[31m%s\x1b[0m\n",
searched,
actual);
drafter_ctest_fail("expected sub string not found", file, line);
}
}

void drafter_ctest_excludes(const char* searched, const char* actual, const char* file, size_t line)
{
const char* where = strstr(actual, searched);
if (where) {
while (where != actual) {
if (where[0] == '\n') {
++where;
break;
}
--where;
}
fprintf( //
stderr,
"Found prohibited sub string\n"
"\x1b[32m%s\x1b[0m\n"
"at\n"
"\x1b[31m%s\x1b[0m\n",
searched,
where);
drafter_ctest_fail("prohibited sub string found", file, line);
}
}
18 changes: 18 additions & 0 deletions test/ctesting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef DRAFTER_CTESTING_H
#define DRAFTER_CTESTING_H

#include <stdlib.h>

#define FAIL(expr) drafter_ctest_fail(#expr, __FILE__, __LINE__)
void drafter_ctest_fail(const char* expr, const char* file, size_t line);

#define REQUIRE(expr) drafter_ctest_assert(expr != 0, #expr, __FILE__, __LINE__)
void drafter_ctest_assert(int a, const char* expr, const char* file, size_t line);

#define REQUIRE_INCLUDES(searched, actual) drafter_ctest_includes(searched, actual, __FILE__, __LINE__)
void drafter_ctest_includes(const char* expected, const char* actual, const char* file, size_t line);

#define REQUIRE_EXCLUDES(searched, actual) drafter_ctest_excludes(searched, actual, __FILE__, __LINE__)
void drafter_ctest_excludes(const char* expected, const char* actual, const char* file, size_t line);

#endif
2 changes: 1 addition & 1 deletion test/draftertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool draftertest::handleResultJSON(const std::string& fixturePath, test_options
int result = snowcrash::parse(source, snowcrash::ExportSourcemapOption, blueprint);

std::ostringstream outStream;
drafter::ConversionContext context(source.c_str(), testOpts.test(TEST_OPTION_EXPAND_MSON));
drafter::ConversionContext context(source.c_str(), nullptr, testOpts.test(TEST_OPTION_EXPAND_MSON));

if (auto parsed = WrapRefract(blueprint, context)) {
auto soValue = refract::serialize::renderSo(*parsed, testOpts.test(TEST_OPTION_SOURCEMAPS));
Expand Down
Loading