Skip to content

Commit

Permalink
feat: basic class implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Feb 8, 2024
1 parent 1f0ebe9 commit 2b21c82
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 185 deletions.
16 changes: 7 additions & 9 deletions class.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/lukasjarosch/skipper/data"
)
Expand All @@ -17,7 +15,10 @@ type Codec interface {
}

type Class struct {
filePath string
// Name is the common name of the class.
Name string
// FilePath is the path to the file which this class represents.
FilePath string
container *data.Container
}

Expand All @@ -41,18 +42,15 @@ func NewClass(filePath string, codec Codec) (*Class, error) {
return nil, fmt.Errorf("unable to decode class data: %w", err)
}

// the name of the container is the filename without file extension
containerName := filepath.Base(filePath)
containerName = strings.TrimSuffix(containerName, filepath.Ext(containerName))

container, err := data.NewContainer(containerName, fileData)
container, err := data.NewContainer(PathFileBaseName(filePath), fileData)
if err != nil {
return nil, fmt.Errorf("invalid container: %w", err)
}

return &Class{
container: container,
filePath: filePath,
FilePath: filePath,
Name: PathFileBaseName(filePath),
}, nil
}

Expand Down
39 changes: 20 additions & 19 deletions class_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
package skipper_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"

"github.com/lukasjarosch/skipper"
"github.com/lukasjarosch/skipper/codec"
"github.com/lukasjarosch/skipper/data"
)

func TestNewClass(t *testing.T) {
path := "/tmp/foo.yaml"
// Test case 1: Valid file path and codec
filePath := "testdata/simple_class.yaml"
class, err := skipper.NewClass(filePath, codec.NewYamlCodec())
assert.NoError(t, err, "No error expected when creating class")
assert.NotNil(t, class, "Class should not be nil")
assert.Equal(t, filePath, class.FilePath, "File path should match input")

class, err := skipper.NewClass(path, codec.NewYamlCodec())
assert.NoError(t, err)
assert.NotNil(t, class)
// Test case 2: Empty file path
emptyFilePath := ""
_, err = skipper.NewClass(emptyFilePath, codec.NewYamlCodec())
assert.Error(t, err)
assert.EqualError(t, err, skipper.ErrEmptyFilePath.Error())

fmt.Println(class.Get("foo.bar"))
class.Set("foo.baz", "changed")
err = class.Set("foo.new.bar.baz.foo.test.ohai", map[string]interface{}{
"hello": "world",
})
err = class.Set("foo.ohai.35.test", map[string]interface{}{
"hello": "world",
})
assert.NoError(t, err)
fmt.Println(class.Get("foo.ohai.23"))
// Test case 3: Non-existent file path
nonExistentFilePath := "nonexistent.txt"
_, err = skipper.NewClass(nonExistentFilePath, codec.NewYamlCodec())
assert.Error(t, err)
assert.ErrorContains(t, err, "no such file or directory")

d := class.GetAll()
o, _ := yaml.Marshal(d.Raw)
fmt.Println(string(o))
// Test case 4: Multiple root keys in file
_, err = skipper.NewClass("testdata/multiple_root_keys.yaml", codec.NewYamlCodec())
assert.ErrorContains(t, err, data.ErrMultipleRootKeys.Error())
}
13 changes: 13 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package skipper

import (
"path/filepath"
"strings"
)

func PathFileBaseName(path string) string {
str := filepath.Base(path)
str = strings.TrimSuffix(str, filepath.Ext(str))

return str
}
6 changes: 0 additions & 6 deletions go.work

This file was deleted.

151 changes: 0 additions & 151 deletions go.work.sum

This file was deleted.

3 changes: 3 additions & 0 deletions testdata/multiple_root_keys.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a: b
c: d
e: f
3 changes: 3 additions & 0 deletions testdata/simple_class.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
simple_class:
foo: bar
hello: world

0 comments on commit 2b21c82

Please sign in to comment.