github.com/go-spring/spring-base@v1.1.3/log/field_value.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package log
    18  
    19  // Value represents a data and encodes it to an Encoder.
    20  type Value interface {
    21  	Encode(enc Encoder) error
    22  }
    23  
    24  // BoolValue represents a bool carried by Field.
    25  type BoolValue bool
    26  
    27  // Encode encodes the data represented by v to an Encoder.
    28  func (v BoolValue) Encode(enc Encoder) error {
    29  	return enc.AppendBool(bool(v))
    30  }
    31  
    32  // Int64Value represents a int64 carried by Field.
    33  type Int64Value int64
    34  
    35  // Encode encodes the data represented by v to an Encoder.
    36  func (v Int64Value) Encode(enc Encoder) error {
    37  	return enc.AppendInt64(int64(v))
    38  }
    39  
    40  // Uint64Value represents a uint64 carried by Field.
    41  type Uint64Value uint64
    42  
    43  // Encode encodes the data represented by v to an Encoder.
    44  func (v Uint64Value) Encode(enc Encoder) error {
    45  	return enc.AppendUint64(uint64(v))
    46  }
    47  
    48  // Float64Value represents a float64 carried by Field.
    49  type Float64Value float64
    50  
    51  // Encode encodes the data represented by v to an Encoder.
    52  func (v Float64Value) Encode(enc Encoder) error {
    53  	return enc.AppendFloat64(float64(v))
    54  }
    55  
    56  // StringValue represents a string carried by Field.
    57  type StringValue string
    58  
    59  // Encode encodes the data represented by v to an Encoder.
    60  func (v StringValue) Encode(enc Encoder) error {
    61  	return enc.AppendString(string(v))
    62  }
    63  
    64  // ReflectValue represents an interface{} carried by Field.
    65  type ReflectValue struct {
    66  	Val interface{}
    67  }
    68  
    69  // Encode encodes the data represented by v to an Encoder.
    70  func (v ReflectValue) Encode(enc Encoder) error {
    71  	return enc.AppendReflect(v.Val)
    72  }
    73  
    74  // BoolsValue represents a slice of bool carried by Field.
    75  type BoolsValue []bool
    76  
    77  // Encode encodes the data represented by v to an Encoder.
    78  func (v BoolsValue) Encode(enc Encoder) error {
    79  	err := enc.AppendArrayBegin()
    80  	if err != nil {
    81  		return err
    82  	}
    83  	for _, val := range v {
    84  		err = enc.AppendBool(val)
    85  		if err != nil {
    86  			return err
    87  		}
    88  	}
    89  	return enc.AppendArrayEnd()
    90  }
    91  
    92  // IntsValue represents a slice of int carried by Field.
    93  type IntsValue []int
    94  
    95  // Encode encodes the data represented by v to an Encoder.
    96  func (v IntsValue) Encode(enc Encoder) error {
    97  	err := enc.AppendArrayBegin()
    98  	if err != nil {
    99  		return err
   100  	}
   101  	for _, val := range v {
   102  		err = enc.AppendInt64(int64(val))
   103  		if err != nil {
   104  			return err
   105  		}
   106  	}
   107  	return enc.AppendArrayEnd()
   108  }
   109  
   110  // Int8sValue represents a slice of int8 carried by Field.
   111  type Int8sValue []int8
   112  
   113  // Encode encodes the data represented by v to an Encoder.
   114  func (v Int8sValue) Encode(enc Encoder) error {
   115  	err := enc.AppendArrayBegin()
   116  	if err != nil {
   117  		return err
   118  	}
   119  	for _, val := range v {
   120  		err = enc.AppendInt64(int64(val))
   121  		if err != nil {
   122  			return err
   123  		}
   124  	}
   125  	return enc.AppendArrayEnd()
   126  }
   127  
   128  // Int16sValue represents a slice of int16 carried by Field.
   129  type Int16sValue []int16
   130  
   131  // Encode encodes the data represented by v to an Encoder.
   132  func (v Int16sValue) Encode(enc Encoder) error {
   133  	err := enc.AppendArrayBegin()
   134  	if err != nil {
   135  		return err
   136  	}
   137  	for _, val := range v {
   138  		err = enc.AppendInt64(int64(val))
   139  		if err != nil {
   140  			return err
   141  		}
   142  	}
   143  	return enc.AppendArrayEnd()
   144  }
   145  
   146  // Int32sValue represents a slice of int32 carried by Field.
   147  type Int32sValue []int32
   148  
   149  // Encode encodes the data represented by v to an Encoder.
   150  func (v Int32sValue) Encode(enc Encoder) error {
   151  	err := enc.AppendArrayBegin()
   152  	if err != nil {
   153  		return err
   154  	}
   155  	for _, val := range v {
   156  		err = enc.AppendInt64(int64(val))
   157  		if err != nil {
   158  			return err
   159  		}
   160  	}
   161  	return enc.AppendArrayEnd()
   162  }
   163  
   164  // Int64sValue represents a slice of int64 carried by Field.
   165  type Int64sValue []int64
   166  
   167  // Encode encodes the data represented by v to an Encoder.
   168  func (v Int64sValue) Encode(enc Encoder) error {
   169  	err := enc.AppendArrayBegin()
   170  	if err != nil {
   171  		return err
   172  	}
   173  	for _, val := range v {
   174  		err = enc.AppendInt64(val)
   175  		if err != nil {
   176  			return err
   177  		}
   178  	}
   179  	return enc.AppendArrayEnd()
   180  }
   181  
   182  // UintsValue represents a slice of uint carried by Field.
   183  type UintsValue []uint
   184  
   185  // Encode encodes the data represented by v to an Encoder.
   186  func (v UintsValue) Encode(enc Encoder) error {
   187  	err := enc.AppendArrayBegin()
   188  	if err != nil {
   189  		return err
   190  	}
   191  	for _, val := range v {
   192  		err = enc.AppendUint64(uint64(val))
   193  		if err != nil {
   194  			return err
   195  		}
   196  	}
   197  	return enc.AppendArrayEnd()
   198  }
   199  
   200  // Uint8sValue represents a slice of uint8 carried by Field.
   201  type Uint8sValue []uint8
   202  
   203  // Encode encodes the data represented by v to an Encoder.
   204  func (v Uint8sValue) Encode(enc Encoder) error {
   205  	err := enc.AppendArrayBegin()
   206  	if err != nil {
   207  		return err
   208  	}
   209  	for _, val := range v {
   210  		err = enc.AppendUint64(uint64(val))
   211  		if err != nil {
   212  			return err
   213  		}
   214  	}
   215  	return enc.AppendArrayEnd()
   216  }
   217  
   218  // Uint16sValue represents a slice of uint16 carried by Field.
   219  type Uint16sValue []uint16
   220  
   221  // Encode encodes the data represented by v to an Encoder.
   222  func (v Uint16sValue) Encode(enc Encoder) error {
   223  	err := enc.AppendArrayBegin()
   224  	if err != nil {
   225  		return err
   226  	}
   227  	for _, val := range v {
   228  		err = enc.AppendUint64(uint64(val))
   229  		if err != nil {
   230  			return err
   231  		}
   232  	}
   233  	return enc.AppendArrayEnd()
   234  }
   235  
   236  // Uint32sValue represents a slice of uint32 carried by Field.
   237  type Uint32sValue []uint32
   238  
   239  // Encode encodes the data represented by v to an Encoder.
   240  func (v Uint32sValue) Encode(enc Encoder) error {
   241  	err := enc.AppendArrayBegin()
   242  	if err != nil {
   243  		return err
   244  	}
   245  	for _, val := range v {
   246  		err = enc.AppendUint64(uint64(val))
   247  		if err != nil {
   248  			return err
   249  		}
   250  	}
   251  	return enc.AppendArrayEnd()
   252  }
   253  
   254  // Uint64sValue represents a slice of uint64 carried by Field.
   255  type Uint64sValue []uint64
   256  
   257  // Encode encodes the data represented by v to an Encoder.
   258  func (v Uint64sValue) Encode(enc Encoder) error {
   259  	err := enc.AppendArrayBegin()
   260  	if err != nil {
   261  		return err
   262  	}
   263  	for _, val := range v {
   264  		err = enc.AppendUint64(val)
   265  		if err != nil {
   266  			return err
   267  		}
   268  	}
   269  	return enc.AppendArrayEnd()
   270  }
   271  
   272  // Float32sValue represents a slice of float32 carried by Field.
   273  type Float32sValue []float32
   274  
   275  // Encode encodes the data represented by v to an Encoder.
   276  func (v Float32sValue) Encode(enc Encoder) error {
   277  	err := enc.AppendArrayBegin()
   278  	if err != nil {
   279  		return err
   280  	}
   281  	for _, val := range v {
   282  		err = enc.AppendFloat64(float64(val))
   283  		if err != nil {
   284  			return err
   285  		}
   286  	}
   287  	return enc.AppendArrayEnd()
   288  }
   289  
   290  // Float64sValue represents a slice of float64 carried by Field.
   291  type Float64sValue []float64
   292  
   293  // Encode encodes the data represented by v to an Encoder.
   294  func (v Float64sValue) Encode(enc Encoder) error {
   295  	err := enc.AppendArrayBegin()
   296  	if err != nil {
   297  		return err
   298  	}
   299  	for _, val := range v {
   300  		err = enc.AppendFloat64(val)
   301  		if err != nil {
   302  			return err
   303  		}
   304  	}
   305  	return enc.AppendArrayEnd()
   306  }
   307  
   308  // StringsValue represents a slice of string carried by Field.
   309  type StringsValue []string
   310  
   311  // Encode encodes the data represented by v to an Encoder.
   312  func (v StringsValue) Encode(enc Encoder) error {
   313  	err := enc.AppendArrayBegin()
   314  	if err != nil {
   315  		return err
   316  	}
   317  	for _, val := range v {
   318  		err = enc.AppendString(val)
   319  		if err != nil {
   320  			return err
   321  		}
   322  	}
   323  	return enc.AppendArrayEnd()
   324  }
   325  
   326  // ObjectValue represents a slice of Field carried by Field.
   327  type ObjectValue []Field
   328  
   329  // Encode encodes the data represented by v to an Encoder.
   330  func (v ObjectValue) Encode(enc Encoder) error {
   331  	err := enc.AppendObjectBegin()
   332  	if err != nil {
   333  		return err
   334  	}
   335  	for _, f := range v {
   336  		err = enc.AppendKey(f.Key)
   337  		if err != nil {
   338  			return err
   339  		}
   340  		err = f.Val.Encode(enc)
   341  		if err != nil {
   342  			return err
   343  		}
   344  	}
   345  	return enc.AppendObjectEnd()
   346  }
   347  
   348  // ArrayValue represents a slice of Value carried by Field.
   349  type ArrayValue []Value
   350  
   351  // Encode encodes the data represented by v to an Encoder.
   352  func (v ArrayValue) Encode(enc Encoder) error {
   353  	err := enc.AppendArrayBegin()
   354  	if err != nil {
   355  		return err
   356  	}
   357  	for _, val := range v {
   358  		err = val.Encode(enc)
   359  		if err != nil {
   360  			return err
   361  		}
   362  	}
   363  	return enc.AppendArrayEnd()
   364  }