Skip to content

Commit

Permalink
NewFromFile method
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Sarvarov committed Jun 15, 2024
1 parent f53cd71 commit 9b3a43d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/vue3_tailwind/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
25 changes: 8 additions & 17 deletions inertia.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -50,25 +45,21 @@ 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)
}
}

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)
Expand Down
51 changes: 30 additions & 21 deletions inertia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,44 @@ var rootTemplate = `<html>
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()

Expand Down

0 comments on commit 9b3a43d

Please sign in to comment.