-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
68 lines (53 loc) · 1.15 KB
/
create.go
File metadata and controls
68 lines (53 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package dnstxtjwt
import (
"fmt"
)
type create struct {
maxSize int
maxLineLength int
}
func (c create) split(buf []byte) (result []string, n int) {
for idx := 0; len(buf) > 0; idx++ {
var max int
switch {
case len(result) > 999:
max = c.maxLineLength - len("9999:")
case len(result) > 99:
max = c.maxLineLength - len("999:")
default:
max = c.maxLineLength - len("99:")
}
if len(buf) < max {
max = len(buf)
}
s := string(buf[:max])
buf = buf[max:]
line := fmt.Sprintf("%02d:%s", idx, s)
result = append(result, line)
n += len(line)
}
return result, n
}
type CreateOption interface {
apply(*create)
}
func CreateRecord(jwt string, opts ...CreateOption) ([]string, error) {
var c create
defaults := []CreateOption{ // nolint:prealloc
WithMaxSize(0),
WithMaxLineLength(0),
}
opts = append(defaults, opts...)
for _, opt := range opts {
if opt != nil {
opt.apply(&c)
}
}
lines, n := c.split([]byte(jwt))
if n > c.maxSize {
return nil, ErrInvalidInput
}
return lines, nil
}