golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/trace/value.go (about)

     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  // Code generated by "gen.bash" from internal/trace/v2; DO NOT EDIT.
     6  
     7  //go:build go1.21
     8  
     9  package trace
    10  
    11  import "fmt"
    12  
    13  // Value is a dynamically-typed value obtained from a trace.
    14  type Value struct {
    15  	kind   ValueKind
    16  	scalar uint64
    17  }
    18  
    19  // ValueKind is the type of a dynamically-typed value from a trace.
    20  type ValueKind uint8
    21  
    22  const (
    23  	ValueBad ValueKind = iota
    24  	ValueUint64
    25  )
    26  
    27  // Kind returns the ValueKind of the value.
    28  //
    29  // It represents the underlying structure of the value.
    30  //
    31  // New ValueKinds may be added in the future. Users of this type must be robust
    32  // to that possibility.
    33  func (v Value) Kind() ValueKind {
    34  	return v.kind
    35  }
    36  
    37  // Uint64 returns the uint64 value for a MetricSampleUint64.
    38  //
    39  // Panics if this metric sample's Kind is not MetricSampleUint64.
    40  func (v Value) Uint64() uint64 {
    41  	if v.kind != ValueUint64 {
    42  		panic("Uint64 called on Value of a different Kind")
    43  	}
    44  	return v.scalar
    45  }
    46  
    47  // valueAsString produces a debug string value.
    48  //
    49  // This isn't just Value.String because we may want to use that to store
    50  // string values in the future.
    51  func valueAsString(v Value) string {
    52  	switch v.Kind() {
    53  	case ValueUint64:
    54  		return fmt.Sprintf("Uint64(%d)", v.scalar)
    55  	}
    56  	return "Bad"
    57  }