Skip to content

Commit

Permalink
doc: Clarify contract for primitive encoders
Browse files Browse the repository at this point in the history
Clarify that custom primitive encoders for
level, time, duration, and caller
must make *exactly* one call to a `Append*` method
on the PrimitiveArrayEncoder provided.

Refs #1359
  • Loading branch information
abhinav committed Sep 16, 2023
1 parent f9c5b00 commit 308fd3f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const DefaultLineEnding = "\n"
const OmitKey = ""

// A LevelEncoder serializes a Level to a primitive type.
//
// This function must make exactly one call
// to a PrimitiveArrayEncoder's Append* method.
type LevelEncoder func(Level, PrimitiveArrayEncoder)

// LowercaseLevelEncoder serializes a Level to a lowercase string. For example,
Expand Down Expand Up @@ -90,6 +93,9 @@ func (e *LevelEncoder) UnmarshalText(text []byte) error {
}

// A TimeEncoder serializes a time.Time to a primitive type.
//
// This function must make exactly one call
// to a PrimitiveArrayEncoder's Append* method.
type TimeEncoder func(time.Time, PrimitiveArrayEncoder)

// EpochTimeEncoder serializes a time.Time to a floating-point number of seconds
Expand Down Expand Up @@ -219,6 +225,9 @@ func (e *TimeEncoder) UnmarshalJSON(data []byte) error {
}

// A DurationEncoder serializes a time.Duration to a primitive type.
//
// This function must make exactly one call
// to a PrimitiveArrayEncoder's Append* method.
type DurationEncoder func(time.Duration, PrimitiveArrayEncoder)

// SecondsDurationEncoder serializes a time.Duration to a floating-point number of seconds elapsed.
Expand Down Expand Up @@ -262,6 +271,9 @@ func (e *DurationEncoder) UnmarshalText(text []byte) error {
}

// A CallerEncoder serializes an EntryCaller to a primitive type.
//
// This function must make exactly one call
// to a PrimitiveArrayEncoder's Append* method.
type CallerEncoder func(EntryCaller, PrimitiveArrayEncoder)

// FullCallerEncoder serializes a caller in /full/path/to/package/file:line
Expand Down Expand Up @@ -292,6 +304,9 @@ func (e *CallerEncoder) UnmarshalText(text []byte) error {

// A NameEncoder serializes a period-separated logger name to a primitive
// type.
//
// This function must make exactly one call
// to a PrimitiveArrayEncoder's Append* method.
type NameEncoder func(string, PrimitiveArrayEncoder)

// FullNameEncoder serializes the logger name as-is.
Expand Down

0 comments on commit 308fd3f

Please sign in to comment.