Move InterfaceHandler from string to JSON

This commit is contained in:
Alex Beregszaszi 2016-11-15 01:04:00 +00:00
parent 0072160d77
commit 9719cf38e6
8 changed files with 71 additions and 48 deletions

View File

@ -160,22 +160,22 @@ vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::inter
return *m_interfaceFunctionList; return *m_interfaceFunctionList;
} }
string const& ContractDefinition::devDocumentation() const Json::Value const& ContractDefinition::devDocumentation() const
{ {
return m_devDocumentation; return m_devDocumentation;
} }
string const& ContractDefinition::userDocumentation() const Json::Value const& ContractDefinition::userDocumentation() const
{ {
return m_userDocumentation; return m_userDocumentation;
} }
void ContractDefinition::setDevDocumentation(string const& _devDocumentation) void ContractDefinition::setDevDocumentation(Json::Value const& _devDocumentation)
{ {
m_devDocumentation = _devDocumentation; m_devDocumentation = _devDocumentation;
} }
void ContractDefinition::setUserDocumentation(string const& _userDocumentation) void ContractDefinition::setUserDocumentation(Json::Value const& _userDocumentation)
{ {
m_userDocumentation = _userDocumentation; m_userDocumentation = _userDocumentation;
} }

View File

@ -35,6 +35,7 @@
#include <libsolidity/ast/Types.h> #include <libsolidity/ast/Types.h>
#include <libsolidity/interface/Exceptions.h> #include <libsolidity/interface/Exceptions.h>
#include <libsolidity/ast/ASTAnnotations.h> #include <libsolidity/ast/ASTAnnotations.h>
#include <json/json.h>
namespace dev namespace dev
{ {
@ -343,11 +344,11 @@ public:
/// Returns the fallback function or nullptr if no fallback function was specified. /// Returns the fallback function or nullptr if no fallback function was specified.
FunctionDefinition const* fallbackFunction() const; FunctionDefinition const* fallbackFunction() const;
std::string const& userDocumentation() const; Json::Value const& userDocumentation() const;
void setUserDocumentation(std::string const& _userDocumentation); void setUserDocumentation(Json::Value const& _userDocumentation);
std::string const& devDocumentation() const; Json::Value const& devDocumentation() const;
void setDevDocumentation(std::string const& _devDocumentation); void setDevDocumentation(Json::Value const& _devDocumentation);
virtual TypePointer type() const override; virtual TypePointer type() const override;
@ -359,8 +360,8 @@ private:
bool m_isLibrary; bool m_isLibrary;
// parsed Natspec documentation of the contract. // parsed Natspec documentation of the contract.
std::string m_userDocumentation; Json::Value m_userDocumentation;
std::string m_devDocumentation; Json::Value m_devDocumentation;
std::vector<ContractDefinition const*> m_linearizedBaseContracts; std::vector<ContractDefinition const*> m_linearizedBaseContracts;
mutable std::unique_ptr<std::vector<std::pair<FixedHash<4>, FunctionTypePointer>>> m_interfaceFunctionList; mutable std::unique_ptr<std::vector<std::pair<FixedHash<4>, FunctionTypePointer>>> m_interfaceFunctionList;

View File

@ -345,17 +345,17 @@ map<string, unsigned> CompilerStack::sourceIndices() const
return indices; return indices;
} }
string const& CompilerStack::interface(string const& _contractName) const Json::Value const& CompilerStack::interface(string const& _contractName) const
{ {
return metadata(_contractName, DocumentationType::ABIInterface); return metadata(_contractName, DocumentationType::ABIInterface);
} }
string const& CompilerStack::metadata(string const& _contractName, DocumentationType _type) const Json::Value const& CompilerStack::metadata(string const& _contractName, DocumentationType _type) const
{ {
if (!m_parseSuccessful) if (!m_parseSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
std::unique_ptr<string const>* doc; std::unique_ptr<Json::Value const>* doc;
Contract const& currentContract = contract(_contractName); Contract const& currentContract = contract(_contractName);
// checks wheather we already have the documentation // checks wheather we already have the documentation
@ -376,7 +376,7 @@ string const& CompilerStack::metadata(string const& _contractName, Documentation
// caches the result // caches the result
if (!*doc) if (!*doc)
doc->reset(new string(InterfaceHandler::documentation(*currentContract.contract, _type))); doc->reset(new Json::Value(InterfaceHandler::documentation(*currentContract.contract, _type)));
return *(*doc); return *(*doc);
} }

View File

@ -162,14 +162,14 @@ public:
/// @returns a mapping assigning each source name its index inside the vector returned /// @returns a mapping assigning each source name its index inside the vector returned
/// by sourceNames(). /// by sourceNames().
std::map<std::string, unsigned> sourceIndices() const; std::map<std::string, unsigned> sourceIndices() const;
/// @returns a string representing the contract interface in JSON. /// @returns a JSON representing the contract interface.
/// Prerequisite: Successful call to parse or compile. /// Prerequisite: Successful call to parse or compile.
std::string const& interface(std::string const& _contractName = "") const; Json::Value const& interface(std::string const& _contractName = "") const;
/// @returns a string representing the contract's documentation in JSON. /// @returns a JSON representing the contract's documentation.
/// Prerequisite: Successful call to parse or compile. /// Prerequisite: Successful call to parse or compile.
/// @param type The type of the documentation to get. /// @param type The type of the documentation to get.
/// Can be one of 4 types defined at @c DocumentationType /// Can be one of 4 types defined at @c DocumentationType
std::string const& metadata(std::string const& _contractName, DocumentationType _type) const; Json::Value const& metadata(std::string const& _contractName, DocumentationType _type) const;
/// @returns the previously used scanner, useful for counting lines during error reporting. /// @returns the previously used scanner, useful for counting lines during error reporting.
Scanner const& scanner(std::string const& _sourceName = "") const; Scanner const& scanner(std::string const& _sourceName = "") const;
@ -213,9 +213,9 @@ private:
eth::LinkerObject object; eth::LinkerObject object;
eth::LinkerObject runtimeObject; eth::LinkerObject runtimeObject;
eth::LinkerObject cloneObject; eth::LinkerObject cloneObject;
mutable std::unique_ptr<std::string const> interface; mutable std::unique_ptr<Json::Value const> interface;
mutable std::unique_ptr<std::string const> userDocumentation; mutable std::unique_ptr<Json::Value const> userDocumentation;
mutable std::unique_ptr<std::string const> devDocumentation; mutable std::unique_ptr<Json::Value const> devDocumentation;
mutable std::unique_ptr<std::string const> sourceMapping; mutable std::unique_ptr<std::string const> sourceMapping;
mutable std::unique_ptr<std::string const> runtimeSourceMapping; mutable std::unique_ptr<std::string const> runtimeSourceMapping;
}; };

View File

@ -8,7 +8,7 @@ using namespace std;
using namespace dev; using namespace dev;
using namespace dev::solidity; using namespace dev::solidity;
string InterfaceHandler::documentation( Json::Value InterfaceHandler::documentation(
ContractDefinition const& _contractDef, ContractDefinition const& _contractDef,
DocumentationType _type DocumentationType _type
) )
@ -24,10 +24,9 @@ string InterfaceHandler::documentation(
} }
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation type")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation type"));
return "";
} }
string InterfaceHandler::abiInterface(ContractDefinition const& _contractDef) Json::Value InterfaceHandler::abiInterface(ContractDefinition const& _contractDef)
{ {
Json::Value abi(Json::arrayValue); Json::Value abi(Json::arrayValue);
@ -104,12 +103,10 @@ string InterfaceHandler::abiInterface(ContractDefinition const& _contractDef)
abi.append(event); abi.append(event);
} }
Json::FastWriter writer; return abi;
writer.omitEndingLineFeed();
return writer.write(abi);
} }
string InterfaceHandler::userDocumentation(ContractDefinition const& _contractDef) Json::Value InterfaceHandler::userDocumentation(ContractDefinition const& _contractDef)
{ {
Json::Value doc; Json::Value doc;
Json::Value methods(Json::objectValue); Json::Value methods(Json::objectValue);
@ -129,10 +126,10 @@ string InterfaceHandler::userDocumentation(ContractDefinition const& _contractDe
} }
doc["methods"] = methods; doc["methods"] = methods;
return Json::StyledWriter().write(doc); return doc;
} }
string InterfaceHandler::devDocumentation(ContractDefinition const& _contractDef) Json::Value InterfaceHandler::devDocumentation(ContractDefinition const& _contractDef)
{ {
Json::Value doc; Json::Value doc;
Json::Value methods(Json::objectValue); Json::Value methods(Json::objectValue);
@ -178,7 +175,7 @@ string InterfaceHandler::devDocumentation(ContractDefinition const& _contractDef
} }
doc["methods"] = methods; doc["methods"] = methods;
return Json::StyledWriter().write(doc); return doc;
} }
string InterfaceHandler::extractDoc(multimap<string, DocTag> const& _tags, string const& _name) string InterfaceHandler::extractDoc(multimap<string, DocTag> const& _tags, string const& _name)

View File

@ -64,24 +64,24 @@ public:
/// @param _contractDef The contract definition /// @param _contractDef The contract definition
/// @param _type The type of the documentation. Can be one of the /// @param _type The type of the documentation. Can be one of the
/// types provided by @c DocumentationType /// types provided by @c DocumentationType
/// @return A string with the json representation of provided type /// @return A JSON representation of provided type
static std::string documentation( static Json::Value documentation(
ContractDefinition const& _contractDef, ContractDefinition const& _contractDef,
DocumentationType _type DocumentationType _type
); );
/// Get the ABI Interface of the contract /// Get the ABI Interface of the contract
/// @param _contractDef The contract definition /// @param _contractDef The contract definition
/// @return A string with the json representation of the contract's ABI Interface /// @return A JSONrepresentation of the contract's ABI Interface
static std::string abiInterface(ContractDefinition const& _contractDef); static Json::Value abiInterface(ContractDefinition const& _contractDef);
/// Get the User documentation of the contract /// Get the User documentation of the contract
/// @param _contractDef The contract definition /// @param _contractDef The contract definition
/// @return A string with the json representation of the contract's user documentation /// @return A JSON representation of the contract's user documentation
static std::string userDocumentation(ContractDefinition const& _contractDef); static Json::Value userDocumentation(ContractDefinition const& _contractDef);
/// Genereates the Developer's documentation of the contract /// Genereates the Developer's documentation of the contract
/// @param _contractDef The contract definition /// @param _contractDef The contract definition
/// @return A string with the json representation /// @return A JSON representation
/// of the contract's developer documentation /// of the contract's developer documentation
static std::string devDocumentation(ContractDefinition const& _contractDef); static Json::Value devDocumentation(ContractDefinition const& _contractDef);
private: private:
/// @returns concatenation of all content under the given tag name. /// @returns concatenation of all content under the given tag name.

View File

@ -107,6 +107,18 @@ static void version()
exit(0); exit(0);
} }
string jsonPrettyPrint(Json::Value const& input)
{
return Json::StyledWriter().write(input);
}
string jsonCompactPrint(Json::Value const& input)
{
Json::FastWriter writer;
writer.omitEndingLineFeed();
return writer.write(input);
}
static bool needsHumanTargetedStdout(po::variables_map const& _args) static bool needsHumanTargetedStdout(po::variables_map const& _args)
{ {
if (_args.count(g_argGas)) if (_args.count(g_argGas))
@ -230,12 +242,18 @@ void CommandLineInterface::handleMeta(DocumentationType _type, string const& _co
if (m_args.count(argName)) if (m_args.count(argName))
{ {
std::string output;
if (_type == DocumentationType::ABIInterface)
output = jsonCompactPrint(m_compiler->metadata(_contract, _type));
else
output = jsonPrettyPrint(m_compiler->metadata(_contract, _type));
if (m_args.count("output-dir")) if (m_args.count("output-dir"))
createFile(_contract + suffix, m_compiler->metadata(_contract, _type)); createFile(_contract + suffix, output);
else else
{ {
cout << title << endl; cout << title << endl;
cout << m_compiler->metadata(_contract, _type) << endl; cout << output << endl;
} }
} }
@ -651,7 +669,7 @@ void CommandLineInterface::handleCombinedJSON()
{ {
Json::Value contractData(Json::objectValue); Json::Value contractData(Json::objectValue);
if (requests.count("abi")) if (requests.count("abi"))
contractData["abi"] = m_compiler->interface(contractName); contractData["abi"] = jsonCompactPrint(m_compiler->interface(contractName));
if (requests.count("bin")) if (requests.count("bin"))
contractData["bin"] = m_compiler->object(contractName).toHex(); contractData["bin"] = m_compiler->object(contractName).toHex();
if (requests.count("bin-runtime")) if (requests.count("bin-runtime"))
@ -676,9 +694,9 @@ void CommandLineInterface::handleCombinedJSON()
contractData["srcmap-runtime"] = map ? *map : ""; contractData["srcmap-runtime"] = map ? *map : "";
} }
if (requests.count("devdoc")) if (requests.count("devdoc"))
contractData["devdoc"] = m_compiler->metadata(contractName, DocumentationType::NatspecDev); contractData["devdoc"] = jsonCompactPrint(m_compiler->metadata(contractName, DocumentationType::NatspecDev));
if (requests.count("userdoc")) if (requests.count("userdoc"))
contractData["userdoc"] = m_compiler->metadata(contractName, DocumentationType::NatspecUser); contractData["userdoc"] = jsonCompactPrint(m_compiler->metadata(contractName, DocumentationType::NatspecUser));
output["contracts"][contractName] = contractData; output["contracts"][contractName] = contractData;
} }
@ -702,7 +720,7 @@ void CommandLineInterface::handleCombinedJSON()
output["sources"][sourceCode.first]["AST"] = converter.json(); output["sources"][sourceCode.first]["AST"] = converter.json();
} }
} }
cout << Json::FastWriter().write(output) << endl; cout << jsonCompactPrint(output) << endl;
} }
void CommandLineInterface::handleAst(string const& _argStr) void CommandLineInterface::handleAst(string const& _argStr)

View File

@ -125,6 +125,13 @@ Json::Value estimateGas(CompilerStack const& _compiler, string const& _contract)
return gasEstimates; return gasEstimates;
} }
string jsonCompactPrint(Json::Value const& input)
{
Json::FastWriter writer;
writer.omitEndingLineFeed();
return writer.write(input);
}
string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback _readCallback) string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback _readCallback)
{ {
Json::Value output(Json::objectValue); Json::Value output(Json::objectValue);
@ -213,7 +220,7 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback
for (string const& contractName: compiler.contractNames()) for (string const& contractName: compiler.contractNames())
{ {
Json::Value contractData(Json::objectValue); Json::Value contractData(Json::objectValue);
contractData["interface"] = compiler.interface(contractName); contractData["interface"] = jsonCompactPrint(compiler.interface(contractName));
contractData["bytecode"] = compiler.object(contractName).toHex(); contractData["bytecode"] = compiler.object(contractName).toHex();
contractData["runtimeBytecode"] = compiler.runtimeObject(contractName).toHex(); contractData["runtimeBytecode"] = compiler.runtimeObject(contractName).toHex();
contractData["opcodes"] = solidity::disassemble(compiler.object(contractName).bytecode); contractData["opcodes"] = solidity::disassemble(compiler.object(contractName).bytecode);
@ -274,7 +281,7 @@ string compile(StringMap const& _sources, bool _optimize, CStyleReadFileCallback
try try
{ {
return Json::FastWriter().write(output); return jsonCompactPrint(output);
} }
catch (...) catch (...)
{ {
@ -292,7 +299,7 @@ string compileMulti(string const& _input, bool _optimize, CStyleReadFileCallback
errors.append("Error parsing input JSON: " + reader.getFormattedErrorMessages()); errors.append("Error parsing input JSON: " + reader.getFormattedErrorMessages());
Json::Value output(Json::objectValue); Json::Value output(Json::objectValue);
output["errors"] = errors; output["errors"] = errors;
return Json::FastWriter().write(output); return jsonCompactPrint(output);
} }
else else
{ {