Skip to content

Commit a43f5fe

Browse files
mruegCopilot
andcommitted
refactor: modernize Go primitives
- Replace interface{} with any (Go 1.18) across confluence/api.go, macro/macro.go, util/cli.go, util/error_handler.go, includes/templates.go - Replace sort.SliceStable with slices.SortStableFunc + cmp.Compare (Go 1.21) in attachment/attachment.go, consistent with existing slices usage - Replace fmt.Errorf("%s", msg) with errors.New(msg) in mark.go Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4a5f379 commit a43f5fe

7 files changed

Lines changed: 51 additions & 49 deletions

File tree

attachment/attachment.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
"net/url"
1414
"path"
1515
"path/filepath"
16-
"sort"
16+
"cmp"
17+
"slices"
1718
"strconv"
1819
"strings"
1920

@@ -235,8 +236,8 @@ func CompileAttachmentLinks(markdown []byte, attachments []Attachment) []byte {
235236
// attachments/a.jpg
236237
// attachments/a.jpg.jpg
237238
// so we replace longer and then shorter
238-
sort.SliceStable(replaces, func(i, j int) bool {
239-
return len(replaces[i]) > len(replaces[j])
239+
slices.SortStableFunc(replaces, func(a, b string) int {
240+
return cmp.Compare(len(b), len(a))
240241
})
241242

242243
for _, replace := range replaces {

confluence/api.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ type tracer struct {
9494
prefix string
9595
}
9696

97-
func (tracer *tracer) Printf(format string, args ...interface{}) {
97+
func (tracer *tracer) Printf(format string, args ...any) {
9898
log.Trace().Msgf(tracer.prefix+" "+format, args...)
9999
}
100100

@@ -485,29 +485,29 @@ func (api *API) CreatePage(
485485
title string,
486486
body string,
487487
) (*PageInfo, error) {
488-
payload := map[string]interface{}{
488+
payload := map[string]any{
489489
"type": pageType,
490490
"title": title,
491-
"space": map[string]interface{}{
491+
"space": map[string]any{
492492
"key": space,
493493
},
494-
"body": map[string]interface{}{
495-
"storage": map[string]interface{}{
494+
"body": map[string]any{
495+
"storage": map[string]any{
496496
"representation": "storage",
497497
"value": body,
498498
},
499499
},
500-
"metadata": map[string]interface{}{
501-
"properties": map[string]interface{}{
502-
"editor": map[string]interface{}{
500+
"metadata": map[string]any{
501+
"properties": map[string]any{
502+
"editor": map[string]any{
503503
"value": "v2",
504504
},
505505
},
506506
},
507507
}
508508

509509
if parent != nil {
510-
payload["ancestors"] = []map[string]interface{}{
510+
payload["ancestors"] = []map[string]any{
511511
{"id": parent.ID},
512512
}
513513
}
@@ -528,20 +528,20 @@ func (api *API) CreatePage(
528528

529529
func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, versionMessage string, appearance string, emojiString string) error {
530530
nextPageVersion := page.Version.Number + 1
531-
oldAncestors := []map[string]interface{}{}
531+
oldAncestors := []map[string]any{}
532532

533533
if page.Type != "blogpost" && len(page.Ancestors) > 0 {
534534
// picking only the last one, which is required by confluence
535-
oldAncestors = []map[string]interface{}{
535+
oldAncestors = []map[string]any{
536536
{"id": page.Ancestors[len(page.Ancestors)-1].ID},
537537
}
538538
}
539539

540-
properties := map[string]interface{}{
540+
properties := map[string]any{
541541
// Fix to set full-width as has changed on Confluence APIs again.
542542
// https://jira.atlassian.com/browse/CONFCLOUD-65447
543543
//
544-
"content-appearance-published": map[string]interface{}{
544+
"content-appearance-published": map[string]any{
545545
"value": appearance,
546546
},
547547
// content-appearance-draft should not be set as this is impacted by
@@ -555,37 +555,37 @@ func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, ve
555555
}
556556
unicodeHex := fmt.Sprintf("%x", r)
557557

558-
properties["emoji-title-draft"] = map[string]interface{}{
558+
properties["emoji-title-draft"] = map[string]any{
559559
"value": unicodeHex,
560560
}
561-
properties["emoji-title-published"] = map[string]interface{}{
561+
properties["emoji-title-published"] = map[string]any{
562562
"value": unicodeHex,
563563
}
564564
}
565565

566-
payload := map[string]interface{}{
566+
payload := map[string]any{
567567
"id": page.ID,
568568
"type": page.Type,
569569
"title": page.Title,
570-
"version": map[string]interface{}{
570+
"version": map[string]any{
571571
"number": nextPageVersion,
572572
"minorEdit": minorEdit,
573573
"message": versionMessage,
574574
},
575575
"ancestors": oldAncestors,
576-
"body": map[string]interface{}{
577-
"storage": map[string]interface{}{
576+
"body": map[string]any{
577+
"storage": map[string]any{
578578
"value": newContent,
579579
"representation": "storage",
580580
},
581581
},
582-
"metadata": map[string]interface{}{
582+
"metadata": map[string]any{
583583
"properties": properties,
584584
},
585585
}
586586

587587
request, err := api.rest.Res(
588-
"content/"+page.ID, &map[string]interface{}{},
588+
"content/"+page.ID, &map[string]any{},
589589
).Put(payload)
590590
if err != nil {
591591
return err
@@ -600,10 +600,10 @@ func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, ve
600600

601601
func (api *API) AddPageLabels(page *PageInfo, newLabels []string) (*LabelInfo, error) {
602602

603-
labels := []map[string]interface{}{}
603+
labels := []map[string]any{}
604604
for _, label := range newLabels {
605605
if label != "" {
606-
item := map[string]interface{}{
606+
item := map[string]any{
607607
"prefix": "global",
608608
"name": label,
609609
}
@@ -765,17 +765,17 @@ func (api *API) RestrictPageUpdatesCloud(
765765
user = currentUser
766766
}
767767

768-
var result interface{}
768+
var result any
769769

770770
request, err := api.rest.
771771
Res("content").
772772
Id(page.ID).
773773
Res("restriction", &result).
774-
Post([]map[string]interface{}{
774+
Post([]map[string]any{
775775
{
776776
"operation": "update",
777-
"restrictions": map[string]interface{}{
778-
"user": []map[string]interface{}{
777+
"restrictions": map[string]any{
778+
"user": []map[string]any{
779779
{
780780
"type": "known",
781781
"accountId": user.AccountID,
@@ -801,15 +801,15 @@ func (api *API) RestrictPageUpdatesServer(
801801
) error {
802802
var (
803803
err error
804-
result interface{}
804+
result any
805805
)
806806

807807
request, err := api.json.Res(
808808
"setContentPermissions", &result,
809-
).Post([]interface{}{
809+
).Post([]any{
810810
page.ID,
811811
"Edit",
812-
[]map[string]interface{}{
812+
[]map[string]any{
813813
{
814814
"userName": allowedUser,
815815
},

includes/templates.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func ProcessIncludes(
7575
templates *template.Template,
7676
) (*template.Template, []byte, bool, error) {
7777
formatVardump := func(
78-
data map[string]interface{},
78+
data map[string]any,
7979
) string {
8080
var parts []string
8181
for key, value := range data {
@@ -105,7 +105,7 @@ func ProcessIncludes(
105105
left = string(groups[3])
106106
right = string(groups[4])
107107
config = groups[5]
108-
data = map[string]interface{}{}
108+
data = map[string]any{}
109109
)
110110

111111
if delimsNone == "none" {

macro/macro.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (macro *Macro) Apply(
3737
content = macro.Regexp.ReplaceAllFunc(
3838
content,
3939
func(match []byte) []byte {
40-
config := map[string]interface{}{}
40+
config := map[string]any{}
4141

4242
err = yaml.Unmarshal([]byte(macro.Config), &config)
4343
if err != nil {
@@ -63,21 +63,21 @@ func (macro *Macro) Apply(
6363
return content, err
6464
}
6565

66-
func (macro *Macro) configure(node interface{}, groups [][]byte) interface{} {
66+
func (macro *Macro) configure(node any, groups [][]byte) any {
6767
switch node := node.(type) {
68-
case map[interface{}]interface{}:
68+
case map[any]any:
6969
for key, value := range node {
7070
node[key] = macro.configure(value, groups)
7171
}
7272

7373
return node
74-
case map[string]interface{}:
74+
case map[string]any:
7575
for key, value := range node {
7676
node[key] = macro.configure(value, groups)
7777
}
7878

7979
return node
80-
case []interface{}:
80+
case []any:
8181
for key, value := range node {
8282
node[key] = macro.configure(value, groups)
8383
}
@@ -126,7 +126,7 @@ func ExtractMacros(
126126
var macro Macro
127127

128128
if strings.HasPrefix(template, "#") {
129-
cfg := map[string]interface{}{}
129+
cfg := map[string]any{}
130130

131131
err = yaml.Unmarshal([]byte(config), &cfg)
132132
if err != nil {
@@ -170,7 +170,7 @@ func ExtractMacros(
170170
macro.Config = config
171171

172172
log.Trace().
173-
Interface("vardump", map[string]interface{}{
173+
Interface("vardump", map[string]any{
174174
"expr": expr,
175175
"template": template,
176176
"config": macro.Config,

mark.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"crypto/sha1"
66
"encoding/hex"
7+
"errors"
78
"fmt"
89
"io"
910
"os"
@@ -97,7 +98,7 @@ func Run(config Config) error {
9798
if config.CI {
9899
log.Warn().Msg(msg)
99100
} else {
100-
return fmt.Errorf("%s", msg)
101+
return errors.New(msg)
101102
}
102103
}
103104

util/cli.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func RunMark(ctx context.Context, cmd *cli.Command) error {
2323
output := zerolog.ConsoleWriter{
2424
Out: os.Stderr,
2525
TimeFormat: "2006-01-02 15:04:05.000",
26-
FormatLevel: func(i interface{}) string {
26+
FormatLevel: func(i any) string {
2727
var l string
2828
if ll, ok := i.(string); ok {
2929
switch ll {
@@ -49,16 +49,16 @@ func RunMark(ctx context.Context, cmd *cli.Command) error {
4949
}
5050
return l
5151
},
52-
FormatFieldName: func(i interface{}) string {
52+
FormatFieldName: func(i any) string {
5353
return ""
5454
},
55-
FormatFieldValue: func(i interface{}) string {
55+
FormatFieldValue: func(i any) string {
5656
return fmt.Sprintf("%s", i)
5757
},
58-
FormatErrFieldName: func(i interface{}) string {
58+
FormatErrFieldName: func(i any) string {
5959
return ""
6060
},
61-
FormatErrFieldValue: func(i interface{}) string {
61+
FormatErrFieldValue: func(i any) string {
6262
return fmt.Sprintf("%s", i)
6363
},
6464
}

util/error_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func NewErrorHandler(continueOnError bool) *FatalErrorHandler {
1414
}
1515
}
1616

17-
func (h *FatalErrorHandler) Handle(err error, format string, args ...interface{}) {
17+
func (h *FatalErrorHandler) Handle(err error, format string, args ...any) {
1818

1919
if err == nil {
2020
if h.ContinueOnError {

0 commit comments

Comments
 (0)