Skip to content
Snippets Groups Projects
Verified Commit 454e2097 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat add the posibility to recognise the filetype, add HasFile and RemoveFile

parent 43f795be
No related branches found
No related tags found
No related merge requests found
......@@ -41,6 +41,8 @@ func (s *Settings[C]) SetMnemonic(mnemonic string) *Settings[C] {
}
// Config() returns the configuration
// Remember that the configuration is a copy of the original configuration.
// Changes to the configuration will not be reflected in the original configuration.
func (s *Settings[C]) Config() C {
return s.config
}
......@@ -4,7 +4,7 @@
package configuration
import (
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/assert"
"testing"
)
......
......@@ -4,7 +4,7 @@
package configuration
import (
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
......
......@@ -89,3 +89,9 @@ type MismatchedTypeError error
func newMismatchedTypeError(expected, actual reflect.Type) MismatchedTypeError {
return MismatchedTypeError(errors.New("expected type " + expected.String() + " but got " + actual.String()))
}
type UnknownFileExtensionError error
func newUnknownFileExtensionError(extension, filename string) UnknownFileExtensionError {
return UnknownFileExtensionError(errors.New("unknown file extension " + extension + " for file " + filename))
}
......@@ -8,6 +8,7 @@ import (
"io/fs"
"os"
"path"
"path/filepath"
)
type files struct {
......@@ -23,9 +24,60 @@ type fileBackend struct {
fs fs.FS
}
func (s *Settings[c]) HasFile(file string) bool {
for _, f := range s.files.files {
if f.path == file {
return true
}
}
return false
}
// AddFiles adds a file to the list of files to import
func (s *Settings[C]) AddFile(file string, format Format) *Settings[C] {
s.files.files = append(s.files.files, files{file, format})
func (s *Settings[C]) AddFile(file string, format ...Format) *Settings[C] {
var f Format
f = RecogniseFormat
if len(format) <= 1 {
f = format[0]
} else {
panic("too many arguments")
}
if f == RecogniseFormat {
ext := filepath.Ext(file)
switch ext {
case ".yaml":
f = Yaml
case ".json":
f = Json
case ".toml":
f = Toml
case ".properties":
f = Properties
default:
if ext != "" {
s.errors = append(s.errors, newUnknownFileExtensionError(ext, file))
return s
}
f = s.files.format
}
}
s.files.files = append(s.files.files, files{file, f})
return s
}
func (s *Settings[C]) RemoveFile(file string) *Settings[C] {
for i, f := range s.files.files {
if f.path == file {
s.files.files = append(s.files.files[:i], s.files.files[i+1:]...)
return s
}
}
return s
}
......
......@@ -5,7 +5,7 @@ package configuration
import (
"bytes"
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/assert"
"io"
"io/fs"
"os"
......@@ -510,6 +510,54 @@ func TestSettingAddFile(t *testing.T) {
}
func TestRemoveFile(t *testing.T) {
cfg := ConfigStruct2{}
f, err := os.CreateTemp("", "watch_test")
if err != nil {
t.Error(err)
return
}
defer os.Remove(f.Name())
f.WriteString("A: \"from file\"")
c := New(cfg)
c.AddFile(f.Name(), Yaml)
assert.Equal(t, c.Config().A, "")
c.Import()
assert.Equal(t, c.Config().A, "from file")
assert.True(t, c.HasFile(f.Name()))
c.RemoveFile(f.Name())
assert.False(t, c.HasFile(f.Name()))
}
func TestSettingAddFileWithRecognise(t *testing.T) {
cfg := ConfigStruct2{}
f, err := os.CreateTemp("", "watch_test")
if err != nil {
t.Error(err)
return
}
defer os.Remove(f.Name())
f.WriteString("A: \"from file\"")
c := New(cfg)
c.AddFile(f.Name(), RecogniseFormat)
assert.Equal(t, c.Config().A, "")
c.Import()
assert.Equal(t, c.Config().A, "from file")
}
func TestSetConfigName(t *testing.T) {
cfg := ConfigStruct2{}
......
......@@ -8,7 +8,7 @@ import (
"os"
"testing"
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/assert"
)
// Example for README
......
......@@ -6,6 +6,7 @@ package configuration
type Format int
const (
RecogniseFormat = -1
Yaml Format = iota
Json
Toml
......
......@@ -4,7 +4,7 @@
package configuration
import (
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/assert"
"testing"
)
......
......@@ -4,7 +4,7 @@
package configuration
import (
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/assert"
"testing"
)
......
......@@ -4,7 +4,7 @@
package configuration
import (
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/assert"
"os"
"testing"
"time"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment