Skip to content

Commit 3b1aa47

Browse files
committed
refactor: moved some helper functions to github.com/ralvarezdev/go-strings
1 parent 2d674d0 commit 3b1aa47

7 files changed

Lines changed: 26 additions & 135 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.24.0
44

55
require (
66
github.com/ralvarezdev/go-reflect v0.2.13
7-
github.com/ralvarezdev/go-strings v0.1.17
7+
github.com/ralvarezdev/go-strings v0.2.0
88
google.golang.org/genproto/googleapis/rpc v0.0.0-20251020155222-88f65dc88635
99
)
1010

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/ralvarezdev/go-strings v0.1.16 h1:0yc7yamYEWVrFTK3C95IjSE5IqCs3H1alII
1616
github.com/ralvarezdev/go-strings v0.1.16/go.mod h1:8sFOqmPJpqzS7bTjf91EzUCITnwpmkfifwY80GxV5r8=
1717
github.com/ralvarezdev/go-strings v0.1.17 h1:HFFqe5fVqsYdaOsj+yoVtgIgkNXubDHSz5ubN8V6iW8=
1818
github.com/ralvarezdev/go-strings v0.1.17/go.mod h1:8sFOqmPJpqzS7bTjf91EzUCITnwpmkfifwY80GxV5r8=
19+
github.com/ralvarezdev/go-strings v0.2.0 h1:JJUbGRpsnmuoEaFVv+rTT3P4BTsMqGOzlLkQRdfbt6c=
20+
github.com/ralvarezdev/go-strings v0.2.0/go.mod h1:8sFOqmPJpqzS7bTjf91EzUCITnwpmkfifwY80GxV5r8=
1921
google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff h1:A90eA31Wq6HOMIQlLfzFwzqGKBTuaVztYu/g8sn+8Zc=
2022
google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk=
2123
google.golang.org/genproto/googleapis/rpc v0.0.0-20251020155222-88f65dc88635 h1:3uycTxukehWrxH4HtPRtn1PDABTU331ViDjyqrUbaog=

mapper/constants.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

mapper/errors.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
var (
88
ErrNilGenerator = errors.New("generator cannot be nil")
99
ErrNilMapper = errors.New("mapper cannot be nil")
10-
ErrProtobufTagNotFound = "missing protobuf tag for field: %s"
11-
ErrProtobufTagNameNotFound = "missing protobuf tag name for field: %s"
12-
ErrEmptyJSONTag = "empty json tag for field: %s"
1310
ErrNilStructInstance = errors.New("struct instance cannot be nil")
1411
ErrStructInstanceNotStruct = errors.New("struct instance must be a struct")
1512
ErrInvalidStructInstance = errors.New("invalid struct instance")

mapper/json.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
goreflect "github.com/ralvarezdev/go-reflect"
9+
gostringsjson "github.com/ralvarezdev/go-strings/json"
910
)
1011

1112
type (
@@ -51,6 +52,11 @@ func (j JSONGenerator) NewMapper(structInstance any) (
5152
*Mapper,
5253
error,
5354
) {
55+
// Check if the struct instance is nil
56+
if structInstance == nil {
57+
return nil, ErrNilStructInstance
58+
}
59+
5460
// Reflection of data
5561
reflectedType := goreflect.GetDereferencedType(structInstance)
5662

@@ -68,7 +74,6 @@ func (j JSONGenerator) NewMapper(structInstance any) (
6874
// Get the field type through reflection
6975
structField := reflectedType.Field(i)
7076
fieldType := structField.Type
71-
fieldTag := structField.Tag
7277
fieldName := structField.Name
7378

7479
// Check if the field is unexported
@@ -79,10 +84,13 @@ func (j JSONGenerator) NewMapper(structInstance any) (
7984
}
8085

8186
// Get the JSON tag of the field
82-
jsonTag := fieldTag.Get(JSONTag)
87+
jsonTag, err := gostringsjson.GetJSONTag(structField, fieldName)
88+
if err != nil {
89+
return nil, err
90+
}
8391

8492
// Get the JSON name from the tag
85-
jsonName, err := GetJSONTagName(jsonTag, fieldName)
93+
jsonName, err := gostringsjson.GetJSONTagName(jsonTag, fieldName)
8694
if err != nil {
8795
return nil, err
8896
}
@@ -91,7 +99,7 @@ func (j JSONGenerator) NewMapper(structInstance any) (
9199
rootMapper.AddFieldTagName(fieldName, jsonName)
92100

93101
// Check if the JSON tag is unassigned or if it contains 'omitempty', which means it is an optional field
94-
if jsonTag == "-" || strings.Contains(jsonTag, JSONOmitempty) {
102+
if jsonTag == "-" || strings.Contains(jsonTag, gostringsjson.JSONOmitempty) {
95103
// Set field name as not required
96104
rootMapper.SetFieldIsRequired(fieldName, false)
97105

mapper/protobuf.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"reflect"
66

77
goreflect "github.com/ralvarezdev/go-reflect"
8+
gostringsprotobuf "github.com/ralvarezdev/go-strings/protobuf"
89
)
910

1011
type (
@@ -49,7 +50,12 @@ func NewProtobufGenerator(logger *slog.Logger) *ProtobufGenerator {
4950
func (p ProtobufGenerator) NewMapper(structInstance any) (
5051
*Mapper,
5152
error,
52-
) {
53+
) {
54+
// Check if the struct instance is nil
55+
if structInstance == nil {
56+
return nil, ErrNilStructInstance
57+
}
58+
5359
// Reflection of data
5460
reflectedType := goreflect.GetDereferencedType(structInstance)
5561

@@ -70,20 +76,20 @@ func (p ProtobufGenerator) NewMapper(structInstance any) (
7076
fieldName := structField.Name
7177

7278
// Omit protobuf internal fields, protobuf oneof fields or just unexported fields
73-
if IsProtobufGeneratedField(fieldName) || IsProtobufOneOfField(structField) || !goreflect.IsStructFieldExported(structField) {
79+
if gostringsprotobuf.IsProtobufGeneratedField(fieldName) || gostringsprotobuf.IsProtobufOneOfField(structField) || !goreflect.IsStructFieldExported(structField) {
7480
// Set field as not required
7581
rootMapper.SetFieldIsRequired(fieldName, false)
7682
continue
7783
}
7884

7985
// Get the Protobuf tag of the field
80-
protobufTag, err := GetProtobufTag(structField, fieldName)
86+
protobufTag, err := gostringsprotobuf.GetProtobufTag(structField, fieldName)
8187
if err != nil {
8288
return nil, err
8389
}
8490

8591
// Get the field name from the Protobuf tag
86-
protobufName, err := GetProtobufTagName(protobufTag, fieldName)
92+
protobufName, err := gostringsprotobuf.GetProtobufTagName(protobufTag, fieldName)
8793
if err != nil {
8894
return nil, err
8995
}
@@ -98,7 +104,7 @@ func (p ProtobufGenerator) NewMapper(structInstance any) (
98104

99105
// Check if the element type is not a struct and the tag to determine if it contains 'oneof', which means it
100106
// is an optional struct field
101-
if fieldType.Kind() != reflect.Struct || IsProtobufFieldOptional(protobufTag) {
107+
if fieldType.Kind() != reflect.Struct || gostringsprotobuf.IsProtobufFieldOptional(protobufTag) {
102108
// Set field as not required
103109
rootMapper.SetFieldIsRequired(fieldName, false)
104110

mapper/utils.go

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)