diff --git a/flixkit.go b/flixkit.go index 9818cc0..21e2b6d 100644 --- a/flixkit.go +++ b/flixkit.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/fs" "log" "net/http" "net/url" @@ -51,9 +50,13 @@ type flixServiceImpl struct { var _ FlixService = (*flixServiceImpl)(nil) +type FileReader interface { + ReadFile(path string) ([]byte, error) +} + type Config struct { FlixServerURL string - FileReader fs.ReadFileFS + FileReader FileReader } func NewFlixService(config *Config) FlixService { @@ -230,6 +233,9 @@ func (s *flixServiceImpl) GetTemplate(ctx context.Context, flixQuery string) (st } case flixPath: + if s.config.FileReader == nil { + return "", fmt.Errorf("file reader not provided") + } file, err := s.config.FileReader.ReadFile(flixQuery) if err != nil { return "", fmt.Errorf("could not read flix file %s: %w", flixQuery, err) diff --git a/flixkit_test.go b/flixkit_test.go index e99fbfd..db80645 100644 --- a/flixkit_test.go +++ b/flixkit_test.go @@ -2,7 +2,6 @@ package flixkit import ( "context" - "io/fs" "net/http" "net/http/httptest" "testing" @@ -252,6 +251,12 @@ func TestFetchFlix(t *testing.T) { assert.Equal("Hello World", body, "GetFlix should return the correct body") } +type DefaultReader struct{} + +func (d DefaultReader) ReadFile(path string) ([]byte, error) { + return []byte(flix_template), nil +} + func TestGetFlixRaw(t *testing.T) { assert := assert.New(t) @@ -261,14 +266,14 @@ func TestGetFlixRaw(t *testing.T) { })) defer server.Close() - flixService := NewFlixService(&Config{FlixServerURL: server.URL}) + flixService := NewFlixService(&Config{FlixServerURL: server.URL, FileReader: DefaultReader{}}) ctx := context.Background() body, err := flixService.GetTemplate(ctx, "templateName") assert.NoError(err, "GetFlixByName should not return an error") assert.Equal("Hello World", body, "GetFlixByName should return the correct body") } -func TestGetFlix(t *testing.T) { +func TestGetFlixFilename(t *testing.T) { assert := assert.New(t) server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { @@ -276,9 +281,9 @@ func TestGetFlix(t *testing.T) { })) defer server.Close() - flixService := NewFlixService(&Config{FlixServerURL: server.URL}) + flixService := NewFlixService(&Config{FlixServerURL: server.URL, FileReader: DefaultReader{}}) ctx := context.Background() - flix, err := flixService.GetTemplate(ctx, "templateName") + flix, err := flixService.GetTemplate(ctx, "./templateFileName") assert.NoError(err, "GetParsedFlixByName should not return an error") assert.NotNil(flix, "GetParsedFlixByName should not return a nil Flix") assert.Equal(flix_template, flix, "GetParsedFlixByName should return the correct Flix") @@ -316,10 +321,6 @@ func TestGetFlixByID(t *testing.T) { assert.Equal(flix_template, flix, "GetParsedFlixByID should return the correct Flix") } -type MapFsReader struct { - FS fs.FS -} - func TestTemplateVersion(t *testing.T) { assert := assert.New(t) diff --git a/templateFileName b/templateFileName new file mode 100644 index 0000000..e69de29 diff --git a/types.go b/types.go index dd8bff1..f5f74b4 100644 --- a/types.go +++ b/types.go @@ -295,7 +295,6 @@ func ShaHex(value interface{}, debugKey string) string { // Convert the hash to a hexadecimal string hashHex := hex.EncodeToString(hash[:]) - //fmt.Println(value, hashHex) return hashHex }