From 37b505274a4897b1557eb2a7858ed84af22292c2 Mon Sep 17 00:00:00 2001 From: metalwolf Date: Tue, 16 Nov 2021 22:40:49 -0600 Subject: [PATCH] patch v0.4.3 --- README.md | 5 +++ go.mod | 5 ++- go.sum | 5 +++ testunit/example.conf | 2 +- xconfig.go | 46 ++++++++++++++++-------- xconfig_test.go | 82 ++++++++++++++++++++++++++++++++----------- 6 files changed, 109 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 0ac58d4..86fb433 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,11 @@ TO DO: Version Changes Control ======================= +v0.4.3 - 2021-11-16 +----------------------- +- Documentation revised and added with Marshal and SaveFile Functions +- Bug corrected in Marshal, sometimes an empty line was ignored by the reconstruction of the string + v0.4.2 - 2020-03-30 ----------------------- - Pointers to self structure changed (c.Parameters instead of (*c).Parameters) diff --git a/go.mod b/go.mod index a9618b5..d558ad5 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/webability-go/xconfig go 1.14 -require github.com/webability-go/xcore/v2 v2.0.2 +require ( + github.com/webability-go/xcore/v2 v2.0.8 + golang.org/x/text v0.3.7 // indirect +) diff --git a/go.sum b/go.sum index aad0a12..c0c5864 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,10 @@ github.com/webability-go/xcore/v2 v2.0.2 h1:ztdwunb54Oe3i+qMV+JZAl2yGhMhjsWLr5oGRW9acpQ= github.com/webability-go/xcore/v2 v2.0.2/go.mod h1:8ZughUZ/qGYTcp02igrR43TjH+s+hsoV3m/rseLd3ZQ= +github.com/webability-go/xcore/v2 v2.0.8 h1:IrkmSGz38/qbD73E51s6lJXg/knJ+Xr25lbx8fQ5rm8= +github.com/webability-go/xcore/v2 v2.0.8/go.mod h1:8kUO/y99ZLalR3CBSeY4jhPQVuvQAQM7WuChbDZFn2A= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/testunit/example.conf b/testunit/example.conf index 9346ceb..cfa128d 100644 --- a/testunit/example.conf +++ b/testunit/example.conf @@ -21,7 +21,7 @@ country=ES language.en.welcome=Welcome to the XConfig examples language.en.ack=OK language.en.cancel=Cancel - +# spanish language.es.welcome=Bienvenido a los ejemplos de XConfig language.es.ack=Perfecto language.es.cancel=Cancelar diff --git a/xconfig.go b/xconfig.go index 9578b4c..6ce64ce 100644 --- a/xconfig.go +++ b/xconfig.go @@ -37,12 +37,22 @@ // // xc.LoadFile("/path/to/my/file.conf") // -// 5. And finally use the configuration +// 5. Use the configuration // // myparam := xc.Get("myparam") // -// myparam will take the type of the parameter: string, integer, float64, bool, or an array of string, integer or float64 -// (you should be aware of the type of your parameter before using it) +// myparam will take the type of the parameter: string, integer, float64, bool, or an array of string, integer or float64. +// (you should be aware of the type of your parameter before using it). +// You can also use the Get* cast functions. +// +// 6. You can also set some new parameters +// +// xc.Set("mynewparam", "newvalue") +// +// 7. And finally save the new configuration +// +// xc.SaveFile("/path/to/my/file.conf") +// // // // File format reference @@ -136,7 +146,7 @@ // This will build a list of values in the object. // The list of values is kept as an array of values. // -// If you have a mixed type of values, you will get an error +// If you have a mixed type of values, you will get an error. // // for instance: // @@ -300,17 +310,25 @@ // config.Set("parameter5", true) // // -// Advanced topics // -// Parsing and algorithms +// Saving configuration +// +// After loading, creating, modifying your configuration file, you may need to save your configuration. +// +// For this yoy have 2 functions: +// +// +// config := &xconfig.XConfig{} +// config.Set("myparam1", "My value") +// // Create a string with the content of the new config: +// fmt.Println(config.Marshal()) +// +// // Directly save the modified config file: +// config.SaveFile("path/to/your/file.conf") +// +// Note: if you load your configuration file with comments in it, when you save it, the comments and presentation (new lines) will be respected. +// If you add new parameters, they will be added to the end of the file. New lines will be removed into the definition of an array of data. // -// LoadFile: load file in string then call LoadString -// MergeFile: load file in string then call MergeString -// loadstring: parse string in temporary XConfig then call LoadXConfig -// Mergestring: parse string in temporary XConfig then call MergeXConfig -// LoadXConfig: call parsemap with merge=false -// MergeConfig: call parsemap with merge=true -// parsemap: // package xconfig @@ -330,7 +348,7 @@ import ( ) // VERSION is the used version nombre of the XCore library. -const VERSION = "0.4.2" +const VERSION = "0.4.3" // Parameter is the basic entry parameter into the configuration object // Value is the value of the parameter. diff --git a/xconfig_test.go b/xconfig_test.go index 51dadb4..7a4d904 100644 --- a/xconfig_test.go +++ b/xconfig_test.go @@ -58,7 +58,7 @@ func TestOneStringParam(t *testing.T) { conf.LoadString("#First test\nparam1=value1\n\n;End of test 1\n") // print what we got - fmt.Println(conf) + // fmt.Println(conf) // direct access if (*conf).Parameters["param1"].Value != "value1" { @@ -77,7 +77,7 @@ func TestStringParam(t *testing.T) { conf.LoadString("param1=value1\nparam2=value2\nparam3=value3\nparam4=\"123\nparam5=\"on") // print what we got - fmt.Println(conf) + // fmt.Println(conf) // direct access if (*conf).Parameters["param1"].Value != "value1" || (*conf).Parameters["param2"].Value != "value2" || (*conf).Parameters["param3"].Value != "value3" { @@ -103,7 +103,7 @@ func TestBoolParam(t *testing.T) { conf := New() conf.LoadString("param1=yes\nparam2=true\nparam3=on\nparam4=no\nparam5=none\nparam6=false\nparam7=off") - fmt.Println(conf) + // fmt.Println(conf) if (*conf).Parameters["param1"].Value != true || (*conf).Parameters["param2"].Value != true || (*conf).Parameters["param3"].Value != true || (*conf).Parameters["param4"].Value != false || (*conf).Parameters["param5"].Value != false || (*conf).Parameters["param6"].Value != false || (*conf).Parameters["param7"].Value != false { t.Errorf("The boolean parameters are not correctly set") @@ -115,7 +115,7 @@ func TestIntegerParam(t *testing.T) { conf := New() conf.LoadString("param1=0\nparam2=-1\nparam3=1234567890") - fmt.Println(conf) + // fmt.Println(conf) if (*conf).Parameters["param1"].Value != 0 || (*conf).Parameters["param2"].Value != -1 || (*conf).Parameters["param3"].Value != 1234567890 { t.Errorf("The integer parameters are not correctly set") @@ -127,7 +127,7 @@ func TestFloatParam(t *testing.T) { conf := New() conf.LoadString("param1=0.123\nparam2=12e7\nparam3=-76364.2") - fmt.Println(conf) + // fmt.Println(conf) if (*conf).Parameters["param1"].Value != 0.123 || (*conf).Parameters["param2"].Value != 12e7 || (*conf).Parameters["param3"].Value != -76364.2 { t.Errorf("The float parameters are not correctly set") @@ -139,13 +139,16 @@ func TestArrayParam(t *testing.T) { conf := New() conf.LoadString("param1=value1\nparam1=value2\nparam1=value3\nparam2=123\nparam2=-1\nparam2=1234567890\nparam3=0.1\nparam3=-123.567\nparam3=12e7\nparam4=true\nparam4=off\nparam4=on") - fmt.Println(conf) + // fmt.Println(conf) - // arr := (*conf).Parameters["param1"].Value - - // if arr.([]string)[0] != "value1" || arr.([]string)[1] != "value2" || arr.([]string)[2] != "value3" { - // t.Errorf("The array parameter is not correctly set") - // } + arr, ext := conf.GetStringCollection("param1") + if !ext { + t.Errorf("The array parameter is not correctly set") + return + } + if arr[0] != "value1" || arr[1] != "value2" || arr[2] != "value3" { + t.Errorf("The array parameter is not correctly set") + } } func TestClone(t *testing.T) { @@ -154,11 +157,19 @@ func TestClone(t *testing.T) { conf.LoadString("#First test\nparam1=value1\n\n;End of test 1\n") conf2 := conf.Clone() - conf.Set("param10", "value10") + + if fmt.Sprint(conf) != fmt.Sprint(conf2) { + t.Errorf("Error cloning the xconfig") + } // print what we got - fmt.Println("ANTES DE CLONE", conf) - fmt.Println("OBJETO CLONED", conf2) + // fmt.Println("ANTES DE CLONE", conf) + // fmt.Println("OBJETO CLONED", conf2) + + conf.Set("param10", "value10") + if fmt.Sprint(conf) == fmt.Sprint(conf2) { + t.Errorf("Error cloning the xconfig") + } } @@ -170,16 +181,47 @@ func TestStructure(t *testing.T) { return } - fmt.Printf(conf.Marshal()) - + s0 := conf.Marshal() + r0 := `# this file is named myconfig.conf, used in following examples +# the # denotes a comment. +; is also a comment +parameter1=value1 +parameter2=value2 +parameter2=value3 + +# global config: +ip=127.0.0.1 +port=80 +domain=test.com + +# Some list of values, they will result into an array +country=MX +country=US +country=FR +country=JP +country=ES + +# some subsets +language.en.welcome=Welcome to the XConfig examples +language.en.ack=OK +language.en.cancel=Cancel +language.es.welcome=Bienvenido a los ejemplos de XConfig +language.es.ack=Perfecto +language.es.cancel=Cancelar +# spanish +` + + if s0 != r0 { + t.Errorf("Error marshelling file, considering pushing the comments into an array of values") + } } func TestDel(t *testing.T) { - // Test 4: conf := New() conf.LoadString("param1=0.123\nparam2=12e7\nparam3=-76364.2") - - fmt.Println(conf) conf.Del("param1") - fmt.Println(conf) + s0 := fmt.Sprint(conf) + if s0 != "XConfig[\nparam2:1.2e+08\nparam3:-76364.2\n]\n" { + t.Errorf("The parameter has not been correctly deleted") + } }