You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

84 lines
1.9 KiB

  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. package model
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. )
  18. // Value is a generic interface for values resulting from a query evaluation.
  19. type Value interface {
  20. Type() ValueType
  21. String() string
  22. }
  23. func (Matrix) Type() ValueType { return ValMatrix }
  24. func (Vector) Type() ValueType { return ValVector }
  25. func (*Scalar) Type() ValueType { return ValScalar }
  26. func (*String) Type() ValueType { return ValString }
  27. type ValueType int
  28. const (
  29. ValNone ValueType = iota
  30. ValScalar
  31. ValVector
  32. ValMatrix
  33. ValString
  34. )
  35. // MarshalJSON implements json.Marshaler.
  36. func (et ValueType) MarshalJSON() ([]byte, error) {
  37. return json.Marshal(et.String())
  38. }
  39. func (et *ValueType) UnmarshalJSON(b []byte) error {
  40. var s string
  41. if err := json.Unmarshal(b, &s); err != nil {
  42. return err
  43. }
  44. switch s {
  45. case "<ValNone>":
  46. *et = ValNone
  47. case "scalar":
  48. *et = ValScalar
  49. case "vector":
  50. *et = ValVector
  51. case "matrix":
  52. *et = ValMatrix
  53. case "string":
  54. *et = ValString
  55. default:
  56. return fmt.Errorf("unknown value type %q", s)
  57. }
  58. return nil
  59. }
  60. func (e ValueType) String() string {
  61. switch e {
  62. case ValNone:
  63. return "<ValNone>"
  64. case ValScalar:
  65. return "scalar"
  66. case ValVector:
  67. return "vector"
  68. case ValMatrix:
  69. return "matrix"
  70. case ValString:
  71. return "string"
  72. }
  73. panic("ValueType.String: unhandled value type")
  74. }