github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/log/slog/value.go (about)

     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  package slog
     6  
     7  import (
     8  	"github.com/shogo82148/std/time"
     9  )
    10  
    11  // Valueは、任意のGo値を表すことができますが、type anyとは異なり、
    12  // 大部分の小さな値を割り当てなしで表現できます。
    13  // ゼロ値のValueはnilに対応します。
    14  type Value struct {
    15  	_ [0]func()
    16  	// num holds the value for Kinds Int64, Uint64, Float64, Bool and Duration,
    17  	// the string length for KindString, and nanoseconds since the epoch for KindTime.
    18  	num uint64
    19  	// If any is of type Kind, then the value is in num as described above.
    20  	// If any is of type *time.Location, then the Kind is Time and time.Time value
    21  	// can be constructed from the Unix nanos in num and the location (monotonic time
    22  	// is not preserved).
    23  	// If any is of type stringptr, then the Kind is String and the string value
    24  	// consists of the length in num and the pointer in any.
    25  	// Otherwise, the Kind is Any and any is the value.
    26  	// (This implies that Attrs cannot store values of type Kind, *time.Location
    27  	// or stringptr.)
    28  	any any
    29  }
    30  
    31  // Kindは [Value] の種類です。
    32  type Kind int
    33  
    34  const (
    35  	KindAny Kind = iota
    36  	KindBool
    37  	KindDuration
    38  	KindFloat64
    39  	KindInt64
    40  	KindString
    41  	KindTime
    42  	KindUint64
    43  	KindGroup
    44  	KindLogValuer
    45  )
    46  
    47  func (k Kind) String() string
    48  
    49  // Kindは、Valueの種類を返します。
    50  func (v Value) Kind() Kind
    51  
    52  // StringValueは、文字列の新しい [Value] を返します。
    53  func StringValue(value string) Value
    54  
    55  // IntValueは、intの [Value] を返します。
    56  func IntValue(v int) Value
    57  
    58  // Int64Valueは、int64の [Value] を返します。
    59  func Int64Value(v int64) Value
    60  
    61  // Uint64Valueは、uint64の [Value] を返します。
    62  func Uint64Value(v uint64) Value
    63  
    64  // Float64Valueは、浮動小数点数の [Value] を返します。
    65  func Float64Value(v float64) Value
    66  
    67  // BoolValueは、boolの [Value] を返します。
    68  func BoolValue(v bool) Value
    69  
    70  // TimeValueは、[time.Time] の [Value] を返します。
    71  // monotonic部分は破棄されます。
    72  func TimeValue(v time.Time) Value
    73  
    74  // DurationValueは、[time.Duration] の [Value] を返します。
    75  func DurationValue(v time.Duration) Value
    76  
    77  // GroupValueは、Attrのリストの新しい [Value] を返します。
    78  // 呼び出し元は、引数スライスを後で変更しないでください。
    79  func GroupValue(as ...Attr) Value
    80  
    81  // AnyValueは、提供された値の [Value] を返します。
    82  //
    83  // 提供された値がValue型の場合、変更されずに返されます。
    84  //
    85  // Goの事前宣言されたstring、bool、または(非複素)数値型のいずれかの値が与えられた場合、
    86  // AnyValueは、[KindString]、[KindBool]、[KindUint64]、[KindInt64]、または [KindFloat64] の種類のValueを返します。
    87  // 元の数値型の幅は保持されません。
    88  //
    89  // [time.Time] または [time.Duration] 値が与えられた場合、AnyValueは、[KindTime] または [KindDuration] のValueを返します。
    90  // monotonic timeは保持されません。
    91  //
    92  // nil、または数値型の基礎型である名前付き型を含む、すべての他の型の値の場合、
    93  // AnyValueは、[KindAny] の種類のValueを返します。
    94  func AnyValue(v any) Value
    95  
    96  // Anyは、vの値をanyとして返します。
    97  func (v Value) Any() any
    98  
    99  // Stringは、Valueの値を [fmt.Sprint] のようにフォーマットした文字列として返します。
   100  // Int64、Float64などのメソッドは、vが間違った種類の場合にpanicしますが、
   101  // Stringは決してpanicしません。
   102  func (v Value) String() string
   103  
   104  // Int64は、vの値をint64として返します。vが符号付き整数でない場合はpanicします。
   105  func (v Value) Int64() int64
   106  
   107  // Uint64は、vの値をuint64として返します。vが符号なし整数でない場合はpanicします。
   108  func (v Value) Uint64() uint64
   109  
   110  // Boolは、vの値をboolとして返します。vがboolでない場合はpanicします。
   111  func (v Value) Bool() bool
   112  
   113  // Durationは、vの値をtime.Durationとして返します。vが [time.Duration] でない場合はpanicします。
   114  func (a Value) Duration() time.Duration
   115  
   116  // Float64は、vの値をfloat64として返します。vがfloat64でない場合はpanicします。
   117  func (v Value) Float64() float64
   118  
   119  // Timeは、vの値をtime.Timeとして返します。vが [time.Time] でない場合はpanicします。
   120  func (v Value) Time() time.Time
   121  
   122  // LogValuerは、vの値をLogValuerとして返します。vがLogValuerでない場合はpanicします。
   123  func (v Value) LogValuer() LogValuer
   124  
   125  // Groupは、vの値を[]Attrとして返します。
   126  // vの [Kind] が [KindGroup] でない場合はpanicします。
   127  func (v Value) Group() []Attr
   128  
   129  // Equalは、vとwが同じGo値を表しているかどうかを報告します。
   130  func (v Value) Equal(w Value) bool
   131  
   132  // LogValuerは、自身をログ出力するためにValueに変換できる任意のGo値です。
   133  //
   134  // このメカニズムは、高価な操作を必要になるまで遅延させるために使用できます。
   135  // また、単一の値を複数のコンポーネントに展開するために使用できます。
   136  type LogValuer interface {
   137  	LogValue() Value
   138  }
   139  
   140  // Resolveは、vが [LogValuer] を実装している間、vのLogValueを繰り返し呼び出し、結果を返します。
   141  // vがグループに解決された場合、グループの属性の値は再帰的に解決されません。
   142  // LogValueの呼び出し回数が閾値を超えた場合、エラーを含むValueが返されます。
   143  // Resolveの戻り値は、[KindLogValuer] の種類ではないことが保証されています。
   144  func (v Value) Resolve() (rv Value)