Tcl Library Source Code

View Ticket
Login
Ticket UUID: d74e6561a29ca0dca4d6198a46aa9474e20e34ad
Title: YAML->HUDDLE->YAML fails
Type: Bug Version: current
Submitter: emmanuel Created on: 2017-09-28 21:37:24
Subsystem: yaml Assigned To: aku
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2021-03-22 16:16:52
Resolution: None Closed By: nobody
    Closed on:
Description:
Converting a YAML file to huddle and back again to YAML will not work. This is because the implementation of huddle2yaml does not pay attention to the internal types that are introduced by the YAML parser to support dictionaries and lists. The following piece of code solves it:

    proc ::yaml::_imp_huddle2yaml {data {offset ""}} {
        set nextoff "$offset[string repeat { } $yaml::_dumpIndent]"
        switch -glob -- [huddle type $data] {
            "int*" -
            "str*" {
                set data [huddle get_stripped $data]
                return [_dumpScalar $data $offset]
            }
            "sequence" -
            "list" {
                set inner {}
                set len [huddle llength $data]
                for {set i 0} {$i < $len} {incr i} {
                    set sub [huddle get $data $i]
                    set tsub [huddle type $sub]
                    set sep [expr {[string match "str*" $tsub] || [string match "int*" $tsub] ? " " : "\n"}]
                    lappend inner [join [list $offset - $sep [_imp_huddle2yaml $sub $nextoff]] ""]
                }
                return [join $inner "\n"]
            }
            "mapping" - 
            "dict" {
                set inner {}
                foreach {key} [huddle keys $data] {
                    set sub [huddle get $data $key]
                    set tsub [huddle type $sub]
                    set sep [expr {[string match "str*" $tsub] || [string match "int*" $tsub] ? " " : "\n"}]
                    lappend inner [join [list $offset $key: $sep [_imp_huddle2yaml $sub $nextoff]] ""]
                }
                return [join $inner "\n"]
            }
            default {
                return $data
            }
        }
    }
User Comments: aku added on 2021-03-22 16:16:52:
Hi Emmanuel. Do you have an example I could convert into a test case for this ?

jlichy added on 2020-03-31 21:02:29:
Also experienced this problem and confirmed proposed fix works