github.com/openshift-online/ocm-sdk-go@v0.1.473/internal/helpers.go (about)

     1  /*
     2  Copyright (c) 2018 Red Hat, Inc.
     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 internal
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net/http"
    23  	"net/url"
    24  	"time"
    25  )
    26  
    27  // AddValue creates the given set of query parameters if needed, an then adds
    28  // the given parameter.
    29  func AddValue(query *url.Values, name string, value interface{}) {
    30  	if *query == nil {
    31  		*query = make(url.Values)
    32  	}
    33  	query.Add(name, fmt.Sprintf("%v", value))
    34  }
    35  
    36  // CopyQuery creates a copy of the given set of query parameters.
    37  func CopyQuery(query url.Values) url.Values {
    38  	if query == nil {
    39  		return nil
    40  	}
    41  	result := make(url.Values)
    42  	for name, values := range query {
    43  		result[name] = CopyValues(values)
    44  	}
    45  	return result
    46  }
    47  
    48  // AddHeader creates the given set of headers if needed, and then adds the given
    49  // header:
    50  func AddHeader(header *http.Header, name string, value interface{}) {
    51  	if *header == nil {
    52  		*header = make(http.Header)
    53  	}
    54  	header.Add(name, fmt.Sprintf("%v", value))
    55  }
    56  
    57  // CopyHeader creates a copy of the given set of headers.
    58  func CopyHeader(header http.Header) http.Header {
    59  	if header == nil {
    60  		return nil
    61  	}
    62  	result := make(http.Header)
    63  	for name, values := range header {
    64  		result[name] = CopyValues(values)
    65  	}
    66  	return result
    67  }
    68  
    69  // CopyValues copies a slice of strings.
    70  func CopyValues(values []string) []string {
    71  	if values == nil {
    72  		return nil
    73  	}
    74  	result := make([]string, len(values))
    75  	copy(result, values)
    76  	return result
    77  }
    78  
    79  // SetTimeout creates the given context, if needed, and sets the given timeout.
    80  func SetTimeout(ctx *context.Context, cancel *context.CancelFunc, timeout time.Duration) {
    81  	if *ctx == nil {
    82  		*ctx = context.Background()
    83  	}
    84  	*ctx, *cancel = context.WithTimeout(*ctx, timeout)
    85  }
    86  
    87  // SetDeadline creates the given context, if needed, and sets the given deadline.
    88  func SetDeadline(ctx *context.Context, cancel *context.CancelFunc, deadline time.Time) {
    89  	if *ctx == nil {
    90  		*ctx = context.Background()
    91  	}
    92  	*ctx, *cancel = context.WithDeadline(*ctx, deadline)
    93  }