Brush up the go code

This commit is contained in:
2022-04-19 21:22:59 +02:00
parent b88208ba3d
commit 63875a6f59
7 changed files with 161 additions and 46 deletions

View File

@@ -12,6 +12,7 @@ import (
"covergen/pkg/covergen"
)
// jsmap is just a type alias that corresponds to the ES5 Record<string, any>, or just a plain object {}
type jsmap = map[string]interface{}
type covergenArgs struct {
@@ -21,6 +22,8 @@ type covergenArgs struct {
HLColor string `jsref:"hlColor"`
}
// parseHexColor takes a CSS hex color (#RRGGBB or the shorthand #RGB) and parses out the red, green, and blue components
// into a covergen.Color
func parseHexColor(s string) (c covergen.Color, err error) {
switch len(s) {
case 7:
@@ -33,11 +36,12 @@ func parseHexColor(s string) (c covergen.Color, err error) {
c.B *= 17
default:
err = fmt.Errorf("invalid length, must be 7 or 4")
}
return
}
// settingsFromValue tries to parse the WASM boundary-crossing arguments blob into a golang-native settings object
func settingsFromValue(arg js.Value) (*covergen.CoverSettings, error) {
settings := covergenArgs{
NumberPrefix: "offerte",
@@ -71,6 +75,7 @@ func settingsFromValue(arg js.Value) (*covergen.CoverSettings, error) {
}, nil
}
// pdfToJs takes the pdf object and outputs it into a Uint8Array and passes that back to JS.
func pdfToJs(pdf *fpdf.Fpdf) (js.Value, error) {
var buf bytes.Buffer
if err := pdf.Output(&buf); err != nil {
@@ -83,10 +88,11 @@ func pdfToJs(pdf *fpdf.Fpdf) (js.Value, error) {
return ta, nil
}
// generateCover is the JS exposed entrypoint to generate a 2 page pdf with both the front and back cover
func generateCover(this js.Value, args []js.Value) interface{} {
defer func() {
if r := recover(); r != nil {
fmt.Println("recovered", r)
fmt.Printf("recovered from critical error: %+v", r)
}
}()
@@ -119,7 +125,7 @@ func generateCover(this js.Value, args []js.Value) interface{} {
func generateSplitCover(this js.Value, args []js.Value) interface{} {
defer func() {
if r := recover(); r != nil {
fmt.Println("recovered", r)
fmt.Printf("recovered from critical error: %+v", r)
}
}()