...

Source file src/github.com/prometheus/common/model/value_type.go

Documentation: github.com/prometheus/common/model

     1  // Copyright 2013 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package model
    15  
    16  import (
    17  	"encoding/json"
    18  	"fmt"
    19  )
    20  
    21  // Value is a generic interface for values resulting from a query evaluation.
    22  type Value interface {
    23  	Type() ValueType
    24  	String() string
    25  }
    26  
    27  func (Matrix) Type() ValueType  { return ValMatrix }
    28  func (Vector) Type() ValueType  { return ValVector }
    29  func (*Scalar) Type() ValueType { return ValScalar }
    30  func (*String) Type() ValueType { return ValString }
    31  
    32  type ValueType int
    33  
    34  const (
    35  	ValNone ValueType = iota
    36  	ValScalar
    37  	ValVector
    38  	ValMatrix
    39  	ValString
    40  )
    41  
    42  // MarshalJSON implements json.Marshaler.
    43  func (et ValueType) MarshalJSON() ([]byte, error) {
    44  	return json.Marshal(et.String())
    45  }
    46  
    47  func (et *ValueType) UnmarshalJSON(b []byte) error {
    48  	var s string
    49  	if err := json.Unmarshal(b, &s); err != nil {
    50  		return err
    51  	}
    52  	switch s {
    53  	case "<ValNone>":
    54  		*et = ValNone
    55  	case "scalar":
    56  		*et = ValScalar
    57  	case "vector":
    58  		*et = ValVector
    59  	case "matrix":
    60  		*et = ValMatrix
    61  	case "string":
    62  		*et = ValString
    63  	default:
    64  		return fmt.Errorf("unknown value type %q", s)
    65  	}
    66  	return nil
    67  }
    68  
    69  func (e ValueType) String() string {
    70  	switch e {
    71  	case ValNone:
    72  		return "<ValNone>"
    73  	case ValScalar:
    74  		return "scalar"
    75  	case ValVector:
    76  		return "vector"
    77  	case ValMatrix:
    78  		return "matrix"
    79  	case ValString:
    80  		return "string"
    81  	}
    82  	panic("ValueType.String: unhandled value type")
    83  }
    84  

View as plain text