Files
rimgo/api/json.go
orangix 14192e2943 change Album and Tag to use encoding/json
* move sanitization code to views

* split Tag into Tag and the subset TagMeta to match Imgur API

* make the code to parse an array generic

* change some of the views to match
2026-01-26 03:08:54 +01:00

47 lines
777 B
Go

package api
import (
"encoding/json"
"errors"
"fmt"
"sync"
)
type array[T any] []T
func (arr *array[T]) UnmarshalJSON(data []byte) error {
var rawArr []json.RawMessage
err := json.Unmarshal(data, &rawArr)
if err != nil {
return err
}
*arr = make(array[T], len(rawArr))
errs := make([]error, 0, len(rawArr))
wg := sync.WaitGroup{}
var handlePanic = func() {
if v := recover(); v != nil {
v, ok := v.(error)
if !ok {
v = fmt.Errorf("%v", v)
}
errs = append(errs, v)
}
}
for i, value := range rawArr {
wg.Add(1)
go func() {
defer handlePanic()
defer wg.Done()
err := json.Unmarshal(value, &(*arr)[i])
if err != nil {
panic(err)
}
}()
}
wg.Wait()
if len(errs) != 0 {
return errors.Join(errs...)
}
return nil
}