Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added go.mod support, fixed a bug and made a simple addition #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/tucnak/store

go 1.20

require (
github.com/BurntSushi/toml v1.2.1
gopkg.in/yaml.v2 v2.4.0
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
60 changes: 35 additions & 25 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
)

// MarshalFunc is any marshaler.
type MarshalFunc func(v interface{}) ([]byte, error)
type MarshalFunc func(v any) ([]byte, error)

// UnmarshalFunc is any unmarshaler.
type UnmarshalFunc func(data []byte, v interface{}) error
type UnmarshalFunc func(data []byte, v any) error

var (
applicationName = ""
Expand All @@ -37,7 +37,7 @@ func init() {
formats["yml"] = format{m: yaml.Marshal, um: yaml.Unmarshal}

formats["toml"] = format{
m: func(v interface{}) ([]byte, error) {
m: func(v any) ([]byte, error) {
b := bytes.Buffer{}
err := toml.NewEncoder(&b).Encode(v)
return b.Bytes(), err
Expand Down Expand Up @@ -73,7 +73,7 @@ func Register(extension string, m MarshalFunc, um UnmarshalFunc) {
// replacing it with a newly created object, derived from type of `v`.
//
// Load panics on unknown configuration formats.
func Load(path string, v interface{}) error {
func Load(path string, v any) error {
if applicationName == "" {
panic("store: application name not defined")
}
Expand All @@ -93,7 +93,7 @@ func Load(path string, v interface{}) error {
// Path is a full filename, including the file extension, e.g. "foobar.json".
//
// Save panics on unknown configuration formats.
func Save(path string, v interface{}) error {
func Save(path string, v any) error {
if applicationName == "" {
panic("store: application name not defined")
}
Expand All @@ -106,7 +106,7 @@ func Save(path string, v interface{}) error {
}

// LoadWith loads the configuration using any unmarshaler at all.
func LoadWith(path string, v interface{}, um UnmarshalFunc) error {
func LoadWith(path string, v any, um UnmarshalFunc) error {
if applicationName == "" {
panic("store: application name not defined")
}
Expand All @@ -122,7 +122,7 @@ func LoadWith(path string, v interface{}, um UnmarshalFunc) error {
empty := reflect.New(reflect.TypeOf(v))
if innerErr := Save(path, &empty); innerErr != nil {
// Smth going on with the file system... returning error.
return err
return innerErr
}

v = empty
Expand All @@ -138,7 +138,7 @@ func LoadWith(path string, v interface{}, um UnmarshalFunc) error {
}

// SaveWith saves the configuration using any marshaler at all.
func SaveWith(path string, v interface{}, m MarshalFunc) error {
func SaveWith(path string, v any, m MarshalFunc) error {
if applicationName == "" {
panic("store: application name not defined")
}
Expand Down Expand Up @@ -175,27 +175,37 @@ func extension(path string) string {
return ""
}

// buildPlatformPath builds a platform-dependent path for relative path given.
func buildPlatformPath(path string) string {
if runtime.GOOS == "windows" {
return fmt.Sprintf("%s\\%s\\%s", os.Getenv("APPDATA"),
applicationName,
path)
func getApplicationDirPath(envPath, sep string) string {
return fmt.Sprintf("%s%s%s", envPath, sep, applicationName)
}

// GetApplicationDirPath returns the platform specific path to
// the application specific configuration directory.
func GetApplicationDirPath() string {
if applicationName == "" {
panic("store: application name not defined")
}
envPath, sep := getPlatformPathAndSep()
return getApplicationDirPath(envPath, sep)
}

var unixConfigDir string
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
unixConfigDir = xdg
func getPlatformPathAndSep() (string, string) {
sep := "/"
var envPath string
if runtime.GOOS == "windows" {
sep = "\\"
envPath = os.Getenv("APPDATA")
} else if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
envPath = xdg
} else {
unixConfigDir = os.Getenv("HOME") + "/.config"
envPath = os.Getenv("HOME") + "/.config"
}

return fmt.Sprintf("%s/%s/%s", unixConfigDir,
applicationName,
path)
return envPath, sep
}

// SetApplicationName is DEPRECATED (use Init instead).
func SetApplicationName(handle string) {
applicationName = handle
// buildPlatformPath builds a platform-dependent path for the given relative path.
func buildPlatformPath(path string) string {
envPath, sep := getPlatformPathAndSep()
applicationDir := getApplicationDirPath(envPath, sep)
return fmt.Sprintf("%s%s%s", applicationDir, sep, path)
}
8 changes: 4 additions & 4 deletions store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func TestSaveLoad(t *testing.T) {
settings := Settings{
Age: 42,
Cats: []Cat{
Cat{"Rudolph", true},
Cat{"Patrick", false},
Cat{"Jeremy", true},
{"Rudolph", true},
{"Patrick", false},
{"Jeremy", true},
},
RandomString: "gophers are gonna conquer the world",
}
Expand All @@ -59,7 +59,7 @@ func TestSaveLoad(t *testing.T) {
return
}

defer os.Remove(buildPlatformPath(settingsFile))
defer os.RemoveAll(GetApplicationDirPath())

var newSettings Settings

Expand Down