Please review my code

type FinalResult struct {
+	TbMissingIssues        int
+	FieldMissingIssues     int
+	ApiIssuesJson          int
+	FieldMissingIssuesJson int
+	JsonVsDbTyIssues       int
+	TotalIssues            int
+}
+
+var rst FinalResult
+
+func TestResult(result string) *FinalResult {
+
+	inputScan := bufio.NewScanner(strings.NewReader(result))
+
+	for inputScan.Scan() {
+
+		lineStr := inputScan.Text()
+
+		switch {
+
+		case strings.Contains(lineStr, "Tables Missing"):
+
+			rst.TbMissingIssues += cntResult(lineStr)
+
+			rst.TotalIssues += cntResult(lineStr)
+
+		case strings.Contains(lineStr, "Fields Missing"):
+
+			rst.FieldMissingIssues += cntResult(lineStr)
+			rst.TotalIssues += cntResult(lineStr)
+
+		case strings.Contains(lineStr, "API Level Errors"):
+
+			rst.ApiIssuesJson += cntResult(lineStr)
+			rst.TotalIssues += cntResult(lineStr)
+
+		case strings.Contains(lineStr, "JSON vs DB Type Mismatch"):
+
+			rst.JsonVsDbTyIssues += cntResult(lineStr)
+			rst.TotalIssues += cntResult(lineStr)
+
+		case strings.Contains(lineStr, "Field Level Errors"):
+
+			rst.FieldMissingIssuesJson += cntResult(lineStr)
+			rst.TotalIssues += cntResult(lineStr)
+
+		}
+	}
+
+	return &rst
+}
+func cntResult(lineStr string) (cntValue int) {
+
+	reg := regexp.MustCompile("[0-9]+")
+
+	cntValue = 0
+
+	if len(reg.FindAllString(lineStr, -1)[0]) == 1 {
+
+		cnt, err := strconv.Atoi(reg.FindAllString(lineStr, -1)[0])
+
+		if err != nil {
+
+			fmt.Println("Conversion Error from string into int", err.Error())
+		}
+
+		cntValue = cnt
+
+	}
+	return
+}```

Could you please post as actual go code rather as a diff?

Also some words about what you wanted to achieve using that code are nice. It’s much easier to review when one knows in advance what the code is supposed to do.

2 Likes

You define rst outside of TestResult as a global variable, but you return a pointer to it. You should define rst inside the function. Regarding performance, if-else should be better than a switch block.

Inside cntResult, you set cntValue to 0 which is defunct as the default value of int is already 0. You can get rid of the cntValue variable and just return the value directly.

If you can supply some samples of the input provided to these functions you can remove the need for regexp which is compute heavy and will effect performance depending on use case.