|
| 1 | +package log |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "slices" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// StructContextOption defines options for StructContext |
| 12 | +type StructContextOption func(*structContext) |
| 13 | + |
| 14 | +// WithPrefix adds a prefix to all struct field names |
| 15 | +func WithPrefix(prefix string) StructContextOption { |
| 16 | + return func(sc *structContext) { |
| 17 | + sc.prefix = prefix |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// WithTag adds a custom tag to look for in struct fields |
| 22 | +func WithTag(tag string) StructContextOption { |
| 23 | + return func(sc *structContext) { |
| 24 | + sc.tag = tag |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// structContext implements the Context interface for struct fields |
| 29 | +type structContext struct { |
| 30 | + fields map[string]Valuer |
| 31 | + prefix string |
| 32 | + tag string |
| 33 | +} |
| 34 | + |
| 35 | +// Context returns a map of field names to Valuer implementations |
| 36 | +func (sc *structContext) Context() map[string]Valuer { |
| 37 | + return sc.fields |
| 38 | +} |
| 39 | + |
| 40 | +// StructContext creates a Context from a struct, extracting fields tagged with `log` |
| 41 | +// It supports nested structs and respects omitempty directive |
| 42 | +func StructContext(v interface{}, opts ...StructContextOption) Context { |
| 43 | + sc := &structContext{ |
| 44 | + fields: make(map[string]Valuer), |
| 45 | + prefix: "", |
| 46 | + tag: "log", |
| 47 | + } |
| 48 | + |
| 49 | + // Apply options |
| 50 | + for _, opt := range opts { |
| 51 | + opt(sc) |
| 52 | + } |
| 53 | + |
| 54 | + if v == nil { |
| 55 | + return sc |
| 56 | + } |
| 57 | + |
| 58 | + value := reflect.ValueOf(v) |
| 59 | + extractFields(value, sc, "") |
| 60 | + |
| 61 | + return sc |
| 62 | +} |
| 63 | + |
| 64 | +// extractFields recursively extracts fields from a struct value |
| 65 | +func extractFields(value reflect.Value, sc *structContext, path string) { |
| 66 | + // If it's a pointer, dereference it |
| 67 | + if value.Kind() == reflect.Ptr { |
| 68 | + if value.IsNil() { |
| 69 | + return |
| 70 | + } |
| 71 | + value = value.Elem() |
| 72 | + } |
| 73 | + |
| 74 | + // Only process structs |
| 75 | + if value.Kind() != reflect.Struct { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + typ := value.Type() |
| 80 | + for i := range typ.NumField() { |
| 81 | + field := typ.Field(i) |
| 82 | + fieldValue := value.Field(i) |
| 83 | + |
| 84 | + // Skip unexported fields |
| 85 | + if !field.IsExported() { |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + // Get the log tag |
| 90 | + tag := field.Tag.Get(sc.tag) |
| 91 | + if tag == "" { |
| 92 | + // Skip fields without log tag |
| 93 | + continue |
| 94 | + } |
| 95 | + |
| 96 | + // Parse the tag |
| 97 | + tagParts := strings.Split(tag, ",") |
| 98 | + fieldName := tagParts[0] |
| 99 | + if fieldName == "" { |
| 100 | + fieldName = field.Name |
| 101 | + } |
| 102 | + |
| 103 | + // Handle omitempty |
| 104 | + omitEmpty := slices.Contains(tagParts, "omitempty") |
| 105 | + |
| 106 | + // Build the full field name with path and prefix |
| 107 | + fullName := fieldName |
| 108 | + if path != "" { |
| 109 | + fullName = path + "." + fieldName |
| 110 | + } |
| 111 | + |
| 112 | + // we add prefis only once, for the field on the first level |
| 113 | + if path == "" && sc.prefix != "" { |
| 114 | + fullName = sc.prefix + "." + fullName |
| 115 | + } |
| 116 | + |
| 117 | + // Check if field should be omitted due to empty value |
| 118 | + if omitEmpty && fieldValue.IsZero() { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + // Store the field value |
| 123 | + valuer := valueToValuer(fieldValue) |
| 124 | + if valuer != nil { |
| 125 | + sc.fields[fullName] = valuer |
| 126 | + } |
| 127 | + |
| 128 | + // If it's a struct, recursively extract its fields only if it has a log tag |
| 129 | + if fieldValue.Kind() == reflect.Struct || |
| 130 | + (fieldValue.Kind() == reflect.Ptr && !fieldValue.IsNil() && fieldValue.Elem().Kind() == reflect.Struct) { |
| 131 | + extractFields(fieldValue, sc, fullName) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// valueToValuer converts a reflect.Value to a Valuer |
| 137 | +func valueToValuer(v reflect.Value) Valuer { |
| 138 | + if !v.IsValid() { |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + //nolint:exhaustive |
| 143 | + switch v.Kind() { |
| 144 | + case reflect.Bool: |
| 145 | + return Bool(v.Bool()) |
| 146 | + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 147 | + return Int64(v.Int()) |
| 148 | + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 149 | + return Uint64(v.Uint()) |
| 150 | + case reflect.Float32: |
| 151 | + return Float32(float32(v.Float())) |
| 152 | + case reflect.Float64: |
| 153 | + return Float64(v.Float()) |
| 154 | + case reflect.String: |
| 155 | + return String(v.String()) |
| 156 | + case reflect.Ptr: |
| 157 | + if v.IsNil() { |
| 158 | + return &any{nil} |
| 159 | + } |
| 160 | + return valueToValuer(v.Elem()) |
| 161 | + case reflect.Struct: |
| 162 | + // Check if it's a time.Time |
| 163 | + if v.Type().String() == "time.Time" { |
| 164 | + if v.CanInterface() { |
| 165 | + t, ok := v.Interface().(time.Time) |
| 166 | + if ok { |
| 167 | + return Time(t) |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // Try to use Stringer for complex types |
| 174 | + if v.CanInterface() { |
| 175 | + if stringer, ok := v.Interface().(fmt.Stringer); ok { |
| 176 | + return Stringer(stringer) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Return as string representation for other types |
| 181 | + return String(fmt.Sprintf("%v", v.Interface())) |
| 182 | +} |
0 commit comments