Refactor parsing JSON-encoded cquery results.

Test: treehugger
Change-Id: I8f1157ad4c3f0e563c9722bb6a1f0fc03e795700
diff --git a/bazel/cquery/request_type.go b/bazel/cquery/request_type.go
index fcc2f07..fa73fb2 100644
--- a/bazel/cquery/request_type.go
+++ b/bazel/cquery/request_type.go
@@ -186,13 +186,8 @@
 // Starlark given in StarlarkFunctionBody.
 func (g getCcInfoType) ParseResult(rawString string) (CcInfo, error) {
 	var ccInfo CcInfo
-	decoder := json.NewDecoder(strings.NewReader(rawString))
-	decoder.DisallowUnknownFields() //useful to detect typos, e.g. in unit tests
-	err := decoder.Decode(&ccInfo)
-	if err != nil {
-		return ccInfo, fmt.Errorf("error parsing CcInfo result. %s RAW STRING: %s", err, rawString)
-	}
-	return ccInfo, err
+	parseJson(rawString, &ccInfo)
+	return ccInfo, nil
 }
 
 // Query Bazel for the artifacts generated by the apex modules.
@@ -237,11 +232,7 @@
 // Starlark given in StarlarkFunctionBody.
 func (g getApexInfoType) ParseResult(rawString string) ApexCqueryInfo {
 	var info ApexCqueryInfo
-	decoder := json.NewDecoder(strings.NewReader(rawString))
-	decoder.DisallowUnknownFields() //useful to detect typos, e.g. in unit tests
-	if err := decoder.Decode(&info); err != nil {
-		panic(fmt.Errorf("cannot parse cquery result '%s': %s", rawString, err))
-	}
+	parseJson(rawString, &info)
 	return info
 }
 
@@ -273,11 +264,7 @@
 // Starlark given in StarlarkFunctionBody.
 func (g getCcUnstippedInfoType) ParseResult(rawString string) CcUnstrippedInfo {
 	var info CcUnstrippedInfo
-	decoder := json.NewDecoder(strings.NewReader(rawString))
-	decoder.DisallowUnknownFields() //useful to detect typos, e.g. in unit tests
-	if err := decoder.Decode(&info); err != nil {
-		panic(fmt.Errorf("cannot parse cquery result '%s': %s", rawString, err))
-	}
+	parseJson(rawString, &info)
 	return info
 }
 
@@ -295,3 +282,13 @@
 		return strings.Split(s, sep)
 	}
 }
+
+// parseJson decodes json string into the fields of the receiver.
+// Unknown attribute name causes panic.
+func parseJson(jsonString string, info interface{}) {
+	decoder := json.NewDecoder(strings.NewReader(jsonString))
+	decoder.DisallowUnknownFields() //useful to detect typos, e.g. in unit tests
+	if err := decoder.Decode(info); err != nil {
+		panic(fmt.Errorf("cannot parse cquery result '%s': %s", jsonString, err))
+	}
+}