jx/internal/folio/mock_file_converter_test.go
Matthew Rich 55fd39f09d
Some checks are pending
Lint / golangci-lint (push) Waiting to run
Declarative Tests / test (push) Waiting to run
Declarative Tests / build-fedora (push) Waiting to run
Declarative Tests / build-ubuntu-focal (push) Waiting to run
add document imports
2024-09-24 19:22:49 +00:00

53 lines
1.3 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package folio
import (
"decl/internal/data"
"decl/internal/codec"
"path/filepath"
"net/url"
)
func RegisterConverterMocks() {
TestConverterTypes.Register([]string{"file"}, func(u *url.URL) data.Converter {
var uri URI
uri.SetURL(u)
f := NewMockFileConverter(uri)
f.Name = filepath.Join(u.Hostname(), u.Path)
return f
})
}
type MockFileConverter struct {
*MockConverter `json:"-" yaml:"-"`
Name string `json:"name" yaml:"name"`
}
func NewMockConverter(typename string, uri URI) *MockConverter {
return &MockConverter {
InjectType: func() data.TypeName { return data.TypeName(typename) },
InjectEmit: func(document data.Document, filter data.ElementSelector) (res data.Resource, err error) {
return
},
InjectExtract: func(res data.Resource, filter data.ElementSelector) (doc data.Document, err error) {
doc = DocumentRegistry.NewDocument(uri)
if r, readErr := uri.ContentReaderStream(); readErr != nil {
return doc, readErr
} else {
err = doc.LoadReader(r, codec.FormatYaml)
defer r.Close()
}
return
},
InjectClose: func() error { return nil },
}
}
func NewMockFileConverter(uri URI) *MockFileConverter {
f := &MockFileConverter {}
f.MockConverter = NewMockConverter("file", uri)
return f
}