From 904fdf698ee3c6ce81e410a7f5facd8ddc091f2a Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 17 Jan 2022 11:53:08 +0100 Subject: [PATCH] Add settings parsing --- settings/settings.go | 28 ++++++++++++++++++++++++++++ settings/settings_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 settings/settings.go create mode 100644 settings/settings_test.go diff --git a/settings/settings.go b/settings/settings.go new file mode 100644 index 0000000..6e88584 --- /dev/null +++ b/settings/settings.go @@ -0,0 +1,28 @@ +package settings + +import "encoding/json" + +const ( + Local FileSystemType = iota +) + +type FileSystemType int + +type Settings struct { + Endpoint string + Token string + FileSystem FileSystemSettings +} + +type FileSystemSettings struct { + Type FileSystemType + BasePath string +} + +func ParseSettings(data []byte) Settings { + settings := Settings{} + + json.Unmarshal(data, &settings) + + return settings +} diff --git a/settings/settings_test.go b/settings/settings_test.go new file mode 100644 index 0000000..3a1f44e --- /dev/null +++ b/settings/settings_test.go @@ -0,0 +1,26 @@ +package settings + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSettingsLoading(t *testing.T) { + const file string = `{ + "endpoint": "0.0.0.0:8000", + "token": "foobar", + "fileSystem": { + "type": 0, + "basePath": "assets" + } + }` + + t.Run("Settings parsing is successful", func(t *testing.T) { + settings := ParseSettings([]byte(file)) + + assert.Equal(t, "0.0.0.0:8000", settings.Endpoint) + assert.Equal(t, "foobar", settings.Token) + assert.Equal(t, "assets", settings.FileSystem.BasePath) + }) +}