Pretty Printing a Dictionary Instance
Not logged in

The following code will return a dictionary instance of a defined type in "pretty print" format:

###############################################################################
##                                                                           ##
##  Copyright (c) 2012, Gerald W. Lester                                     ##
##  All rights reserved.                                                     ##
##                                                                           ##
##  Redistribution and use in source and binary forms, with or without       ##
##  modification, are permitted provided that the following conditions       ##
##  are met:                                                                 ##
##                                                                           ##
##    * Redistributions of source code must retain the above copyright       ##
##      notice, this list of conditions and the following disclaimer.        ##
##    * Redistributions in binary form must reproduce the above              ##
##      copyright notice, this list of conditions and the following          ##
##      disclaimer in the documentation and/or other materials provided      ##
##      with the distribution.                                               ##
##    * Neither the name of the Visiprise Software, Inc nor the names        ##
##      of its contributors may be used to endorse or promote products       ##
##      derived from this software without specific prior written            ##
##      permission.                                                          ##
##                                                                           ##
##  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      ##
##  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        ##
##  LIMITED  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS       ##
##  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE           ##
##  COPYRIGHT OWNER OR  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,     ##
##  INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,    ##
##  BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;        ##
##  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER         ##
##  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT       ##
##  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  OTHERWISE) ARISING IN       ##
##  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  ADVISED OF THE         ##
##  POSSIBILITY OF SUCH DAMAGE.                                              ##
##                                                                           ##
###############################################################################


package require WS::Client 2.2.8
package require WS::Utils 2.2.8

proc PrettyPrintType {service type indent typeDict {indentStep 4}} {
    if {[string match {*()} $type]} {
        set isArray 1
        set type [string trim $type {()}]
    } else {
        set isArray 0
    }
    set typeDef [::WS::Utils::GetServiceTypeDef Client $service $type]
    if {[dict exists $typeDef type]} {
        set isSimple 1
    } else {
        set isSimple 0
        set typeDef [dict get $typeDef definition]
    }
    set result {}
    append indent [string repeat { } $indentStep]
    switch -exact -- [list $isSimple $isArray] {
        {1 0} {
            ##
            ## Simple non-array
            ##
            set result [format { == '%s'} $typeDict]
        }
        {1 1} {
            ##
            ## Simple array
            ##
            append result "\n"
            set index 0
            foreach value $typeDict {
                append result $indent [format {[%d] == '%s'} [incr index] $value] "\n"
            }
        }
        {0 0} {
            ##
            ## Complex non-array
            ##
            append result " -- "
            foreach element [dict keys $typeDef] {
                if {![dict exists $typeDict $element]} {
            ## Complex non-array
            ##
            append result " -- "
            foreach element [dict keys $typeDef] {
                if {![dict exists $typeDict $element]} {
                    continue
                }
                set elementType [dict get $typeDef $element type]
                append result "\n" $indent $element [PrettyPrintType $service $elementType $indent [dict get $typeDict $element] $indentStep]
            }
        }
        {0 1} {
            ##
            ## Complex array
            ##
            set index 0
            foreach value $typeDict {
                append result "\n" $indent [format {[%d] --} [incr index]]
                foreach element [dict keys $typeDef] {
                    if {![dict exists $value $element]} {
                        continue
                    }
                   set elementType [dict get $typeDef $element type]
                   append result "\n" $indent $element [PrettyPrintType $service $elementType $indent [dict get $value $element] $indentStep]
                }
            }
        }
    }
    return $result
}

proc PrettyPrintResult {service operation resultDict} {
    set resultTypeList [lindex [::WS::Client::GetOperationList $service $operation] end]
    set result {}
    foreach type $resultTypeList {
        append result [PrettyPrintType $service $type {} $resultDict] "\n"
    }
    return $result
}