Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package kvstore |
| 17 | |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame^] | 18 | import "fmt" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 19 | |
| 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. |
| 22 | func 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. |
| 35 | func 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 | } |