Skip to content

Commit

Permalink
fix: sort package tags alphabeticaly
Browse files Browse the repository at this point in the history
Signed-off-by: Rachel Powers <[email protected]>
  • Loading branch information
Ryex committed Oct 23, 2024
1 parent 8e428bf commit f586574
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/structures/tags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package structures

import (
"bytes"
"encoding/json"
"maps"
"slices"

"github.com/ryex/dungeondraft-gopackager/internal/utils"
Expand Down Expand Up @@ -155,3 +158,56 @@ func (pt *PackageTags) ClearTagsFor(resources ...string) {
pt.Tags[tag].RemoveM(relPaths...)
}
}

func (pt *PackageTags) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer

buf.WriteString("{")

buf.WriteString(`"tags":{`)
tagsKeys := slices.Sorted(maps.Keys(pt.Tags))
for i, key := range tagsKeys {
if i != 0 {
buf.WriteString(",")
}
keyJSON, err := json.Marshal(key)
if err != nil {
return nil, err
}
buf.Write(keyJSON)
buf.WriteString(":")
valJSON, err := json.Marshal(pt.Tags[key])
if err != nil {
return nil, err
}
buf.Write(valJSON)
}
buf.WriteString("}")

buf.WriteString(",")

buf.WriteString(`"sets":{`)

setsKeys := slices.Sorted(maps.Keys(pt.Sets))
for i, key := range setsKeys {
if i != 0 {
buf.WriteString(",")
}
keyJSON, err := json.Marshal(key)
if err != nil {
return nil, err
}
buf.Write(keyJSON)
buf.WriteString(":")
valJSON, err := json.Marshal(pt.Sets[key])
if err != nil {
return nil, err
}
buf.Write(valJSON)
}
buf.WriteString("}")

buf.WriteString("}")

return buf.Bytes(), nil
}

0 comments on commit f586574

Please sign in to comment.