blob: 64e7d302034876324e9fb34e01e6d22621fc4a2a [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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
Neha Sharma130ac6d2020-04-08 08:46:32 +000018import "fmt"
Scott Baker2c1c4822019-10-16 11:02:41 -070019
20// ToString converts an interface value to a string. The interface should either be of
21// a string type or []byte. Otherwise, an error is returned.
22func ToString(value interface{}) (string, error) {
23 switch t := value.(type) {
24 case []byte:
25 return string(value.([]byte)), nil
26 case string:
27 return value.(string), nil
28 default:
29 return "", fmt.Errorf("unexpected-type-%T", t)
30 }
31}
32
33// ToByte converts an interface value to a []byte. The interface should either be of
34// a string type or []byte. Otherwise, an error is returned.
35func ToByte(value interface{}) ([]byte, error) {
36 switch t := value.(type) {
37 case []byte:
38 return value.([]byte), nil
39 case string:
40 return []byte(value.(string)), nil
41 default:
42 return nil, fmt.Errorf("unexpected-type-%T", t)
43 }
44}