go.uber.org/cadence@v1.2.9/internal/common/util/util.go (about)

     1  // Copyright (c) 2017-2020 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package util
    22  
    23  import (
    24  	"reflect"
    25  	"sync"
    26  	"time"
    27  )
    28  
    29  // MergeDictoRight copies the contents of src to dest
    30  func MergeDictoRight(src map[string]string, dest map[string]string) {
    31  	for k, v := range src {
    32  		dest[k] = v
    33  	}
    34  }
    35  
    36  // MergeDicts creates a union of the two dicts
    37  func MergeDicts(dic1 map[string]string, dic2 map[string]string) (resultDict map[string]string) {
    38  	resultDict = make(map[string]string)
    39  	MergeDictoRight(dic1, resultDict)
    40  	MergeDictoRight(dic2, resultDict)
    41  	return
    42  }
    43  
    44  // AwaitWaitGroup calls Wait on the given wait
    45  // Returns true if the Wait() call succeeded before the timeout
    46  // Returns false if the Wait() did not return before the timeout
    47  func AwaitWaitGroup(wg *sync.WaitGroup, timeout time.Duration) bool {
    48  
    49  	doneC := make(chan struct{})
    50  
    51  	go func() {
    52  		wg.Wait()
    53  		close(doneC)
    54  	}()
    55  
    56  	select {
    57  	case <-doneC:
    58  		return true
    59  	case <-time.After(timeout):
    60  		return false
    61  	}
    62  }
    63  
    64  var typeOfByteSlice = reflect.TypeOf(([]byte)(nil))
    65  
    66  // IsTypeByteSlice checks whether the type passed in is a ByteSlice type
    67  func IsTypeByteSlice(inType reflect.Type) bool {
    68  	return inType == typeOfByteSlice || inType == reflect.PtrTo(typeOfByteSlice)
    69  }