Skip to content

Commit

Permalink
Merge pull request #18 from webability-go/late-night
Browse files Browse the repository at this point in the history
patch v0.4.3
  • Loading branch information
metalwolf authored Nov 17, 2021
2 parents 6c34598 + 37b5052 commit ea49a9f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 36 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
2 changes: 1 addition & 1 deletion testunit/example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 32 additions & 14 deletions xconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
//
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down
82 changes: 62 additions & 20 deletions xconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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" {
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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) {
Expand All @@ -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")
}

}

Expand All @@ -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")
}
}

0 comments on commit ea49a9f

Please sign in to comment.