dict-iterator.go: fixed behaviour of NewDictIterator() with no args

This commit is contained in:
Celestino Amoroso 2026-04-23 22:06:23 +02:00
parent 9e4252173b
commit c39ee7cec0

View File

@ -68,40 +68,52 @@ func (it *DictIterator) makeKeys(m map[any]any, sort sortType) {
func NewDictIterator(dict *DictType, args []any) (it *DictIterator, err error) { func NewDictIterator(dict *DictType, args []any) (it *DictIterator, err error) {
var sortType = sortTypeNone var sortType = sortTypeNone
var s string var s string
var argAny any
it = &DictIterator{a: dict, count: 0, index: -1, keys: nil, iterMode: dictIterModeKeys} it = &DictIterator{a: dict, count: 0, index: -1, keys: nil, iterMode: dictIterModeKeys}
if len(args) > 0 { if len(args) > 0 {
if s, err = ToGoString(args[0], "sort type"); err == nil { argAny = args[0]
switch strings.ToLower(s) { } else {
case "a", "asc": argAny = "default"
sortType = sortTypeAsc }
case "d", "desc": if s, err = ToGoString(argAny, "sort type"); err == nil {
sortType = sortTypeDesc switch strings.ToLower(s) {
case "n", "none", "nosort", "no-sort": case "a", "asc":
sortType = sortTypeNone sortType = sortTypeAsc
case "", "default": case "d", "desc":
sortType = sortTypeDefault sortType = sortTypeDesc
default: case "n", "none", "nosort", "no-sort":
err = fmt.Errorf("invalid sort type %q", s) sortType = sortTypeNone
case "", "default":
sortType = sortTypeDefault
default:
err = fmt.Errorf("invalid sort type %q", s)
}
if err == nil {
if len(args) > 1 {
argAny = args[1]
} else {
argAny = "default"
} }
if err == nil && len(args) > 1 { if s, err = ToGoString(argAny, "iteration mode"); err == nil {
if s, err = ToGoString(args[1], "iteration mode"); err == nil { switch strings.ToLower(s) {
switch strings.ToLower(s) { case "k", "key", "keys":
case "k", "key", "keys": it.iterMode = dictIterModeKeys
it.iterMode = dictIterModeKeys case "v", "value", "values":
case "v", "value", "values": it.iterMode = dictIterModeValues
it.iterMode = dictIterModeValues case "i", "item", "items":
case "i", "item", "items": it.iterMode = dictIterModeItems
it.iterMode = dictIterModeItems case "", "default":
case "", "default": it.iterMode = dictIterModeKeys
it.iterMode = dictIterModeKeys default:
default: err = fmt.Errorf("invalid iteration mode %q", s)
err = fmt.Errorf("invalid iteration mode %q", s)
}
} }
} }
} }
} }
it.makeKeys(*dict, sortType) it.makeKeys(*dict, sortType)
return return
} }