mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-04-16 21:35:38 -04:00
* 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
47 lines
777 B
Go
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
|
|
}
|