jx/internal/builtin/builtin.go

70 lines
1.8 KiB
Go
Raw Normal View History

2024-09-19 05:31:13 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package builtin
import (
_ "context"
_ "encoding/json"
"fmt"
_ "gopkg.in/yaml.v3"
_ "net/url"
_ "regexp"
_ "strings"
_ "os"
_ "io"
_ "compress/gzip"
_ "archive/tar"
_ "errors"
_ "path/filepath"
_ "decl/internal/codec"
"decl/internal/data"
"decl/internal/fs"
"decl/internal/folio"
_ "decl/internal/resource"
_ "decl/internal/config"
_ "decl/internal/fan"
"embed"
"log/slog"
)
//go:embed documents/*.jx.yaml
var documentFiles embed.FS
func Load(uri folio.URI) (documents []data.Document, err error) {
var extractor data.Converter
var sourceResource data.Resource
if extractor, err = folio.DocumentRegistry.ConverterTypes.New(string(uri)); err == nil {
2024-09-25 05:13:53 +00:00
slog.Info("builtin.Load() extractor", "uri", uri, "error", err)
2024-09-19 05:31:13 +00:00
targetDeclaration := folio.NewDeclaration()
if err = targetDeclaration.NewResource((*string)(&uri)); err == nil {
2024-09-25 05:13:53 +00:00
slog.Info("builtin.Load() extract many", "resource", sourceResource, "error", err, "uri", uri, "extractor", extractor)
2024-09-19 05:31:13 +00:00
sourceResource = targetDeclaration.Attributes
sourceResource.(data.FileResource).SetFS(documentFiles)
documents, err = extractor.(data.ManyExtractor).ExtractMany(sourceResource, nil)
2024-09-25 05:13:53 +00:00
slog.Info("builtin.Load() extract many", "resource", sourceResource, "error", err)
2024-09-19 05:31:13 +00:00
}
}
return
}
func BuiltInDocuments() (documents []data.Document, err error) {
docFs := fs.NewWalkDir(documentFiles, "", func(fsys fs.FS, path string, file fs.DirEntry) (walkErr error) {
2024-09-19 05:31:13 +00:00
u := folio.URI(fmt.Sprintf("file://%s", path))
slog.Info("BuiltInDocuments()", "file", u)
if ! file.IsDir() {
if loadDocs, loadErr := Load(u); loadErr == nil {
documents = append(documents, loadDocs...)
} else {
err = loadErr
}
}
return
})
docFs.Walk(nil)
2024-09-19 05:31:13 +00:00
return documents, err
}