Any方法会根据value的类型返回不同的Field,如果value没有实现zapcore.ObjectMarshaler、zapcore.ArrayMarshaler,也不是基础类型,则走的是默认的Reflect(key, val)
Reflectzap@v1.16.0/field.go
func Reflect(key string, val interface{}) Field { return Field{Key: key, Type: zapcore.ReflectType, Interface: val}}Reflect创建的Field类型的Type为zapcore.ReflectType
AddTozap@v1.16.0/zapcore/field.go
func (f Field) AddTo(enc ObjectEncoder) { var err error switch f.Type { case ArrayMarshalerType: err = enc.AddArray(f.Key, f.Interface.(ArrayMarshaler)) case ObjectMarshalerType: err = enc.AddObject(f.Key, f.Interface.(ObjectMarshaler)) case BinaryType: enc.AddBinary(f.Key, f.Interface.([]byte)) case BoolType: enc.AddBool(f.Key, f.Integer == 1) case ByteStringType: enc.AddByteString(f.Key, f.Interface.([]byte)) case Complex128Type: enc.AddComplex128(f.Key, f.Interface.(complex128)) case Complex64Type: enc.AddComplex64(f.Key, f.Interface.(complex64)) case DurationType: enc.AddDuration(f.Key, time.Duration(f.Integer)) case Float64Type: enc.AddFloat64(f.Key, math.Float64frombits(uint64(f.Integer))) case Float32Type: enc.AddFloat32(f.Key, math.Float32frombits(uint32(f.Integer))) case Int64Type: enc.AddInt64(f.Key, f.Integer) case Int32Type: enc.AddInt32(f.Key, int32(f.Integer)) case Int16Type: enc.AddInt16(f.Key, int16(f.Integer)) case Int8Type: enc.AddInt8(f.Key, int8(f.Integer)) case StringType: enc.AddString(f.Key, f.String) case TimeType: if f.Interface != nil { enc.AddTime(f.Key, time.Unix(0, f.Integer).In(f.Interface.(*time.Location))) } else { // Fall back to UTC if location is nil. enc.AddTime(f.Key, time.Unix(0, f.Integer)) } case TimeFullType: enc.AddTime(f.Key, f.Interface.(time.Time)) case Uint64Type: enc.AddUint64(f.Key, uint64(f.Integer)) case Uint32Type: enc.AddUint32(f.Key, uint32(f.Integer)) case Uint16Type: enc.AddUint16(f.Key, uint16(f.Integer)) case Uint8Type: enc.AddUint8(f.Key, uint8(f.Integer)) case UintptrType: enc.AddUintptr(f.Key, uintptr(f.Integer)) case ReflectType: err = enc.AddReflected(f.Key, f.Interface) case NamespaceType: enc.OpenNamespace(f.Key) case StringerType: err = encodeStringer(f.Key, f.Interface, enc) case ErrorType: encodeError(f.Key, f.Interface.(error), enc) case SkipType: break default: panic(fmt.Sprintf("unknown field type: %v", f)) } if err != nil { enc.AddString(fmt.Sprintf("%sError", f.Key), err.Error()) }}
推荐阅读
-
-
-
买它|李佳琦新歌《买它》上线,歌词简直太魔性!买它买它!
-
带翅膀的蜗牛|虽然学历不高,但人品智商都在线,你喜欢“大林子”郭麒麟吗?
-
-
-
情感|女婿给岳父母养老是趋势,3个女婿:有时是岳父母不习惯
-
[广州恒大]重获新生!恒大功勋转会新东家迎来爆发,未来将再次坐稳主力!
-
-
-
-
人民日报客户端上海频道|为装修房屋,一男子竟将路面护栏偷回家
-
「PanDa杂谈」报应来了,给美国捐6亿!对武汉一分未捐?“伪国人”被刨祖坟
-
董路|刚刚!董路爆料:京鲁大战下半场主裁是带着亏欠心理找平衡
-
人民网|国际观察:“7分钟胡扯” 失尽“大国样子”
-
『黄忠』王者荣耀5.7日小乔丁香结迎来最后一次返场,黄忠新皮肤提前上架
-
【华为荣耀】华为正式启动新一轮系统推送:49款机型全覆盖、有你的手机吗?
-
-
-