Add Seed functino

This commit is contained in:
George Suntres
2026-04-01 20:47:02 -04:00
parent c78aed6441
commit c83e44b04a
3 changed files with 142 additions and 5 deletions

126
seed.go Normal file
View File

@@ -0,0 +1,126 @@
package structful
import (
"log"
"fmt"
"os"
"encoding/json"
"crypto/md5"
"github.com/spf13/cast"
"git.gsuntres.com/general/commons"
)
func SeedStructful(files []string) error {
for _, file := range files {
// 1. Read the file bytes
content, err := os.ReadFile(file)
if err != nil {
log.Fatalf("Error reading file: %v", err)
}
// 2. Define the target map
var data map[string]any
// 3. Unmarshal (parse) the JSON into the map
err = json.Unmarshal(content, &data)
if err != nil {
if syntaxErr, ok := err.(*json.SyntaxError); ok {
min := commons.MathMin(int64(50), syntaxErr.Offset)
from := syntaxErr.Offset - min
to := syntaxErr.Offset + min
sliceBuf := content[from:to]
log.Printf("Failed to unmarshal %#v \n %s", syntaxErr, string(sliceBuf))
}
return err
}
// 4. Check if composite
isComposite := false
isCompositeVal, ok := data["_composite"]
if ok {
isComposite = isCompositeVal.(bool)
}
var name string
nameVal, nameOk := data["_name"]
if nameOk {
name = cast.ToString(nameVal)
}
var version string
versionVal, versionOk := data["_version"]
if versionOk {
version = cast.ToString(versionVal)
}
if isComposite {
// if composite extract entries
entries := make([]any, 0)
entriesVal, entriesOk := data["_entries"]
if entriesOk {
entries = entriesVal.([]any)
for _, entry := range entries {
reg := Current()
d, err := cast.ToStringMapE(entry)
if err != nil {
log.Printf("failed to cast %v", err)
continue
}
toHash, err := json.Marshal(d)
if err != nil {
log.Printf("failed to hash %v", err)
continue
}
hashed := fmt.Sprintf("%x", md5.Sum(toHash))
d["_hash"] = hashed
d["_name"] = name
d["_version"] = version
if !reg.CheckHash(hashed) {
err = reg.Save(d)
if err != nil {
log.Printf("failed to save %v", err)
continue
}
}
}
}
} else {
toHash, err := json.Marshal(data)
if err != nil {
log.Printf("failed to hash %v", err)
continue
}
hashed := fmt.Sprintf("%x", md5.Sum(toHash))
// 5. Save it
reg := Current()
data["_hash"] = hashed
if !reg.CheckHash(hashed) {
err = reg.Save(data)
if err != nil {
return err
}
}
}
}
return nil
}