mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-07 12:56:47 +00:00
e89e6244e0
Temporarily remove go.mod, since we can't generate an accurate one until the corresponding v1 change is submitted. Change-Id: I1e1ad97f2b455e33f61ffaeb8676289795e47e72 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177000 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Package mapsort provides sorted access to maps.
|
|
package mapsort
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
// Range iterates over every map entry in sorted key order,
|
|
// calling f for each key and value encountered.
|
|
func Range(mapv protoreflect.Map, keyKind protoreflect.Kind, f func(protoreflect.MapKey, protoreflect.Value) bool) {
|
|
var keys []protoreflect.MapKey
|
|
mapv.Range(func(key protoreflect.MapKey, _ protoreflect.Value) bool {
|
|
keys = append(keys, key)
|
|
return true
|
|
})
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
switch keyKind {
|
|
case protoreflect.BoolKind:
|
|
return !keys[i].Bool() && keys[j].Bool()
|
|
case protoreflect.Int32Kind,
|
|
protoreflect.Sint32Kind,
|
|
protoreflect.Sfixed32Kind,
|
|
protoreflect.Int64Kind,
|
|
protoreflect.Sint64Kind,
|
|
protoreflect.Sfixed64Kind:
|
|
return keys[i].Int() < keys[j].Int()
|
|
case protoreflect.Uint32Kind,
|
|
protoreflect.Fixed32Kind,
|
|
protoreflect.Uint64Kind,
|
|
protoreflect.Fixed64Kind:
|
|
return keys[i].Uint() < keys[j].Uint()
|
|
default:
|
|
return keys[i].String() < keys[j].String()
|
|
}
|
|
})
|
|
for _, key := range keys {
|
|
if !f(key, mapv.Get(key)) {
|
|
break
|
|
}
|
|
}
|
|
}
|