Skip to content

Commit e070a8e

Browse files
jackspirouclaude
andcommitted
refactor: modernize Go code syntax
- Use min() function instead of manual comparisons - Use range n syntax instead of traditional for loops - Clean up whitespace and formatting These changes use Go 1.21+ features for cleaner, more idiomatic code. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 83353ac commit e070a8e

8 files changed

Lines changed: 116 additions & 123 deletions

File tree

apikey.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,7 @@ func generateEntropy(size numOfCrock32Chars) (string, error) {
307307
blake2bHasher.Write(inputBytes)
308308
hash := blake2bHasher.Sum(nil)
309309

310-
copyLen := numOfRandomBytes - pos
311-
if copyLen > len(hash) {
312-
copyLen = len(hash)
313-
}
310+
copyLen := min(numOfRandomBytes-pos, len(hash))
314311
copy(entropy[pos:], hash[:copyLen])
315312
inputBytes = hash
316313
}
@@ -320,10 +317,7 @@ func generateEntropy(size numOfCrock32Chars) (string, error) {
320317
entropyEncoded.Grow(int(size))
321318

322319
for i := 0; i < len(entropy); i += 8 {
323-
end := i + 8
324-
if end > len(entropy) {
325-
end = len(entropy)
326-
}
320+
end := min(i+8, len(entropy))
327321

328322
var n uint64
329323
for j, b := range entropy[i:end] {
@@ -333,16 +327,16 @@ func generateEntropy(size numOfCrock32Chars) (string, error) {
333327
// Convert uint64 to bytes for encoding
334328
var buf [8]byte
335329
binary.BigEndian.PutUint64(buf[:], n)
336-
330+
337331
// Find first non-zero byte
338332
start := 0
339-
for j := 0; j < 8; j++ {
333+
for j := range 8 {
340334
if buf[j] != 0 {
341335
start = j
342336
break
343337
}
344338
}
345-
339+
346340
// Encode using standard library
347341
encoded := crockford.EncodeToString(buf[start:])
348342
entropyEncoded.WriteString(encoded)

cmd/uuidkey/apikey_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ func TestAPIKeyCommand(t *testing.T) {
144144
t.Run(tt.name, func(t *testing.T) {
145145
// Use executeCommand which handles state reset
146146
output, err := executeCommand(rootCmd, tt.args...)
147-
147+
148148
if (err != nil) != tt.wantErr {
149149
t.Errorf("expected error: %v, got: %v (output: %s)", tt.wantErr, err, output)
150150
}
151-
151+
152152
if tt.check != nil && err == nil {
153153
tt.check(t, output)
154154
}
@@ -160,40 +160,40 @@ func TestAPIKeyGeneration(t *testing.T) {
160160
// Test that multiple generations produce different API keys
161161
t.Run("unique generation", func(t *testing.T) {
162162
keys := make(map[string]bool)
163-
164-
for i := 0; i < 10; i++ {
163+
164+
for range 10 {
165165
output, err := executeCommand(rootCmd, "apikey", "--prefix", "UNIQ", "-q")
166166
if err != nil {
167167
t.Fatalf("unexpected error: %v", err)
168168
}
169-
169+
170170
key := strings.TrimSpace(output)
171171
if keys[key] {
172172
t.Errorf("duplicate API key generated: %s", key)
173173
}
174174
keys[key] = true
175175
}
176176
})
177-
177+
178178
// Test different entropy levels produce different length keys
179179
t.Run("entropy levels", func(t *testing.T) {
180180
entropies := []int{128, 160, 256}
181181
lengths := make(map[int]int)
182-
182+
183183
for _, entropy := range entropies {
184184
output, err := executeCommand(rootCmd, "apikey", "--prefix", "ENT", "--entropy", fmt.Sprintf("%d", entropy), "-q")
185185
if err != nil {
186186
t.Fatalf("unexpected error for entropy %d: %v", entropy, err)
187187
}
188-
188+
189189
key := strings.TrimSpace(output)
190190
parts := strings.Split(key, "_")
191191
if len(parts) == 3 {
192192
// Middle part is key+entropy
193193
lengths[entropy] = len(parts[1])
194194
}
195195
}
196-
196+
197197
// Higher entropy should have longer keys
198198
// Note: The actual implementation concatenates key+entropy, so longer entropy = longer total
199199
})
@@ -260,11 +260,11 @@ func TestAPIKeyErrors(t *testing.T) {
260260
for _, tt := range tests {
261261
t.Run(tt.name, func(t *testing.T) {
262262
output, err := executeCommand(newRootCommand(), tt.args...)
263-
263+
264264
if (err != nil) != tt.wantErr {
265265
t.Errorf("expected error: %v, got: %v (output: %s)", tt.wantErr, err, output)
266266
}
267-
267+
268268
if tt.check != nil {
269269
tt.check(t, output)
270270
}
@@ -317,11 +317,11 @@ func TestAPIKeyEdgeCases(t *testing.T) {
317317
for _, tt := range tests {
318318
t.Run(tt.name, func(t *testing.T) {
319319
output, err := executeCommand(rootCmd, tt.args...)
320-
320+
321321
if (err != nil) != tt.wantErr {
322322
t.Errorf("expected error: %v, got: %v", tt.wantErr, err)
323323
}
324-
324+
325325
if tt.check != nil {
326326
tt.check(t, output)
327327
}
@@ -369,14 +369,14 @@ func TestAPIKeySpecificEntropyCases(t *testing.T) {
369369
for _, tt := range tests {
370370
t.Run(tt.name, func(t *testing.T) {
371371
output, err := executeCommand(newRootCommand(), tt.args...)
372-
372+
373373
if (err != nil) != tt.wantErr {
374374
t.Errorf("expected error: %v, got: %v (output: %s)", tt.wantErr, err, output)
375375
}
376-
376+
377377
if tt.check != nil {
378378
tt.check(t, output)
379379
}
380380
})
381381
}
382-
}
382+
}

cmd/uuidkey/uuid_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestUUIDCommand(t *testing.T) {
2020
// Should generate both UUID and key
2121
assertContains(t, output, "UUID:")
2222
assertContains(t, output, "Key:")
23-
23+
2424
// Extract UUID from output
2525
lines := strings.Split(output, "\n")
2626
for _, line := range lines {
@@ -142,11 +142,11 @@ func TestUUIDCommand(t *testing.T) {
142142
for _, tt := range tests {
143143
t.Run(tt.name, func(t *testing.T) {
144144
output, err := executeCommand(rootCmd, tt.args...)
145-
145+
146146
if (err != nil) != tt.wantErr {
147147
t.Errorf("expected error: %v, got: %v (output: %s)", tt.wantErr, err, output)
148148
}
149-
149+
150150
if tt.check != nil && err == nil {
151151
tt.check(t, output)
152152
}
@@ -158,25 +158,25 @@ func TestUUIDVersionGeneration(t *testing.T) {
158158
// Test that multiple generations produce different UUIDs
159159
t.Run("unique generation", func(t *testing.T) {
160160
uuids := make(map[string]bool)
161-
162-
for i := 0; i < 10; i++ {
161+
162+
for range 10 {
163163
output, err := executeCommand(rootCmd, "uuid", "-q")
164164
if err != nil {
165165
t.Fatalf("unexpected error: %v", err)
166166
}
167-
167+
168168
uuid := strings.TrimSpace(output)
169169
if uuids[uuid] {
170170
t.Errorf("duplicate UUID generated: %s", uuid)
171171
}
172172
uuids[uuid] = true
173173
}
174174
})
175-
175+
176176
// Test v6 is sortable
177177
t.Run("v6 sortable", func(t *testing.T) {
178178
// Generate multiple v6 UUIDs
179-
for i := 0; i < 5; i++ {
179+
for range 5 {
180180
output, err := executeCommand(rootCmd, "uuid", "--version", "6", "-q")
181181
if err != nil {
182182
t.Fatalf("unexpected error: %v", err)
@@ -187,7 +187,7 @@ func TestUUIDVersionGeneration(t *testing.T) {
187187
t.Errorf("expected v6 UUID, got %s", uuid)
188188
}
189189
}
190-
// Note: v6 UUIDs are sortable by time but might not always be strictly
190+
// Note: v6 UUIDs are sortable by time but might not always be strictly
191191
// increasing due to timing resolution and MAC address components
192192
})
193193
}
@@ -253,11 +253,11 @@ func TestUUIDErrors(t *testing.T) {
253253
for _, tt := range tests {
254254
t.Run(tt.name, func(t *testing.T) {
255255
output, err := executeCommand(newRootCommand(), tt.args...)
256-
256+
257257
if (err != nil) != tt.wantErr {
258258
t.Errorf("expected error: %v, got: %v", tt.wantErr, err)
259259
}
260-
260+
261261
if tt.check != nil {
262262
tt.check(t, output)
263263
}
@@ -374,11 +374,11 @@ func TestUUIDGenerateErrors(t *testing.T) {
374374
for _, tt := range tests {
375375
t.Run(tt.name, func(t *testing.T) {
376376
output, err := executeCommand(rootCmd, tt.args...)
377-
377+
378378
if (err != nil) != tt.wantErr {
379379
t.Errorf("expected error: %v, got: %v", tt.wantErr, err)
380380
}
381-
381+
382382
if tt.check != nil {
383383
tt.check(t, output)
384384
}
@@ -489,14 +489,14 @@ func TestUUIDSpecificCases(t *testing.T) {
489489
for _, tt := range tests {
490490
t.Run(tt.name, func(t *testing.T) {
491491
output, err := executeCommand(newRootCommand(), tt.args...)
492-
492+
493493
if (err != nil) != tt.wantErr {
494494
t.Errorf("expected error: %v, got: %v (output: %s)", tt.wantErr, err, output)
495495
}
496-
496+
497497
if tt.check != nil {
498498
tt.check(t, output)
499499
}
500500
})
501501
}
502-
}
502+
}

crockford.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ func crock32Encode(n uint32) string {
1515
if n == 0 {
1616
return "0"
1717
}
18-
18+
1919
// Use a fixed-size array to avoid allocations
2020
var buf [8]byte // Maximum size needed for uint32 in base32
2121
idx := len(buf)
22-
22+
2323
for n > 0 {
2424
idx--
2525
buf[idx] = digits[n%32]
2626
n /= 32
2727
}
28-
28+
2929
return string(buf[idx:])
3030
}
3131

@@ -37,12 +37,12 @@ var decodeTable = func() [256]byte {
3737
for i := range table {
3838
table[i] = 255
3939
}
40-
40+
4141
// Numbers 0-9
4242
for i := byte('0'); i <= '9'; i++ {
4343
table[i] = i - '0'
4444
}
45-
45+
4646
// Uppercase letters
4747
table['A'] = 10
4848
table['B'] = 11
@@ -70,22 +70,22 @@ var decodeTable = func() [256]byte {
7070
table['X'] = 29
7171
table['Y'] = 30
7272
table['Z'] = 31
73-
73+
7474
// Lowercase letters (same values as uppercase)
7575
for c := byte('a'); c <= 'z'; c++ {
7676
if table[c-'a'+'A'] != 255 {
7777
table[c] = table[c-'a'+'A']
7878
}
7979
}
80-
80+
8181
// Special mappings per Crockford spec
8282
table['O'] = 0
8383
table['o'] = 0
8484
table['I'] = 1
8585
table['i'] = 1
8686
table['L'] = 1
8787
table['l'] = 1
88-
88+
8989
return table
9090
}()
9191

@@ -94,21 +94,21 @@ func crock32Decode(s string) (uint32, error) {
9494
if len(s) == 0 {
9595
return 0, fmt.Errorf("crock32.Decode: empty string")
9696
}
97-
97+
9898
var result uint32
99-
for i := 0; i < len(s); i++ {
99+
for i := range len(s) {
100100
// Use lookup table for fast conversion
101101
digit := decodeTable[s[i]]
102102
if digit == 255 {
103103
return 0, fmt.Errorf("crock32.Decode: invalid character %c", s[i])
104104
}
105-
105+
106106
// Check for overflow before multiplication
107107
if result > (^uint32(0))/32 {
108108
return 0, fmt.Errorf("crock32.Decode: integer overflow")
109109
}
110110
result = result*32 + uint32(digit)
111111
}
112-
112+
113113
return result, nil
114-
}
114+
}

0 commit comments

Comments
 (0)