value_120.go raw

   1  // Copyright 2022 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  //go:build !go1.21
   6  
   7  package slog
   8  
   9  import (
  10  	"time"
  11  
  12  	"golang.org/x/exp/slog"
  13  )
  14  
  15  // A Value can represent any Go value, but unlike type any,
  16  // it can represent most small values without an allocation.
  17  // The zero Value corresponds to nil.
  18  type Value = slog.Value
  19  
  20  // Kind is the kind of a Value.
  21  type Kind = slog.Kind
  22  
  23  // The following list is sorted alphabetically, but it's also important that
  24  // KindAny is 0 so that a zero Value represents nil.
  25  const (
  26  	KindAny       = slog.KindAny
  27  	KindBool      = slog.KindBool
  28  	KindDuration  = slog.KindDuration
  29  	KindFloat64   = slog.KindFloat64
  30  	KindInt64     = slog.KindInt64
  31  	KindString    = slog.KindString
  32  	KindTime      = slog.KindTime
  33  	KindUint64    = slog.KindUint64
  34  	KindGroup     = slog.KindGroup
  35  	KindLogValuer = slog.KindLogValuer
  36  )
  37  
  38  //////////////// Constructors
  39  
  40  // StringValue returns a new Value for a string.
  41  func StringValue(value string) Value {
  42  	return slog.StringValue(value)
  43  }
  44  
  45  // IntValue returns a Value for an int.
  46  func IntValue(v int) Value {
  47  	return slog.IntValue(v)
  48  }
  49  
  50  // Int64Value returns a Value for an int64.
  51  func Int64Value(v int64) Value {
  52  	return slog.Int64Value(v)
  53  }
  54  
  55  // Uint64Value returns a Value for a uint64.
  56  func Uint64Value(v uint64) Value {
  57  	return slog.Uint64Value(v)
  58  }
  59  
  60  // Float64Value returns a Value for a floating-point number.
  61  func Float64Value(v float64) Value {
  62  	return slog.Float64Value(v)
  63  }
  64  
  65  // BoolValue returns a Value for a bool.
  66  func BoolValue(v bool) Value {
  67  	return slog.BoolValue(v)
  68  }
  69  
  70  // TimeValue returns a Value for a time.Time.
  71  // It discards the monotonic portion.
  72  func TimeValue(v time.Time) Value {
  73  	return slog.TimeValue(v)
  74  }
  75  
  76  // DurationValue returns a Value for a time.Duration.
  77  func DurationValue(v time.Duration) Value {
  78  	return slog.DurationValue(v)
  79  }
  80  
  81  // GroupValue returns a new Value for a list of Attrs.
  82  // The caller must not subsequently mutate the argument slice.
  83  func GroupValue(as ...Attr) Value {
  84  	return slog.GroupValue(as...)
  85  }
  86  
  87  // AnyValue returns a Value for the supplied value.
  88  //
  89  // If the supplied value is of type Value, it is returned
  90  // unmodified.
  91  //
  92  // Given a value of one of Go's predeclared string, bool, or
  93  // (non-complex) numeric types, AnyValue returns a Value of kind
  94  // String, Bool, Uint64, Int64, or Float64. The width of the
  95  // original numeric type is not preserved.
  96  //
  97  // Given a time.Time or time.Duration value, AnyValue returns a Value of kind
  98  // KindTime or KindDuration. The monotonic time is not preserved.
  99  //
 100  // For nil, or values of all other types, including named types whose
 101  // underlying type is numeric, AnyValue returns a value of kind KindAny.
 102  func AnyValue(v any) Value {
 103  	return slog.AnyValue(v)
 104  }
 105  
 106  // A LogValuer is any Go value that can convert itself into a Value for logging.
 107  //
 108  // This mechanism may be used to defer expensive operations until they are
 109  // needed, or to expand a single value into a sequence of components.
 110  type LogValuer = slog.LogValuer
 111