Add JSON render + small refactoring (Closes #25)

This commit is contained in:
2025-01-28 19:50:42 +01:00
parent 71fefd760e
commit 2bdcdcb1dd
5 changed files with 46 additions and 8 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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
export/renderJSON.go Normal file
View File

@ -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
}