github.com/webability-go/xconfig (go): Package xconfig loads a configuration file similar to a .ini file, but with some important improvements:  - The xconfig recognize bool, int, float and Strings, and also collections of values and hierarchical nested sub-config sets.  - The xconfig is compatible with XDataset to inject and use in templates, database records, etc.  - You can load more than one file in merge mode or replacing mode in the same config object.  - You can set and get parameters at any time.  - You can load the config object based on a file, but also on a string and another object; this way you can serialize, unserialize, transfer by any strem, save or load on database the object, etc.  - You can also save back the config string with all its comments.  1. Installing the package:  Execute on your operating system:  2. Importing the package:  3. Then you need first to create a blank XConfig instance:  4. Then, you generally load a file to fill in your XConfig definition  5. Use the configuration  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  7. And finally save the new configuration  The config file is a simple utf8 flat text file. The configuration file is a set of key=value parameters, with optional comments.  The configuration file have the following syntax:  You can add as many as parameters you wish into the file.  1. comments:  You may add comments and also comment unused parameter with # or ; at the beginning of the line  2. Parameter keys:  The parameter key is a string with characters [a-zA-Z0-9_-] only, with a minimum of 1 character.  The point (.) denotes a sub set of parameters (a new sub XConfig dataset for this parameter)  In this case the database entry of the XConfig is again another XConfig with 3 parameters  into it: user, pass and db.  3. Assignation sign:  A simple = sign is the normal assignation, the "add" or "replace" behaviour depends on the funcion called for loading the configuration (Load* or Merge* functions).  In this case various asignation to the same parameter will create an array of values of the same type as the first declared parameter.  An equal sign preceded by a + (+=) will always add the parameter to the array of values, never replace it (see Merge/Load).  An equal sign preceded by a : (:=) will always replace the parameter and discard any already set values.  4. Parameter values:  There are 4 types of values:  - Strings  - Integer  - Float  - Boolean  The value has no restrictions except it must enter into the line (no line breaks allowed) The compiler accepts strings "true", "on", "yes" as a boolean 'true' and "false", "off", "no", "none" as a boolean 'false'. For instance, that means parameter=off is a boolean false, and parameter=yes is a boolean true in the XConfig structure.  The compiler also convert all integers to an int parameter in the XConfig structure, and float values as float64 type. If you want a natural integer, float or boolean interpreted as a string, you must start it with a " character: param1="123   will be the string 123 in the XConfig structure  If you want a string starting with a ", you will need to put 2 " at the beginning: param=""abc   will be the string "abc in the XConfig structure  3. list of values:  You can repeat as many time you need the same parameter name with different values. 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.  for instance:  The order IS important.  Once loaded you will get a []string{“es”, “en”, “fr”, “jp”} assigned to the “languages” parameter.  + and :  You may load two config file (or more), for example when you have a master config file and a local replacement values config file. In this case, the values of the second file will "replace" the data already loaded with the first file. This is a replacement of config entries.  In the other hand, you may merge two config files (or more), for example when you need to fragment the data into a set of simpler files. In this case, the values will just be "added" as if the two files were a simple file (create arrays on same id entries). This is a merging of config entries.  If you want to force the behaviour of variables (merging into a replacement loading, or replace into a merging loading), you may use the two assignement operators + and :  := will be used to replace a value by a new value, meanwhile += will be used to add a new value to the array of data. The rules of "same type of data" must be observed also in this case (+=)  With the following files:  The result config after merging local into global will be:  The XConfig object is easily usable as:  or, if you load your own file by other means (remote, database etc)  or, if you already have your configuration into a Map of Strings (unserialized, etc)  There are 3 sets of public functions:  Load*: to load a file, a string dataset, or another XConfig dataset. Loading means all already existing parameters will be replaced by the new configuration.  This is useful when you have a main config file, and a local config file that must replace some values Functions are LoadFile, LoadString and LoadXConfig  Merge*: to merge a file, a string dataset, or another XConfig dataset. Merging means all new entries will be added to the already existing parameters.  This is userfull then you split your config file into subset of parameters each (for instance database config, memory config, internationalization config, etc) Functions are MergeFile, MergeString and MergeXConfig  Get/Set/Add: to read, set (replace) or add (merge) parameters to the XConfig.  Once you have an instance of your configuration, you may use it like this:  After loading, creating, modifying your configuration file, you may need to save your configuration.  For this yoy have 2 functions:  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.
Keywords: go|golang|webability|webability-xconfig|xconfig