-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from LinusU/sync-api
added sync api
- Loading branch information
Showing
10 changed files
with
193 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "protagonist.h" | ||
#include "snowcrash.h" | ||
|
||
using namespace v8; | ||
using namespace protagonist; | ||
|
||
//static const std::string RenderDescriptionsOptionKey = "renderDescriptions"; | ||
static const std::string RequireBlueprintNameOptionKey = "requireBlueprintName"; | ||
static const std::string ExportSourcemapOptionKey = "exportSourcemap"; | ||
|
||
OptionsResult* protagonist::ParseOptionsObject(Handle<Object> optionsObject) { | ||
OptionsResult *optionsResult = (OptionsResult *) malloc(sizeof(OptionsResult)); | ||
|
||
optionsResult->options = 0; | ||
optionsResult->error = NULL; | ||
|
||
const Local<Array> properties = optionsObject->GetPropertyNames(); | ||
const uint32_t length = properties->Length(); | ||
|
||
for (uint32_t i = 0 ; i < length ; ++i) { | ||
const Local<Value> key = properties->Get(i); | ||
const Local<Value> value = optionsObject->Get(key); | ||
|
||
if (RequireBlueprintNameOptionKey == *String::Utf8Value(key)) { | ||
// RequireBlueprintNameOption | ||
if (value->IsTrue()) | ||
optionsResult->options |= snowcrash::RequireBlueprintNameOption; | ||
else | ||
optionsResult->options &= snowcrash::RequireBlueprintNameOption; | ||
} | ||
else if (ExportSourcemapOptionKey == *String::Utf8Value(key)) { | ||
// ExportSourcemapOption | ||
if (value->IsTrue()) | ||
optionsResult->options |= snowcrash::ExportSourcemapOption; | ||
else | ||
optionsResult->options &= snowcrash::ExportSourcemapOption; | ||
} | ||
else { | ||
// Unrecognized option | ||
std::stringstream ss; | ||
ss << "unrecognized option '" << *String::Utf8Value(key) << "', expected: "; | ||
ss << "'" << RequireBlueprintNameOptionKey << "' or '" << ExportSourcemapOptionKey << "'"; | ||
|
||
optionsResult->error = ss.str().c_str(); | ||
return optionsResult; | ||
} | ||
} | ||
|
||
return optionsResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <string> | ||
#include <sstream> | ||
#include "protagonist.h" | ||
#include "snowcrash.h" | ||
#include "drafter.h" | ||
|
||
using std::string; | ||
using namespace v8; | ||
using namespace protagonist; | ||
|
||
NAN_METHOD(protagonist::ParseSync) { | ||
NanScope(); | ||
|
||
// Check arguments | ||
if (args.Length() != 1 && args.Length() != 2) { | ||
NanThrowTypeError("wrong number of arguments, `parseSync(string, options)` expected"); | ||
NanReturnUndefined(); | ||
} | ||
|
||
if (!args[0]->IsString()) { | ||
NanThrowTypeError("wrong argument - string expected, `parseSync(string, options)`"); | ||
NanReturnUndefined(); | ||
} | ||
|
||
if (args.Length() == 2 && !args[1]->IsObject()) { | ||
NanThrowTypeError("wrong argument - object expected, `parseSync(string, options)`"); | ||
NanReturnUndefined(); | ||
} | ||
|
||
// Get source data | ||
String::Utf8Value sourceData(args[0]->ToString()); | ||
|
||
// Prepare options | ||
snowcrash::BlueprintParserOptions options = 0; | ||
|
||
if (args.Length() == 2) { | ||
OptionsResult *optionsResult = ParseOptionsObject(Handle<Object>::Cast(args[1])); | ||
|
||
if (optionsResult->error != NULL) { | ||
NanThrowTypeError(optionsResult->error); | ||
NanReturnUndefined(); | ||
} | ||
|
||
options = optionsResult->options; | ||
free(optionsResult); | ||
} | ||
|
||
// Parse the source data | ||
snowcrash::ParseResult<snowcrash::Blueprint> parseResult; | ||
drafter::ParseBlueprint(*sourceData, options, parseResult); | ||
|
||
if (parseResult.report.error.code != snowcrash::Error::OK) { | ||
NanThrowError(SourceAnnotation::WrapSourceAnnotation(parseResult.report.error)); | ||
NanReturnUndefined(); | ||
} | ||
|
||
NanReturnValue(Result::WrapResult(parseResult.report, parseResult.node, parseResult.sourceMap, options)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
fs = require 'fs' | ||
path = require 'path' | ||
{assert} = require 'chai' | ||
protagonist = require '../build/Release/protagonist' | ||
|
||
describe "Parser AST - Sync", -> | ||
|
||
ast_fixture = require './fixtures/sample-api-ast.json' | ||
ast_parsed = null | ||
|
||
# Read & parse blueprint fixture | ||
before (done) -> | ||
|
||
fixture_path = path.join __dirname, './fixtures/sample-api.apib' | ||
|
||
fs.readFile fixture_path, 'utf8', (err, data) -> | ||
return done err if err | ||
|
||
ast_parsed = protagonist.parseSync(data).ast | ||
done() | ||
|
||
# Parser AST should conform to AST serialization JSON media type | ||
it '`ast` field conforms to `vnd.apiblueprint.ast.raw+json; version=3.0`', -> | ||
assert.deepEqual ast_parsed, ast_fixture |