-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
adyusupov
committed
May 27, 2024
1 parent
dd7cff7
commit ed4a873
Showing
4 changed files
with
62 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,25 @@ | ||
package clog | ||
|
||
import "log/slog" | ||
import ( | ||
"log/slog" | ||
"reflect" | ||
) | ||
|
||
type fieldKey string | ||
|
||
type fields map[fieldKey]interface{} | ||
type Fields map[fieldKey]interface{} | ||
|
||
// convertToAttrs converts a map of custom fields to a slice of slog.Attr | ||
func convertToAttrs(fields fields) []any { | ||
// ConvertToAttrs converts a map of custom fields to a slice of slog.Attr | ||
func ConvertToAttrs(fields Fields) []any { | ||
var attrs []any | ||
for k, v := range fields { | ||
attrs = append(attrs, slog.Any(string(k), v)) | ||
if v != nil && !isZeroValue(v) { | ||
attrs = append(attrs, slog.Any(string(k), v)) | ||
} | ||
} | ||
return attrs | ||
} | ||
|
||
func isZeroValue(v interface{}) bool { | ||
return v == reflect.Zero(reflect.TypeOf(v)).Interface() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters