Socket
Socket
Sign inDemoInstall

github.com/webability-go/xcore/v2

Package Overview
Dependencies
1
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/webability-go/xcore/v2

Package xcore is a set of basic objects for programation (XCache for caches, XDataset for data sets, XLanguage for languages and XTemplate for templates). For GO, the actual existing code includes: - XCache: Application Memory Caches for any purpose, with time control and quantity control of object in the cache and also check changes against original source. It is a thread safe cache. - XDataset: Basic nested data structures for any purpose (template injection, configuration files, database records, etc). - XLanguage: language dependent text tables for internationalization of code. The sources can be text or XML file definitions. - XTemplate: template system with meta language to create complex documents (compatible with any text language, HTML, CSS, JS, PDF, XML, etc), heavily used on CMS systems and others. It is already used on sites that serve more than 60 million pages a month (500 pages per second on pike hour) and can be used on multithreading environment safely. XCache is a library to cache all the data you want into current application memory for a very fast access to the data. The access to the data support multithreading and concurrency. For the same reason, this type of cache is not persistent (if you exit the application) and cannot grow too much (as memory is the limit). However, you can control a timeout of each cache piece, and eventually a comparison function against a source (file, database, etc) to invalid the cache. 1. Declare a new XCache with NewXCache() function: 2. Fill in the cache: Once you have declared the cache, you can fill it with anything you want. The main cache object is an interface{} so you can put here anything you need, from simple variables to complex structures. You need to use the Set function: Note the ID is always a string, so convert a database key to string if needed. 3. To use the cache, just ask for your entry with Get function: 4. To maintain the cache: You may need Del function, to delete a specific entry (maybe because you deleted the record in database). You may also need Clean function to deletes a percentage of the cache, or Flush to deletes it all. The Verify function is used to check cache entries against their sources through the Validator function. Be very careful, if the cache is big or the Validator function is complex (maybe ask for a remote server information), the verification may be VERY slow and huge CPU use. The Count function gives some stats about the cache. 5. How to use Verify Function: This function is recommended when the source is local and fast to check (for instance a language file or a template file). When the source is distant (other cluster database, any rpc source on another network, integration of many parts, etc), it is more recommended to create a function that will delete the cache when needed (on demand cache change). The validator function is a func(id, time.Time) bool function. The first parameter is the ID entry in the cache, the second parameter the time of the entry was created. The validator function returns true is the cache is still valid, or false if it needs to be invalidated. The XCache is thread safe. The cache can be limited in quantity of entries and timeout for data. The cache is automanaged (for invalid expired data) and can be cleaned partially or totally manually. The XLanguage table of text entries can be loaded from XML file, XML string or normal text file or string. It is used to keep a table of id=value set of entries in any languages you need, so it is easy to switch between XLanguage instance based on the required language needed. Obviously, any XLanguage you load in any language should have the same id entries translated, for the same use. The XLanguage object is thread safe 1. loading: You can load any file or XML string directly into the object. 1.1 The XML Format is: NAMEOFTABLE is the name of your table entry, for example "loginform", "user_report", etc. LG is the ISO-3369 2 letters language ID, for example "es" for spanish, "en" for english, "fr" for french, etc. ENTRYNAME is the ID of the entry, for example "greating", "yourname", "submitbutton". ENTRYVALUE is the text for your entry, for example "Hello", "You are:", "Save" if your table is in english. STATUSVALUE is the status of the entry- You may put any value to control your translation over time and processes. 1.2 The flat text format is: ENTRYNAME is the ID of the entry, for example "greating", "yourname", "submitbutton". ENTRYVALUE is the text for your entry, for example "Hello", "You are:", "Save" if your table is in english. There is no name of table or language in this format (you "know" what you are loading). The advantage to use XML format is to have more control over your language, and eventyally add attributes into your entries, for instance you may add attributes translated="yes/no", verified="yes/no", and any other data that your system could insert. The XLanguage will ignore those attributes loading the table. 2. creation: To create a new XLanguage empty structure: There are 4 functions to create the language from a file or string, flat text or XML text: Then you can use the set of basic access functions: SetName/SetLanguage functions are used to set the table name and language of the object (generally to build an object from scratch). GetName/GetLanguage functions are used to get the table name and language of the object (generally when you load it from some source). Set/Get/Del functions are used to add or modify a new entry, read an entry, or deletes an entry in the object. SetStatus/GetStatus functions are used to add or get a status for the entry in the object. To create am XML file from the objet, you can use the GetXML() function 1. Overview: The XDataSet is a set of interfaces and basic classes ready-to-use to build a standard set of data optionally nested and hierarchical, that can be used for any purpose: - Keep complex data in memory. - Create JSON structures. - Inject data into templates. - Interchange database data (records set and record). You can store into it generic supported data, as well as any complex interface structures: - Int - Float - String - Time - Bool - []Int - []Float - []Time - []Bool - XDataSetDef (anything extended with this interface) - []String - Anything else ( interface{} ) - XDataSetCollectionDef (anything extended with this interface) The generic supported data comes with a set of functions to get/set those data directly into the XDataset. Example: Note that all references to XDataset and XDatasetCollection are pointers, always (to be able to modify the values of them). 2. XDatasetDef interface: It is the interface to describe a simple set of data mapped as "name": value, where value can be of any type. The interface implements a good amount of basic methods to get the value on various format such as GetString("name"), GetInt("name"), etc (see below). If the value is another type as asked, the method should contert it if possible. For instance "key":123 required through GetString("key") should return "123". The XDataset type is a simple map[string]interface{} with all the implemented methods and should be enough to use for almost all required cases. However, you can build any complex structure that extends the interface and implements all the required functions to stay compatible with the XDatasetDef. 3. XDatasetCollectionDef Interface: This is the interface used to extend any type of data as a Collection, i-e an array of XDatasetDef. This is a slice of any XDatasetDef compatible data. The interface implements some methods to work on array structure such as Push, Pop, Shift, Unshift and some methods to search data into the array. The XDatasetCollection type is a simple []DatasetDef with all the implemented methods and should be enough to use for almost all required cases. 1. Overview: The XDataSetTS is a DatasetDef structure, thread safe. It is build on the XDataset with the same properties, but is thread safe to protect Read/Write accesses from different thread. Example: You may also build a XDatasetTS to encapsulate a XDatasetDef that is not thread safe, to use it safely Note that all references to XDatasetTS are pointers, always (to be able to modify the values of them). The DatasetTS meet the XDatasetDef interface 1. Overview: This is a class to compile and keep a Template that can be injected with an XDataSet structure of data, with a metalanguage to inject the data. The metalanguage is extremely simple and is made to be useful and **really** separate programation from template code (not like other many generic template systems that just mix code and data). A template is a set of HTML/XML (or any other language) string with a meta language to inject variables and build a final string. The XCore XTemplate system is based on the injection of parameters, language translation strings and data fields directly into the HTML (Or any other language you need) template. The HTML itself (or any other language) is a text code not directly used by the template system, but used to dress the data you want to represent in your preferred language. The variables to inject must be into a XDataSet structure or into a structure extended from XDataSetDef interface. The injection of data is based on a XDataSet structure of values that can be nested into another XDataSet and XDataSetConnection and so on. The template compiler recognize nested arrays to automatically make loops on the information. Templates are made to store reusable HTML code, and overall easily changeable by people that do not know how to write programs. A template can be as simple as a single character (no variables to inject) to a very complex nested, conditional and loops sub-templates. Yes. this is a template, but a very simple one without need to inject any data. Let's go more complex: Having an array of data, we want to paint it beautifull: We can create a template to inject this data into it: 2. Create and use XTemplateData: In sight to create and use templates, you have all those possible options to use: Creates the XTemplate from a string or a file or any other source: Clone the XTemplate: 3. Metalanguage Reference: 3.1 Comments: %-- and --% You may use comments into your template. The comments will be discarded immediately at the compilation of the template and do not interfere with the rest of your code. Example: 3.2 Nested Templates: [[...]] and [[]] You can define new nested templates into your main template A nested template is defined by: The templteid is any combination of lowers letters only (a-z), numbers (0-9), and 3 special chars: . (point) - (dash) and _ (underline). The template is closed with [[]]. There is no limits into nesting templates. Any nested template will inheritate all the father elements and can use father elements too. To call a sub-template, you need to use &&templateid&& syntax (described below in this document). Example: You may use more than one id into the same template to avoid repetition of the same code. The different id's are separated with a pipe | Important note: A template will be visible only on the same level of its declaration. For example, if you put a subtemplate "b" into a subtemplate "a", it will not be visible by &&b&& from the top level, but only into the subtemplate "a". 3.3 Simple Elements: ##...## and {{...}} There are 2 types of simple elements. Language elements and Data injector elements (also called field elements). We "logically" define the 2 type of elements. The separation is only for human logic and template filling, however the language information can perfectly fit into the data to inject (and not use ## entries). 3.3.1 Languages elements: ##entry## All the languages elements should have the format: ##entry##. A language entry is generally anything written into your code or page that does not come from a database, and should adapt to the language of the client visiting your site. Using the languages elements may depend on the internationalization of your page. If your page is going to be in a single language forever, you really dont need to use languages entries. The language elements generally carry titles, menu options, tables headers etc. The language entries are set into the "#" entry of the main template XDataset to inject, and is a XLanguage table. Example: With data to inject: 3.3.2 Field elements: {{fieldname}} Fields values should have the format: {{fieldname}}. Your fields source can be a database or any other preferred repository data source. Example: You can access an element with its path into the data set to inject separating each field level with a > (greater than). This will take the name of the second hobby in the dataset defined upper. (collections are 0 indexed). The 1 denotes the second record of the hobbies XDatasetCollection. If the field is not found, it will be replaced with an empty string. Tecnically your field names can be any string in the dataset. However do not use { } or > into the names of your fields or the XTemplate may not use them correctly. We recommend to use lowercase names with numbers and ._- Accents and UTF8 symbols are also welcome. 3.3.3 Scope: When you use an id to point a value, the template will first search into the available ids of the local level. If no id is found, the it will search into the upper levers if any, and so on. Example: At the level of 'data2', using {{appname}} will get back 'DomCore'. At the level of 'key1', using {{appname}} will get back 'Nested App'. At the level of 'key2', using {{appname}} will get back 'DomCore'. At the level of root, 'data1' or 'detail', using {{appname}} will get back an empty string. 3.3.4 Path access: id>id>id>id At any level into the data array, you can access any entry into the subset array. For instance, taking the previous array of data to inject, let's suppose we are into a nested meta elements at the 'data1' level. You may want to access directly the 'Juan' entry. The path will be: The José's status value from the root will be: 3.4 Meta Elements They consist into an injection of a XDataset, called the "data to inject", into the template. The meta language is directly applied on the structure of the data array. The data to inject is a nested set of variables and values with the structure you want (there is no specific construction rules). You can inject nearly anything into a template meta elements. Example of a data array to inject: You can access directly any data into the array with its relative path (relative to the level you are when the metaelements are applied, see below). There are 4 structured meta elements in the XTemplate templates to use the data to inject: Reference, Loops, Condition and Debug. The structure of the meta elements in the template must follow the structure of the data to inject. 3.4.1 References to another template: &&order&& 3.4.1.1 When order is a single id (characters a-z0-9.-_), it will make a call to a sub template with the same set of data and replace the &&...&& with the result. The level in the data set is not changed. Example based on previous array of Fred's data: 3.4.1.2 When order contains 2 parameters separated by a semicolumn :, then second parameter is used to change the level of the data of array, with the subset with this id. The level in the data set is changed to this sub set. Example based on previous array of Fred's data: 3.4.1.3 When order contains 3 parameters separated by a semicolumn :, the second and third parameters are used to search the name of the new template based on the data fields to inject. This is an indirect access to the template. The name of the subtemplate is build with parameter3 as prefix and the content of parameter2 value. The third parameter must be empty. 3.4.2 Loops: @@order@@ 3.4.2.1 Overview This meta element will loop over each itterance of the set of data and concatenate each created template in the same order. You need to declare a sub template for this element. You may aso declare derivated sub templates for the different possible cases of the loop: For instance, If your main subtemplate for your look is called "hobby", you may need a different template for the first element, last element, Nth element, Element with a value "no" in the sport field, etc. The supported postfixes are: When the array to iterate is empty: - .none (for example "There is no hobby") When the array contains elements, it will search in order, the following template and use the first found: - templateid.key.[value] value is the key of the vector line. If the collection has a named key (string) or is a direct array (0, 1, 2...) - templateid.first if it is the first element of the array set (new from v1.01.11) - templateid.last if it is the first element of the array set (new from v1.01.11) - templateid.even if the line number is even - templateid in all other cases (odd is contained here if even is defined) Since v2.1.7, you can also use the pseudo field {{.counter}} into the loop subtemplate, to get the number of the counter of the loop, it is 1-based (first loop is 1, not 0) 3.4.2.2 When order is a single id (characters a-z0-9.-_), it will make a call to the sub template id with the same subset of data with the same id and replace the @@...@@ for each itterance of the data with the result. Example based on previous array of Fred's data: 3.4.2.3 When order contains 2 parameters separated by a semicolumn :, then first parameter is used to change the level of the data of array, with the subset with this id, and the second one for the template to use. Example based on previous array of Fred's data: 3.4.3 Conditional: ??order?? Makes a call to a subtemplate only if the field exists and have a value. This is very userfull to call a sub template for instance when an image or a video is set. When the condition is not met, it will search for the [id].none template. The conditional element does not change the level in the data set. 3.4.3.1 When order is a single id (characters a-z0-9.-_), it will make a call to the sub template id with the same field in the data and replace the ??...?? with the corresponding template Example based on previous array of Fred's data: 3.4.3.2 When order contains 2 parameters separated by a semicolumn :, then second parameter is used to change the level of the data of array, with the subset with this id. Example based on previous array of Fred's data: If the asked field is a catalog, true/false, numbered, you may also use .[value] subtemplates 3.5 Debug Tools: !!order!! There are two keywords to dump the content of the data set. This is very useful when you dont know the code that calls the template, don't remember some values, or for debug facilities. 3.5.1 !!dump!! Will show the totality of the data set, with ids and values. 3.5.1 !!list!! Will show only the tree of parameters, values are not shown.


Version published

Readme

Source

XCore v2 for GO

Go Report Card GoDoc GolangCI

Minimum version of GO: 1.17 (for time.Time compatibility)

The XCore package is used to build basic object for programmation. for the WebAbility compatility code For GO, the actual existing code includes:

  • XCache: Application Memory Caches, thread safe.
  • XDataset: Basic nested data structures for any purpose (template injection, configuration files, database records, etc) Support thread safe operations on thread safe structures (XDatasetTS and XDatasetCollectionTS)
  • XLanguage: language dependent text tables, thread safe
  • XTemplate: template system with meta language, thread safe cloning

Manuals are available on godoc.org GoDoc

TO DO, maybe:

  • XDataset.Set should accept path too > > >
  • Get*Collection should convert types too
  • XTemplate must concatenate strings after compilation
  • Implements functions as data entry for template Execute (simple data or loop functions, can get backs anything, creates an interface) Some improvements to check, later:
  • Adds mutex on XTemplate ?? (they should be used locally on every thread, or not ??), maybe adds a flag "thread safe" ?
  • XCache: activate persistant cache too (shared memory) ????? maybe not for go itself, but for instance to talk with other memory data used by other languages and apps, or to not loose the caches if the app is restarted.

Version Changes Control

v2.2.2 - 2023-10-12

  • Added a security on the sub template .none for ?? meta language into XTemplate, to not try to use an inexistant template and throw a panic error.

v2.2.1 - 2023-09-29

  • Added the missing sub template .none for ?? meta language into XTemplate.

v2.2.0 - 2023-08-31

  • Added the parameter status to xlanguage XML and function to get/set the parameter.
  • Added the function GetXML() to marshal the structure to an XML file.

v2.1.7 - 2023-06-15

  • Added the missing counter for @@ meta language. Using {{.counter}} into the loop template, you can add the number of the dataset, 1-based.

v2.1.6 - 2023-05-25

  • Bug corrected into GetCollection(id) function, if the dataset is not an XDatasetCollection, it should return nil, not a panic error

v2.1.5 - 2023-05-17

  • Bump golang.org/x/text from 0.3.5 to 0.3.8 for security upgrade

v2.1.4 - 2023-05-17

  • Bug corrected on @@ loops subtemplates, the .none template was not reach when the array is set but empty

v2.1.3 - 2022-09-02

  • Bug corrected on XDataset.GetString() and XDatasetCollection.GetDataString(). If the value is NIL int the dataset, it returns now "" and not ""

v2.1.2 - 2022-03-02

  • XTemplate: Added = to metalanguage string tags to resolve also the paths (bug corrected).

v2.1.1 - 2022-03-01

  • XTemplate: Added > to metalanguage string tags to resolve also the paths (bug corrected).

v2.1.0 - 2022-02-27

  • XLanguage: bug corrected on unlock of stringload and loadFromFile (was blocking the system)
  • XTemplate: The metalanguage keywords are now only recognized if they match authorized characters (for instance &&keyword&&), to avoid bugs in JS with && and || and !!.
  • Print functions of time.Time corrected (as in go 1.17, the print format changes) into the *test.go test functions

v2.0.9 - 2021-11-25

  • XCache modified to defer mutex unlocks instead of directly unlock into the code, to avoid dead locks in case of thread panic and crashes.
  • XLanguage modified to defer mutex unlocks instead of directly unlock into the code, to avoid dead locks in case of thread panic and crashes.

v2.0.8 - 2021-05-18

  • XTemplate is now clonable: newtemplate := template.Clone()

v2.0.7 - 2021-03-23

  • []interface{} added to NewXDataset to be decoded as []map[string]interface{} because of decoded JSON

v2.0.5 - 2021-03-23

  • function NewXDataset(data) and NewXDatasetCollection(data) added, to build XDatasets based on a classic map[string]interface{}
  • XDatasetTS added to the manual
  • XLanguage is now thread safe
  • XTemplate: Error corrected in string() function (was saying XLanguage)
  • Each cache entry is now able to manage its own TLL if set. New function SetTTL(id, duration)

v2.0.4 - 2020-04-13

  • XLanguage: Added GetEntries() func (not thread safe yet)

v2.0.3 - 2020-04-08

  • XLanguage: Error corrected on loadXMLString: the data was not loading correctly into the XLanguage object.

v2.0.1, v2.0.2 - 2020-03-29

  • Version adjustment for github and go modules v2

v2.0.0 - 2020-03-29

  • xdataset.go now as a coverage of 100% with xdataset_test.go
  • XCache now uses R/W mutex
  • New interfaces.go file to keep all the interfaces in it (XDatasetDef, XDatasetCollectionDef)
  • New xdatasetts.go for thread safe dataset
  • New xdatasetts_test.go for thread safe dataset tests
  • New xdatasetcollection.go for the collection of dataset (separation from xdataset.go)
  • New xdatasetcollection_test.go for collection tests
  • New xdatasetcollectionts.go for thread safe dataset
  • New xdatasetcollectionts_test.go for thread safe datasetcollection tests
  • XLanguage is now thread safe with R/W mutexes

v1.1.0 - 2020-03-01

  • Modularization of XCore
  • XLanguage tests and examples are now conform to Go test units
  • Implementation of XLanguage.String and XLanguage.GoString, removed Print
  • XCache tests and examples are now conform to Go test units
  • XDataset tests and examples are now conform to Go test units
  • Implementation of XDataset.String and XDataset.GoString, removed Print
  • Implementation of XDatasetCollection.String and XDatasetCollection.GoString, removed Print
  • XTemplate tests and examples are now conform to Go test units
  • Implementation of XTemplate.String and XTemplate.GoString, removed Print

v1.0.1 - 2020-02-10

  • Documentation corrections
  • Bug on String() and GoString() corrected

v1.0.0 - 2020-02-09

  • Version leveling
  • Documentation corrections
  • Change functions Stringify() by String() and GoString() for language compatibility
  • Tests functions enhanced

v0.3.1 - 2020-02-09

  • XDatasetDef.Get must accept a path as key (id>id>id)
  • XTemplates now resolve {{ fields with path id>id>id
  • XTemplates now resolve @@ metalanguage with 1 and 2 Parameters
  • XTemplates now resolve && metalanguage with 1,2 and 3 Parameters
  • XTemplates now resolve ?? metalanguage with 1, and 2 Parameters
  • XTemplates now resolve !! debug orders
  • XTemplates now implements sub templates derivation (.none .first .last .(number) )
  • Manuals for XCache, XLanguage and XTemplate written with reference of the metalanguage
  • Examples for dataset and xtemplate added (working version)
  • XDataset and XDatasetCollection .Stringify now prints also field names.

v0.3.0 - 2020-02-06

  • The properties of XTemplateParam are now public so the full structure can be used to build other type of code based on the XTemplate rules
  • The subtemplates IDs must be lowers, numbers and . - _ in sight of integration with other systems that can mix tags [[]] within the code

v0.2.3 - 2020-01-23

  • Corrected a bug to avoid null pointer panic error if the array of data for XTemplate.Execute function is nil

v0.2.2 - 2019-12-21

  • XLanguage now support golang x/text/language instead of direct iso 2 charater language
  • godoc manuals for xlanguage, xdataset and xtemplate prepared

v0.2.1 - 2019-12-13

  • XCache manual enhanced with examples

v0.2.0 - 2019-12-06

  • XCache Code simplified to expose XCache definition as public, remove not usefull funcion (Get*)
  • XCache 0.2.0 is not compatible with XCache 0.1.* , you may need to change your code
  • Added more conversions between int-float-bool in XDataset.Get*

v0.1.2 - 2019-12-05

  • Code cleaned to meet golangci standards, golint checks, more documentation.

V0.1.1 - 2019-11-05

  • XCore Code comments enhanced to publish in godoc.org as libraries documentation

V0.1.0 - 2019-10-18

  • Code cleaned to pass 100% of goreportcard.com. Card note added in this document

V0.0.9 - 2019-07-18

  • Error corrected on XCache: removing an element from a slice when the element is the last one was causing out of bound index.
  • XCache.maxitem = 0 (no number of elements limit) is corrected: it was not working

V0.0.8 - 2019-06-25

  • Added Clone on XDatasetDef and XDataCollectionsetDef
  • XDataset testunit added

V0.0.7 - 2019-03-06

  • Time functions added to XDatasetDef and XDatasetCollectionDef interfaces, and XDataset and XDatasetCollection structures
  • Manual for XCache finished
  • Manual for XDataset finished
  • Preformat for XLanguage manual
  • Preformat for XTemplate manual

V0.0.6 - 2019-02-07

  • Added xcache.GetId(), xcache.GetMax() and xcache.GetExpire()
  • XCache Documentation modified

V0.0.5 - 2019-02-05

  • Added conversion between types con XDataset.Get* functions
  • Manuals for XDataSet and XTemplate complemented

V0.0.4 - 2019-01-06

  • XDataset.Get* functions added to comply with any type of data of a dataset for templates, config, database record etc.
  • XCache manual completed.

V0.0.3 - 2019-01-02

  • Added XCache.Flush function
  • Function XCache.Del implemented
  • Function XCache.Clean implemented for expiration, and free some space
  • Function XCache.Verify created
  • Function XCache.SetValidator added, to check cache validity agains a validator function
  • Files flags and code removed from XCache. If the cache is a file, the user should controls the files with its own Validator function (original funcions put in examples as a file validator). This lets a lots of flexibility to validate against any source of data (files, database, complex calculations, external streams, etc)
  • XCache is ready for candidate release

V0.0.2 - 2018-12-17

  • Creation of XCache with all set of functions.
  • Creation of XLanguage with all set of functions.
  • Creation of XTemplate with all set of functions. Basic work done
  • Creation of a set of interfaces that XTemplate need to execute and inject the template,
  • Creation of a basic XDataset and colection based on interfaces to build a set of data for the template.
  • Added xcore.go with version number as constant

V0.0.1 - 2018-11-14

  • First basic commit with XLanguage object created

FAQs

Last updated on 12 Oct 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc