github.com/google/cloudprober@v0.11.3/metrics/string.go (about)

     1  // Copyright 2017-2019 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package metrics
    16  
    17  import "errors"
    18  
    19  // String implements a value type with string storage.
    20  // It satisfies the Value interface.
    21  type String struct {
    22  	s string
    23  }
    24  
    25  // NewString returns a new String with the given string value.
    26  func NewString(s string) String {
    27  	return String{s: s}
    28  }
    29  
    30  // Add isn't supported for the String type, this is only to satisfy the Value
    31  // interface.
    32  func (s String) Add(val Value) error {
    33  	return errors.New("string value type doesn't support Add() operation")
    34  }
    35  
    36  // SubtractCounter isn't supported for the String type, this is only to satisfy
    37  // the Value interface.
    38  func (s String) SubtractCounter(val Value) (bool, error) {
    39  	return false, errors.New("string value type doesn't support SubtractCounter() operation")
    40  }
    41  
    42  // AddInt64 generates a panic for the String type. This is added only to satisfy
    43  // the Value interface.
    44  func (s String) AddInt64(i int64) {
    45  	panic("String type doesn't implement AddInt64()")
    46  }
    47  
    48  // AddFloat64 generates a panic for the String type. This is added only to
    49  // satisfy the Value interface.
    50  func (s String) AddFloat64(f float64) {
    51  	panic("String type doesn't implement AddFloat64()")
    52  }
    53  
    54  // String simply returns the stored string.
    55  func (s String) String() string {
    56  	return "\"" + s.s + "\""
    57  }
    58  
    59  // Clone returns the copy of receiver String.
    60  func (s String) Clone() Value {
    61  	return String{s: s.s}
    62  }
    63  
    64  // IsString checks if the given value is a string.
    65  func IsString(v Value) bool {
    66  	if v == nil {
    67  		return false
    68  	}
    69  	_, ok := v.(String)
    70  	return ok
    71  }