common/compiler: Extract combined JSON parsing from Solidity.run

We'll be reusing it when we read combined JSON from stdin rather than a
direct solc run.
This commit is contained in:
Antonio Salazar Cardozo 2018-05-04 11:09:57 -04:00
parent 5c75423e3c
commit fcca429926
No known key found for this signature in database
GPG key ID: AFF226F4A6B64797

View file

@ -148,8 +148,22 @@ func (s *Solidity) run(cmd *exec.Cmd, source string) (map[string]*Contract, erro
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("solc: %v\n%s", err, stderr.Bytes()) return nil, fmt.Errorf("solc: %v\n%s", err, stderr.Bytes())
} }
return ParseCombinedJSON(stdout.Bytes(), source, s.Version, s.Version, strings.Join(s.makeArgs(), " "))
}
// ParseCombinedJSON takes the direct output of a solc --combined-output run and
// parses it into a map of string contract name to Contract structs. The
// provided source, language and compiler version, and compiler options are all
// passed through into the Contract structs.
//
// The solc output is expected to contain ABI, user docs, and dev docs.
//
// Returns an error if the JSON is malformed or missing data, or if the JSON
// embedded within the JSON is malformed.
func ParseCombinedJSON(combinedJSON []byte, source string, languageVersion string, compilerVersion string, compilerOptions string) (map[string]*Contract, error) {
var output solcOutput var output solcOutput
if err := json.Unmarshal(stdout.Bytes(), &output); err != nil { if err := json.Unmarshal(combinedJSON, &output); err != nil {
return nil, err return nil, err
} }
@ -174,9 +188,9 @@ func (s *Solidity) run(cmd *exec.Cmd, source string) (map[string]*Contract, erro
Info: ContractInfo{ Info: ContractInfo{
Source: source, Source: source,
Language: "Solidity", Language: "Solidity",
LanguageVersion: s.Version, LanguageVersion: languageVersion,
CompilerVersion: s.Version, CompilerVersion: compilerVersion,
CompilerOptions: strings.Join(s.makeArgs(), " "), CompilerOptions: compilerOptions,
AbiDefinition: abi, AbiDefinition: abi,
UserDoc: userdoc, UserDoc: userdoc,
DeveloperDoc: devdoc, DeveloperDoc: devdoc,