blob: cf9a95c7ba81049ecf9bc39cc4b94076833bc51b [file] [log] [blame]
divyadesai81bb7ba2020-03-11 11:45:23 +00001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package kvstore
17
18import (
19 "fmt"
20 "time"
21)
22
23// GetDuration converts a timeout value from int to duration. If the timeout value is
24// either not set of -ve then we default KV timeout (configurable) is used.
25func GetDuration(timeout int) time.Duration {
26 if timeout <= 0 {
27 return defaultKVGetTimeout * time.Second
28 }
29 return time.Duration(timeout) * time.Second
30}
31
32// ToString converts an interface value to a string. The interface should either be of
33// a string type or []byte. Otherwise, an error is returned.
34func ToString(value interface{}) (string, error) {
35 switch t := value.(type) {
36 case []byte:
37 return string(value.([]byte)), nil
38 case string:
39 return value.(string), nil
40 default:
41 return "", fmt.Errorf("unexpected-type-%T", t)
42 }
43}
44
45// ToByte converts an interface value to a []byte. The interface should either be of
46// a string type or []byte. Otherwise, an error is returned.
47func ToByte(value interface{}) ([]byte, error) {
48 switch t := value.(type) {
49 case []byte:
50 return value.([]byte), nil
51 case string:
52 return []byte(value.(string)), nil
53 default:
54 return nil, fmt.Errorf("unexpected-type-%T", t)
55 }
56}