Prechádzať zdrojové kódy

Add JSON render + small refactoring (Closes #25)

Sacha Ligthert 1 rok pred
rodič
commit
2bdcdcb1dd

+ 4 - 2
export/Export.go

@@ -4,9 +4,11 @@ func (export *Export) Export() (render string) {
 	// Print the valid solutions
 	switch export.Controller.Output {
 	case "human":
-		render = export.RenderHumanReadableSolutions()
+		render = export.renderHumanReadable()
 	case "flat":
-		render = export.renderFlatSolutions()
+		render = export.renderFlat()
+	case "json":
+		render = export.renderJSON()
 	}
 	return
 }

+ 2 - 2
export/renderFlatSolutions.go → export/renderFlat.go

@@ -5,8 +5,8 @@ import (
 	"strconv"
 )
 
-// Print solutions into a human friendly format for in the console.
-func (export *Export) renderFlatSolutions() (render string) {
+// Render output as stored internally.
+func (export *Export) renderFlat() (render string) {
 	for solutionIndex, solution := range export.Controller.Solutions {
 		render += fmt.Sprintln("\nSolution #" + strconv.Itoa(solutionIndex+1) + ":")
 		render += fmt.Sprintln(solution)

+ 2 - 2
export/renderHumanReadableSolutions.go → export/renderHumanReadable.go

@@ -5,8 +5,8 @@ import (
 	"strconv"
 )
 
-// Print solutions into a human friendly format for in the console.
-func (export *Export) RenderHumanReadableSolutions() (render string) {
+// Render solutions in a human friendly format.
+func (export *Export) renderHumanReadable() (render string) {
 	for solutionIndex, solution := range export.Controller.Solutions {
 		render += fmt.Sprintln("\nSolution #" + strconv.Itoa(solutionIndex+1) + ":")
 		render += fmt.Sprintln("╔═══════════╗")

+ 36 - 0
export/renderJSON.go

@@ -0,0 +1,36 @@
+package export
+
+import (
+	"encoding/json"
+	"log"
+)
+
+// Render JSON output.
+func (export *Export) renderJSON() (render string) {
+	type solution_type map[string]any
+	solutions := make([]solution_type, 0)
+
+	for solutionIndex, solution := range export.Controller.Solutions {
+		solutionMap := map[string]any{
+			"order": solutionIndex,
+			"row1":  solution[0],
+			"row2":  solution[1],
+			"row3":  solution[2],
+			"row4":  solution[3],
+			"row5":  solution[4],
+			"row6":  solution[5],
+			"row7":  solution[6],
+			"row8":  solution[7],
+			"row9":  solution[8],
+		}
+		solutions = append(solutions, solutionMap)
+	}
+	renderBytes, err := json.Marshal(solutions)
+	if err != nil {
+		log.Println("ERROR: json.Marshal error:", err)
+		log.Println("Printing solution as-is:", solutions)
+		return ""
+	}
+	render = string(renderBytes)
+	return
+}

+ 2 - 2
flags/ParseFlags.go

@@ -24,7 +24,7 @@ func (flags *Flags) ParseFlags() {
 	flag.IntVar(&flags.Controller.NumCPUs, "numcpu", runtime.NumCPU(), "Number of CPU cores to assign to this task.")
 	flag.IntVar(&flags.Controller.Split, "split", 1, "Split the tasks in n parts. This depends on the availability of the first row.")
 	flag.IntVar(&flags.Controller.Part, "part", 1, "Process part x in n parts. Cannot be lower than 1, or higher than specified in split.")
-	flag.StringVar(&flags.Controller.Output, "output", "human", "Type of output. 'human' for human readable. 'flat' for flat as stored in memory output.")
+	flag.StringVar(&flags.Controller.Output, "output", "human", "Type of output. 'human' for human readable. 'flat' for flat as stored in memory output. 'json' for JSON output.")
 
 	// Parse the flags
 	flag.Parse()
@@ -75,7 +75,7 @@ func (flags *Flags) ParseFlags() {
 
 	// Process output selection
 	flags.Controller.Output = strings.ToLower(flags.Controller.Output)
-	if flags.Controller.Output != "human" && flags.Controller.Output != "flat" {
+	if flags.Controller.Output != "human" && flags.Controller.Output != "flat" && flags.Controller.Output != "json" {
 		log.Printf("ERROR: Invalid output, can only be 'human' or 'flat'.\n")
 		flags.printUsage()
 		os.Exit(1)