diff --git a/container/container.go b/container/container.go index a8d2708c..e7a12d82 100644 --- a/container/container.go +++ b/container/container.go @@ -49,6 +49,7 @@ func (c *container) GetConfig() *config.Config { return c.config } +// GetMessages returns the map has key and message. func (c *container) GetMessages() map[string]string { return c.messages } diff --git a/go.mod b/go.mod index e59ebbf0..ef8bdaa8 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/ybkuroki/go-webapp-sample -go 1.19 +go 1.20 require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 diff --git a/main.go b/main.go index 8ee9be3a..1e3e667e 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ var propsFile embed.FS func main() { e := echo.New() - messages := util.ReadPropertiesFile(propsFile) + messages := util.ReadPropertiesFile(propsFile, "messages.properties") conf, env := config.Load(yamlFile) logger := logger.InitLogger(env, zapYamlFile) logger.GetZapLogger().Infof("Loaded this configuration : application." + env + ".yml") diff --git a/util/props.go b/util/props.go index 8f968703..37ba950b 100644 --- a/util/props.go +++ b/util/props.go @@ -12,10 +12,10 @@ const ( ) // ReadPropertiesFile reads a properties file and it returns a map has the keys and values in the file. -func ReadPropertiesFile(fs embed.FS) map[string]string { +func ReadPropertiesFile(fs embed.FS, fileName string) map[string]string { config := make(map[string]string) - file, err := fs.Open("messages.properties") + file, err := fs.Open(fileName) if err != nil { return nil } diff --git a/util/props_test.go b/util/props_test.go new file mode 100644 index 00000000..93019f4f --- /dev/null +++ b/util/props_test.go @@ -0,0 +1,22 @@ +package util + +import ( + "embed" + "testing" + + "github.com/stretchr/testify/assert" +) + +//go:embed test.properties +var testPropsFile embed.FS + +func TestReadPropertiesFile(t *testing.T) { + messages := ReadPropertiesFile(testPropsFile, "test.properties") + + assert.Equal(t, "testtest", messages["props1"]) + assert.Equal(t, "test test", messages["props2"]) + assert.Equal(t, "", messages["props3"]) + _, ok := messages["props4"] + assert.Equal(t, false, ok) + assert.Equal(t, "test #test", messages["props5"]) +} diff --git a/util/test.properties b/util/test.properties new file mode 100644 index 00000000..7f53d0c4 --- /dev/null +++ b/util/test.properties @@ -0,0 +1,7 @@ +# comment line +props1=testtest +props2 = test test +props3= +# comment line +props4 +props5= test #test