// Copyright 2024 Matthew Rich . 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 }