• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

apiaryio/drafter: API Blueprint Parser (C++)

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

apiaryio/drafter

开源软件地址:

https://github.com/apiaryio/drafter

开源编程语言:

C++ 61.8%

开源软件介绍:

logo

Drafter Circle CI Build status

Snowcrash parser harness

API Blueprint Parser

Drafter is complex builder of API Blueprint. Internally it uses Snowcrash library, reference API Blueprint parser.

API Blueprint is Web API documentation language. You can find API Blueprint documentation on the API Blueprint site.

Drafter also provides the user ability to select the type of the output. There are two possible values:

  • API Elements Parse Result: Parse Result is defined in API Elements according to Parse Result Namespace.
  • Normal AST Parse Result: Parse Result defined by the API Blueprint AST Parse Result. The AST is deprecated and only available in the Drafter command line tool.

By default, Drafter assumes the Refract Parse Result.

Both the types of Parse Results are available in two different serialization formats, YAML and JSON. YAML is the default for the CLI.

Status

Install

OS X using Homebrew:

$ brew install drafter

AUR package for Arch Linux.

Other systems refer to installation notes.

Usage

Drafter is both a library and a command line tool.

Command line tool

The command line tool allows you to parse a blueprint and/or check the validity of a blueprint.

$ cat << 'EOF' > blueprint.apib
# My API
## GET /message
+ Response 200 (text/plain)

        Hello World!
EOF

$ drafter blueprint.apib
element: "parseResult"
content:
  -
    element: "category"
    meta:
      classes:
        - "api"
      title: "My API"
...

See parse feature for the details on using the drafter command line tool.

C/C++ API

#include <drafter/drafter.h>

The header itself is annotated with comments. C API unit tests provide more examples.

Parse API Blueprint into API Elements

The drafter_parse_blueprint_to function translates a buffered API blueprint into API Elements, serialized into one of its supported serialization formats.

drafter_error drafter_parse_blueprint_to(
    const char* source,
    char** out,
    const drafter_parse_options* parse_opts,
    const drafter_serialize_options* serialize_opts);
);

Given a pointer to a UTF-8 encoded c-string,

const char* blueprint =
  "# My API\n"
  "## GET /message\n"
  "+ Response 200 (text/plain)\n"
  "\n"
  "      Hello World!\n";
Serialized as YAML

Without options, the resulting API Elements is serialized as YAML.

char* yamlApie = NULL;
if (DRAFTER_OK == drafter_parse_blueprint_to(blueprint, &yamlApie, NULL, NULL)) {
    printf("%s\n", yamlApie);
}

free(yamlApie);
Serialized as JSON

Tweaking drafter_serialize_options allows serialization into JSON.

drafter_serialize_options* serialize_options = drafter_init_serialize_options();
drafter_set_format(serialize_options, DRAFTER_SERIALIZE_JSON);

char* jsonApie = NULL;
if (DRAFTER_OK == drafter_parse_blueprint_to(blueprint, &jsonApie, NULL, serialize_options)) {
    printf("%s\n", jsonApie);
}

free(jsonApie);
drafter_free_serialize_options(serialize_options);

Validate API Blueprint

API Blueprint can be validated via drafter_check_blueprint.

drafter_error drafter_check_blueprint(
    const char* source,
    drafter_result** res,
    const drafter_parse_options* parse_opts);
Simple validation

The return value of drafter_check_blueprint indicates validation success.

drafter_result* result = NULL;
if (DRAFTER_OK == drafter_check_blueprint(blueprint, result)) {
    printf("Understood.\n");
}

drafter_free_result(result);
Access warnings and errors

After running drafter_check_blueprint, the result parameter is set to reference an API Element containing validation warnings or errors.

Because the result is an API Element - drafter_result - it can be serialized as such.

drafter_result* result = NULL;
drafter_check_blueprint(blueprint, result);

if(result) {
    char* yamlApie = drafter_serialize(result, NULL);
    printf("%s\n", yamlApie);
    free(yamlApie);
}

drafter_free_result(result);

Serialization of API Elements as JSON is achieved by tweaking drafter_serialize_options as discussed here.

Installation

Building Drafter will require a modern C++ compiler and CMake. The following compilers are tested and known to work:

Compiler Minimum Version
Clang 4.0
GCC 5.3
MSVC++ 2015

The following steps can be used to build and install Drafter:

  1. Download a stable release of Drafter (release tarballs can be found in GitHub Releases):

    $ curl -OL <url to drafter release from GitHub releases>
    $ tar xvf drafter.tar.gz
    $ cd drafter

    Alternatively, you can clone the source repository, for example:

    $ git clone --recursive https://github.com/apiaryio/drafter.git
    $ cd drafter
  2. Build & Install Drafter:

    POSIX (macOS/Linux):

    $ mkdir build
    $ cd build
    $ cmake ..
    $ make
    $ [sudo] make install

    NOTE: You can use cmake -DCMAKE_INSTALL_PREFIX="$HOME/.local .. if you don't want a system wide install.

    Windows:

    > mkdir build
    > cd build
    > cmake ..
    > cmake --build . --target drafter --config Release

    On Windows, drafter.exe can be found inside src\Release

  3. You can now use Drafter CLI and library:

    $ drafter --help

Bindings

Drafter bindings in other languages:

CLI Wrapper

Contribute

Fork & Pull Request

If you want to create a binding for Drafter please refer to the Writing a Binding article.

License

MIT License. See the LICENSE file.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap