Skip to content

Commit 9041939

Browse files
committed
feat(pkg/unique): add Generate func
1 parent 166a220 commit 9041939

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

pkg/unique/unique.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,23 @@ import (
77
"strings"
88
)
99

10-
func FromStruct(data interface{}) (*string, error) {
10+
// Generate creates a unique hash from a slice of interface{} values.
11+
func Generate(data ...interface{}) string {
12+
var values []string
13+
14+
for d := range data {
15+
values = append(values, toString(d))
16+
}
17+
18+
combined := strings.Join(values, ",")
19+
hash := sha256.Sum256([]byte(combined))
20+
return hex.EncodeToString(hash[:])
21+
}
22+
23+
// FromStruct generates a unique key from a struct based on fields tagged with "libnuke:uniqueKey".
24+
func FromStruct(data interface{}) *string {
1125
if data == nil {
12-
return nil, nil
26+
return nil
1327
}
1428

1529
v := reflect.ValueOf(data)
@@ -18,10 +32,10 @@ func FromStruct(data interface{}) (*string, error) {
1832
}
1933

2034
if v.Kind() != reflect.Struct {
21-
return nil, nil
35+
return nil
2236
}
2337

24-
var values []string
38+
var values []interface{}
2539
for i := 0; i < v.NumField(); i++ {
2640
field := v.Type().Field(i)
2741
tag := field.Tag.Get("libnuke")
@@ -34,18 +48,15 @@ func FromStruct(data interface{}) (*string, error) {
3448
continue
3549
}
3650

37-
value := valueField.Interface()
38-
values = append(values, toString(value))
51+
values = append(values, valueField.Interface())
3952
}
4053

4154
if len(values) == 0 {
42-
return nil, nil
55+
return nil
4356
}
4457

45-
combined := strings.Join(values, ",")
46-
hash := sha256.Sum256([]byte(combined))
47-
result := hex.EncodeToString(hash[:])
48-
return &result, nil
58+
hash := Generate(values)
59+
return &hash
4960
}
5061

5162
// toString converts interface{} to string for basic types

pkg/unique/unique_test.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ func TestFromStruct(t *testing.T) {
7171

7272
for _, tt := range tests {
7373
t.Run(tt.name, func(t *testing.T) {
74-
key, err := FromStruct(tt.input)
75-
if err != nil {
76-
t.Errorf("unexpected error: %v", err)
77-
}
74+
key := FromStruct(tt.input)
7875
if tt.unique && (key == nil || *key == "") {
7976
t.Errorf("expected unique key, got nil or empty string")
8077
}
@@ -91,10 +88,7 @@ func TestFromStruct(t *testing.T) {
9188
Name string
9289
Other string
9390
}
94-
key, err := FromStruct(noTagStruct{ID: "1", Name: "2", Other: "3"})
95-
if err != nil {
96-
t.Errorf("unexpected error: %v", err)
97-
}
91+
key := FromStruct(noTagStruct{ID: "1", Name: "2", Other: "3"})
9892
if key != nil {
9993
t.Errorf("expected nil for struct with no uniqueKey tags, got: %v", *key)
10094
}
@@ -106,10 +100,7 @@ func TestFromStruct(t *testing.T) {
106100
Name string
107101
Other string
108102
}
109-
key, err := FromStruct(testStructNoTags{ID: "1", Name: "2", Other: "3"})
110-
if err != nil {
111-
t.Errorf("unexpected error: %v", err)
112-
}
103+
key := FromStruct(testStructNoTags{ID: "1", Name: "2", Other: "3"})
113104
if key != nil {
114105
t.Errorf("expected nil for struct with no uniqueKey tags, got: %v", *key)
115106
}
@@ -142,30 +133,21 @@ func TestFromStruct_Pointer(t *testing.T) {
142133
ID string `libnuke:"uniqueKey"`
143134
}
144135
val := &testStruct{ID: "abc"}
145-
key, err := FromStruct(val)
146-
if err != nil {
147-
t.Errorf("unexpected error: %v", err)
148-
}
136+
key := FromStruct(val)
149137
if key == nil || *key == "" {
150138
t.Error("expected unique key for pointer struct, got nil or empty string")
151139
}
152140
}
153141

154142
func TestFromStruct_NonStruct(t *testing.T) {
155-
key, err := FromStruct(123)
156-
if err != nil {
157-
t.Errorf("unexpected error: %v", err)
158-
}
143+
key := FromStruct(123)
159144
if key != nil {
160145
t.Errorf("expected nil for non-struct, got: %v", *key)
161146
}
162147
}
163148

164149
func TestFromStruct_Nil(t *testing.T) {
165-
key, err := FromStruct(nil)
166-
if err != nil {
167-
t.Errorf("unexpected error: %v", err)
168-
}
150+
key := FromStruct(nil)
169151
if key != nil {
170152
t.Errorf("expected nil for nil, got: %v", *key)
171153
}

0 commit comments

Comments
 (0)