Skip to content

dots aren't decoded in deeply nested sections #22

Open

Description

@rlidwka
> require('ini').decode('[x\\.y\\.z.a\\.b\\.c]\nfoo=bar')
{ 'x\\.y\\.z': { 'a.b.c': { foo: 'bar' } } }

Activity

  1. alvassin commented on May 21, 2014

    @alvassin

    +1

    Example №1

    [test]
    lvl1.value = value1
    lvl1.lvl2.value1 = 1

    will return

    { 'lvl1.value': 'value1', 'lvl1.lvl2.value1': '1' }

    Example №2

    [test][test1]
    lvl1.value = value1
    lvl1.lvl2.value1 = 1

    Will return undefined. Guys, your package is working well, it has cool name, and is very popular. Please add such support.

  2. isaacs commented on May 21, 2014

    @isaacs
    Contributor

    @rlidwka Your example is a valid weirdness. Patch welcome.

    @avasin Your first example is working as intended. Your second example is weird and I am not sure what the correct behavior is. Can you please post separate issues for what you expect the (2) case to do? It's not related to @rlidwka's issue.

  3. alvassin commented on May 21, 2014

    @alvassin

    @isaacs hm.. i expected to get deep parsed object in the first example:

    {
      lvl1: {
        value : value1
        lvl2  : {
            value1 : 1
        }
      }
    }
    

    Second example behaviour is expected to be the same as described here - deep object parsing.

  4. mycaule commented on May 26, 2014

    @mycaule
    ; config.ini
    [file1.jpg]
    size=10
    date=2014
    
    [file2.jpg]
    size=11
    date=2013
    
    [other-file2.jpg]
    size=10
    date=2012
    

    I would like to access a file with variable filenames sections. It would be nice to access the object with quotes.

    var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
    config["file1.jpg"].size = 6;
    // config.file1.jpg.size = 6
    

    You need to comment lines 101-120 to do this. One possible feature should be to be able to disable this with an option when invoking ini package.

  5. rlidwka commented on Jun 17, 2014

    @rlidwka
    Author

    @mycaule , that's not how ini works. All ini parsers treat dots as path separators, and not doing so would be incompatible.

    Instead, I would suggest to use this syntax:

    [files."other-file2.jpg"]
    size=10
    date=2012
    
    var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
    config.files["file1.jpg"].size = 6;
    

    This module doesn't support it, but I've seen parsers in other languages that do.

    I implemented it here, can provide a patch if someone is interested.

  6. mycaule commented on Jun 20, 2014

    @mycaule

    I have never seen a dot in a Windows config file section used that way.
    And this doesn't seem standard at all.

    I think it is more like a hack to do some sort of mapping between JSON and INI.

  7. p3k commented on Sep 18, 2019

    @p3k

    All ini parsers treat dots as path separators, and not doing so would be incompatible.

    i cannot find this in the spec anywhere… is this something that evolved as a quasi-standard? is the statement really true for all ini parsers?

    the idea to use a filename for the section key does seem legit. but this package makes parsing such ini files cumbersome at the very least.

  8. rehanvdm commented on Feb 11, 2023

    @rehanvdm

    This is my solution, seems to be working to flatten it back as expected:

    /**
     * Reduce the nested object back to a flat object by concatenating object keys with a `.`
     * @param obj Nested object
     * @returns {{}}
     */
    function flattenIniObject(obj)
    {
        function _flattenIniObject(obj, topLevel = [], resp = {}) {
            let props = {};
            for(let key in obj) {
                if(typeof obj[key] == 'object') {
                    topLevel.push(key);
                    resp = { ...resp, ..._flattenIniObject(obj[key], topLevel, resp)}
                }
                else
                    props[key] = obj[key];
            }
    
            const topLevelName = topLevel.join(".");
            if(topLevelName !== '' && Object.keys(props).length > 0)
                resp = { ...resp, [topLevelName]: props}
    
            topLevel.pop();
            return resp;
        }
    
        return _flattenIniObject(obj);
    }
    
    let iniData = ini.parse(`; config.ini
    [file1.jpg]
    size=10
    date=2014
    
    [file2.jpg]
    size=11
    date=2013
    
    [other-file2.jpg]
    size=10
    date=2012
    
    ; extra test cases
    [file1]
    strange=but okay
    
    [level_1]
    data=level 111
    [level_1.level_2]
    data=level 222
    [level_1.level_2.level_3]
    data=level 333
    cool=yes
    `)
    
    const flat = flattenIniObject(iniData);
    console.log(flat)

    Outputs:
    image

  9. s-light commented on Apr 23, 2025

    @s-light

    i think dots should not be used as separators if they are part of a quoted string in a section header...
    and if i understand the most comments here correctly that is the main point?!

    .gitmodules

    [submodule "public/mks/mks-Taster"]
    	path = public/mks/parts/mks-Taster
    	url = https://github.com/Make-Your-School/mks-Taster.git
    [submodule "public/mks/parts/mks-Grove125KHzRFIDReader-V1.0"]
    	path = public/mks/parts/mks-Grove125KHzRFIDReader-V1.0
    	url = https://github.com/Make-Your-School/mks-Grove125KHzRFIDReader-V1.0.git

    the first is working
    the second ist split up:

    [Object: null prototype] {
      'submodule "public/mks/mks-Taster"': [Object: null prototype] {
        path: 'public/mks/parts/mks-Taster',
        url: 'https://github.com/Make-Your-School/mks-Taster.git'
      },
      'submodule "public/mks/parts/mks-Grove125KHzRFIDReader-V1': [Object: null prototype] {
        '0"': [Object: null prototype] {
          path: 'public/mks/parts/mks-Grove125KHzRFIDReader-V1.0',
          url: 'https://github.com/Make-Your-School/mks-Grove125KHzRFIDReader-V1.0.git'
        }
      }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions