github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/util/util.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"reflect"
    21  	"sort"
    22  
    23  	"github.com/hyperledger/fabric/common/util"
    24  )
    25  
    26  // GetSortedKeys returns the keys of the map in a sorted order. This function assumes that the keys are string
    27  func GetSortedKeys(m interface{}) []string {
    28  	mapVal := reflect.ValueOf(m)
    29  	keyVals := mapVal.MapKeys()
    30  	keys := []string{}
    31  	for _, keyVal := range keyVals {
    32  		keys = append(keys, keyVal.String())
    33  	}
    34  	sort.Strings(keys)
    35  	return keys
    36  }
    37  
    38  // GetValuesBySortedKeys returns the values of the map (mapPtr) in the list (listPtr) in the sorted order of key of the map
    39  // This function assumes that the mapPtr is a pointer to a map and listPtr is is a pointer to a list. Further type of keys of the
    40  // map are assumed to be string and the types of the values of the maps and the list are same
    41  func GetValuesBySortedKeys(mapPtr interface{}, listPtr interface{}) {
    42  	mapVal := reflect.ValueOf(mapPtr).Elem()
    43  	keyVals := mapVal.MapKeys()
    44  	if len(keyVals) == 0 {
    45  		return
    46  	}
    47  	keys := make(keys, len(keyVals))
    48  	for i, k := range keyVals {
    49  		keys[i] = newKey(k)
    50  	}
    51  	sort.Sort(keys)
    52  	out := reflect.ValueOf(listPtr).Elem()
    53  	for _, k := range keys {
    54  		val := mapVal.MapIndex(k.Value)
    55  		out.Set(reflect.Append(out, val))
    56  	}
    57  }
    58  
    59  type key struct {
    60  	reflect.Value
    61  	str string
    62  }
    63  
    64  type keys []*key
    65  
    66  func newKey(v reflect.Value) *key {
    67  	return &key{v, v.String()}
    68  }
    69  
    70  func (keys keys) Len() int {
    71  	return len(keys)
    72  }
    73  
    74  func (keys keys) Swap(i, j int) {
    75  	keys[i], keys[j] = keys[j], keys[i]
    76  }
    77  
    78  func (keys keys) Less(i, j int) bool {
    79  	return keys[i].str < keys[j].str
    80  }
    81  
    82  // ComputeStringHash computes the hash of the given string
    83  func ComputeStringHash(input string) []byte {
    84  	return ComputeHash([]byte(input))
    85  }
    86  
    87  // ComputeHash computes the hash of the given bytes
    88  func ComputeHash(input []byte) []byte {
    89  	return util.ComputeSHA256(input)
    90  }