Source file src/internal/trace/value.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package trace
     6  
     7  import "fmt"
     8  
     9  // Value is a dynamically-typed value obtained from a trace.
    10  type Value struct {
    11  	kind   ValueKind
    12  	scalar uint64
    13  }
    14  
    15  // ValueKind is the type of a dynamically-typed value from a trace.
    16  type ValueKind uint8
    17  
    18  const (
    19  	ValueBad ValueKind = iota
    20  	ValueUint64
    21  )
    22  
    23  // Kind returns the ValueKind of the value.
    24  //
    25  // It represents the underlying structure of the value.
    26  //
    27  // New ValueKinds may be added in the future. Users of this type must be robust
    28  // to that possibility.
    29  func (v Value) Kind() ValueKind {
    30  	return v.kind
    31  }
    32  
    33  // Uint64 returns the uint64 value for a MetricSampleUint64.
    34  //
    35  // Panics if this metric sample's Kind is not MetricSampleUint64.
    36  func (v Value) Uint64() uint64 {
    37  	if v.kind != ValueUint64 {
    38  		panic("Uint64 called on Value of a different Kind")
    39  	}
    40  	return v.scalar
    41  }
    42  
    43  // valueAsString produces a debug string value.
    44  //
    45  // This isn't just Value.String because we may want to use that to store
    46  // string values in the future.
    47  func valueAsString(v Value) string {
    48  	switch v.Kind() {
    49  	case ValueUint64:
    50  		return fmt.Sprintf("Uint64(%d)", v.scalar)
    51  	}
    52  	return "Bad"
    53  }
    54  

View as plain text