Skip to content

Commit

Permalink
Refactor parse json
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cataldo committed Dec 12, 2023
1 parent 7f1cac9 commit a24c80d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ go-logger
=================
<img align="right" src="gopher-logger.png" alt="">

[![Project status](https://img.shields.io/badge/version-v1.0.2-vividgreen.svg)](https://github.com/GabrielHCataldo/go-logger/releases/tag/v1.0.2)
[![Project status](https://img.shields.io/badge/version-v1.0.3-vividgreen.svg)](https://github.com/GabrielHCataldo/go-logger/releases/tag/v1.0.2)
[![Go Report Card](https://goreportcard.com/badge/github.com/GabrielHCataldo/go-logger)](https://goreportcard.com/report/github.com/GabrielHCataldo/go-logger)
[![Coverage Status](https://coveralls.io/repos/GabrielHCataldo/go-logger/badge.svg?branch=main&service=github)](https://coveralls.io/github/GabrielHCataldo/go-logger?branch=main)
[![Open Source Helpers](https://www.codetriage.com/gabrielhcataldo/go-logger/badges/users.svg)](https://www.codetriage.com/gabrielhcataldo/go-logger)
Expand Down
19 changes: 11 additions & 8 deletions logger/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,25 @@ func prepareStructMsg(t reflect.Type, v reflect.Value, sub bool, tag string) any
fieldValue := v.Field(i)
fieldStruct := t.Field(i)
fieldTag := tag
if fieldValue.Kind() == reflect.Pointer || fieldValue.Kind() == reflect.Interface {
fieldValue = fieldValue.Elem()
fieldName := util.GetJsonNameByTag(fieldStruct.Tag.Get("json"))
if len(fieldName) == 0 {
fieldName = fieldStruct.Name
}
if len(fieldTag) == 0 {
fieldTag = fieldStruct.Tag.Get("logger")
}
jsonName := util.GetJsonNameByTag(fieldStruct.Tag.Get("json"))
if util.IsNilValueReflect(fieldValue) {
if !util.ContainsJsonOmitemptyByTag(fieldStruct.Tag.Get("json")) {
result.Set(jsonName, nil)
result.Set(fieldName, nil)
}
continue
}
if len(fieldTag) == 0 {
fieldTag = fieldStruct.Tag.Get("logger")
if fieldValue.Kind() == reflect.Pointer || fieldValue.Kind() == reflect.Interface {
fieldValue = fieldValue.Elem()
}
fieldValueProcessed := getValueByReflect(fieldValue.Type(), fieldValue, fieldTag)
if len(jsonName) != 0 && fieldValueProcessed != nil {
result.Set(jsonName, fieldValueProcessed)
if len(fieldName) != 0 && fieldValueProcessed != nil {
result.Set(fieldName, fieldValueProcessed)
}
}
if sub {
Expand Down

0 comments on commit a24c80d

Please sign in to comment.