Files
oikos/vendor/github.com/getkin/kin-openapi/openapi3/stringmap.go
dtoro febc153b7f
Some checks failed
ci / build-test (push) Has been cancelled
ci / docker-build (push) Has been cancelled
ci / web (push) Has been cancelled
Desktop App / Build Linux (amd64) (push) Has been cancelled
Desktop App / Attach to Release (push) Has been cancelled
fix: add involves edge from task to agent:nomos at creation
Plus sync vendor directory for Docker build compatibility.
2026-08-11 22:03:12 +02:00

65 lines
1.5 KiB
Go

package openapi3
import "encoding/json"
// StringMap is a map[string]string that ignores the origin in the underlying json representation.
type StringMap[V any] map[string]V
// UnmarshalJSON sets StringMap to a copy of data.
func (stringMap *StringMap[V]) UnmarshalJSON(data []byte) (err error) {
*stringMap, err = unmarshalStringMap[V](data)
return
}
// unmarshalStringMapP unmarshals given json into a map[string]*V
func unmarshalStringMapP[V any](data []byte) (map[string]*V, error) {
var m map[string]any
if err := json.Unmarshal(data, &m); err != nil {
return nil, err
}
result := make(map[string]*V, len(m))
for _, k := range componentNames(m) {
value, err := deepCast[V](m[k])
if err != nil {
return nil, err
}
result[k] = value
}
return result, nil
}
// unmarshalStringMap unmarshals given json into a map[string]V
func unmarshalStringMap[V any](data []byte) (map[string]V, error) {
var m map[string]any
if err := json.Unmarshal(data, &m); err != nil {
return nil, err
}
result := make(map[string]V, len(m))
for _, k := range componentNames(m) {
value, err := deepCast[V](m[k])
if err != nil {
return nil, err
}
result[k] = *value
}
return result, nil
}
// deepCast casts any value to a value of type V.
func deepCast[V any](value any) (*V, error) {
data, err := json.Marshal(value)
if err != nil {
return nil, err
}
var result V
if err = json.Unmarshal(data, &result); err != nil {
return nil, err
}
return &result, nil
}