ini icon indicating copy to clipboard operation
ini copied to clipboard

Fails to parse multiline fields with blank lines

Open subpop opened this issue 6 years ago • 2 comments

Please give general description of the problem

Fails to parse multiline notes key from http://builder.libguestfs.org/index.

Please provide code snippets to reproduce the problem described above

package main

import (
	"fmt"
	"net/http"
	
	"gopkg.in/ini.v1"
)

func main() {
	res, err := http.Get("http://builder.libguestfs.org/index")
	if err != nil {
		fmt.Println(err)
	}
	defer res.Body.Close()

	cfg, err := ini.LoadSources(ini.LoadOptions{
		AllowPythonMultilineValues: true,
	}, res.Body)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(cfg.SectionStrings())
}

Do you have any suggestion to fix the problem?

I traced the parser to parser.go#L297 and suspect it has something to do with not matching the pythonMultiline regex.

This can be worked around (provided you don't need the value in the multi-line field) by setting SkipUnrecognizableLines to false.

subpop avatar Sep 10 '19 19:09 subpop

I was able to reproduce this failure by adding a blank (but still indented) line to the multiline tests.

diff --git a/ini_test.go b/ini_test.go
index 5e87be0..9ae58ea 100644
--- a/ini_test.go
+++ b/ini_test.go
@@ -517,6 +517,7 @@ key = test value <span style="color: %s\; background: %s">more text</span>
 long_rsa_private_key = -----BEGIN RSA PRIVATE KEY-----
   foo
   bar
+
   foobar
   barfoo
   -----END RSA PRIVATE KEY-----
@@ -528,7 +529,7 @@ multiline_list =
                                So(err, ShouldBeNil)
                                So(f, ShouldNotBeNil)
 
-                               So(f.Section("long").Key("long_rsa_private_key").String(), ShouldEqual, "-----BEGIN RSA PRIVATE KEY-----\nfoo\nbar\nfoobar\nbarfoo\n-----END RSA PRIVATE KEY-----")
+                               So(f.Section("long").Key("long_rsa_private_key").String(), ShouldEqual, "-----BEGIN RSA PRIVATE KEY-----\nfoo\nbar\n \nfoobar\nbarfoo\n-----END RSA PRIVATE KEY-----")
                                So(f.Section("long").Key("multiline_list").String(), ShouldEqual, "\nfirst\nsecond\nthird")
                        })

subpop avatar Sep 10 '19 20:09 subpop

replace the "" to `` in ini files.

hugh2632 avatar Apr 07 '20 04:04 hugh2632