From 9b3a43d25058d8d03d2af0d09faf36b712a4f4df Mon Sep 17 00:00:00 2001 From: Roman Sarvarov Date: Sun, 16 Jun 2024 01:40:25 +0300 Subject: [PATCH] NewFromFile method --- README.md | 2 +- examples/vue3_tailwind/main.go | 2 +- inertia.go | 25 ++++++----------- inertia_test.go | 51 ++++++++++++++++++++-------------- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index c35158b..e5713ff 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ import ( ) func main() { - i, err := inertia.New("resources/views/root.html") // put here HTML or path to the root template file + i, err := inertia.NewFromFile("resources/views/root.html") // or just provide HTML to New constructor if err != nil { log.Fatal(err) } diff --git a/examples/vue3_tailwind/main.go b/examples/vue3_tailwind/main.go index 711f877..ef1d423 100644 --- a/examples/vue3_tailwind/main.go +++ b/examples/vue3_tailwind/main.go @@ -26,7 +26,7 @@ func main() { func initInertia() *inertia.Inertia { manifestPath := "./public/build/manifest.json" - i, err := inertia.New( + i, err := inertia.NewFromFile( "resources/views/root.html", inertia.WithVersionFromFile(manifestPath), inertia.WithSSR(), diff --git a/inertia.go b/inertia.go index 5e1383f..25db1db 100644 --- a/inertia.go +++ b/inertia.go @@ -29,18 +29,13 @@ type Inertia struct { } // New initializes and returns Inertia. -func New(rootTemplate string, opts ...Option) (*Inertia, error) { - rootTemplate, err := tryGetRootTemplateHTMLFromPath(rootTemplate) - if err != nil { - return nil, fmt.Errorf("try get root template html from path: %w", err) - } - - if rootTemplate == "" { +func New(rootTemplateHTML string, opts ...Option) (*Inertia, error) { + if rootTemplateHTML == "" { return nil, fmt.Errorf("blank root template") } i := &Inertia{ - rootTemplateHTML: rootTemplate, + rootTemplateHTML: rootTemplateHTML, marshallJSON: json.Marshal, containerID: "app", logger: log.New(io.Discard, "", 0), @@ -50,7 +45,7 @@ func New(rootTemplate string, opts ...Option) (*Inertia, error) { } for _, opt := range opts { - if err = opt(i); err != nil { + if err := opt(i); err != nil { return nil, fmt.Errorf("initialize inertia: %w", err) } } @@ -58,17 +53,13 @@ func New(rootTemplate string, opts ...Option) (*Inertia, error) { return i, nil } -func tryGetRootTemplateHTMLFromPath(rootTemplate string) (string, error) { - bs, err := os.ReadFile(rootTemplate) +func NewFromFile(rootTemplatePath string, opts ...Option) (*Inertia, error) { + bs, err := os.ReadFile(rootTemplatePath) if err != nil { - if os.IsNotExist(err) { - return rootTemplate, nil - } - - return "", fmt.Errorf("read file: %w", err) + return nil, fmt.Errorf("read file %q: %w", rootTemplatePath, err) } - return string(bs), nil + return New(string(bs), opts...) } type marshallJSON func(v any) ([]byte, error) diff --git a/inertia_test.go b/inertia_test.go index 60bf6a5..72b93ca 100644 --- a/inertia_test.go +++ b/inertia_test.go @@ -13,35 +13,44 @@ var rootTemplate = ` func TestNew(t *testing.T) { t.Parallel() - t.Run("root template init", func(t *testing.T) { + t.Run("success", func(t *testing.T) { t.Parallel() - t.Run("by html", func(t *testing.T) { - i, err := New(rootTemplate) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } + i, err := New(rootTemplate) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } - if i.rootTemplateHTML != rootTemplate { - t.Fatalf("root template html=%s, want=%s", i.rootTemplateHTML, rootTemplate) - } - }) - - t.Run("by path", func(t *testing.T) { - f := tmpFile(t, rootTemplate) + if i.rootTemplateHTML != rootTemplate { + t.Fatalf("root template html=%s, want=%s", i.rootTemplateHTML, rootTemplate) + } + }) - i, err := New(f.Name()) - if err != nil { - t.Fatalf("unexpected error: %s", err) - } + t.Run("blank", func(t *testing.T) { + t.Parallel() - if i.rootTemplateHTML != rootTemplate { - t.Fatalf("root template html=%s, want=%s", i.rootTemplateHTML, rootTemplate) - } - }) + _, err := New("") + if err == nil { + t.Fatal("error expected") + } }) } +func TestNewFromFile(t *testing.T) { + t.Parallel() + + f := tmpFile(t, rootTemplate) + + i, err := NewFromFile(f.Name()) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + if i.rootTemplateHTML != rootTemplate { + t.Fatalf("root template html=%s, want=%s", i.rootTemplateHTML, rootTemplate) + } +} + func TestInertia_ShareProp(t *testing.T) { t.Parallel()