Continued experimentation with the INIX “ini” file format. I take the original post code and add an alias feature.
Background
In three prior posts I presented a very simple metadata file storage approach. Pretty much using the INI file format with the sections as “heredocs.” In original INI format, afaik, the data within the sections were properties only, x=y.
Alias
Now I am working on adding the ability for a section of data to load data from another section. The sections to load are indicated by the ‘@’ symbol and then the section path. Multiple sections can be indicated. Though I’m using the term ‘alias’ for this, perhaps a better term is ‘importing’. So far, I can parse the alias from the tag string.
I have not implemented the actual import. One complexity left to solve is recursion. If this section imports another section, what if that section imports others sections?
Alias use case
Since currently the Inix file format is being used for test data, aliasing allows reuse of data without duplication, i.e., DRY. This is problematic with hierarchical data like JSON or XML, but much easier with lists or maps. Further features like overriding and interpolation would be useful for Java Properties data. The goal would be to eventually support use of the Cascading Configuration Pattern.
Example 1
[>First] Data in first [<] [>Second@First] Data in second [<]
Now when the data in section “Second” is loaded, the data from the aliased section is prepended to the current section data:
Data in first Data in second
Tag format
The section tag format is now: [>path#fragment@aliases?querystring]. Note that unlike a URI, the fragment does not appear at the end of the string.
The section ID is really the path#fragment. Thus, the end tag could be [<] or can use the section ID: [<path#fragment].
Example 2
[>demo1/deploy#two@account897@policy253?enabled=true&owner=false] stuff here [<demo1/deploy#two]
Grammar
The start of a grammar follows, but has not been ‘checked’ by attempted use of a parser generator like Antlr.
grammar Inix; section: start CRLF data end; start: '[>' path (fragment)?(alias)*('?' args)? ']'; end: '[<' path? ']'; path: NAME ('/' NAME)*; fragment: '#' NAME; alias: '@' NAME args: (NAME=NAME ('&' NAME=NAME)*)?; data: (ANYTHING CRLF)*; NAME: ('a'..'z' | 'A'..'Z')('a' .. 'z' | 'A'..'Z'|'0'..'9'|'_');
- Do the actual import of aliased section data.
- Allow multiple params per param key: ?measure=21,measure=34,measure=90. Or better yet, just allow array in arg string: measure=[21,34,90],color=red
Implementation
Source code available at Github: https://gist.github.com/josefbetancourt/7701645
Test class
Note that there are not enough tests and the implementation code has not been reviewed.
The test data is:
Environment
Groovy Version: 2.2.2 JVM: 1.7.0_25 Vendor: Oracle Corporation OS: Windows 7
Further Reading
- INI file
- Data File Metaformats
- Here document
- JSON configuration file format
- Creating External DSLs using ANTLR and Java
- Groovy Object Notation (GrON) for Data
Interchange - Cloanto Implementation of INI File Format
- http://groovy.codehaus.org/Tutorial+5+-+Capturing+regex+groups
- URI
- Designing a simple file format
- The Universal Design Pattern
![Creative Commons License](http://creativecommons.org/images/public/somerights20.png)