debian-forge-composer/vendor/github.com/getkin/kin-openapi/jsoninfo/marshal_ref.go
Chloe Kaubisch 13c79294b6 cloudapi: validate input
Validate incoming requests with openapi3. Remove unsupported
uuid format from the openapi spec. Similarly, change url to uri as
uri is a supported format and url is not.

Co-authored-by: Ondřej Budai <obudai@redhat.com>
Signed-off-by: Ondřej Budai <ondrej@budai.cz>
2022-05-16 13:20:46 +02:00

30 lines
599 B
Go

package jsoninfo
import (
"encoding/json"
)
func MarshalRef(value string, otherwise interface{}) ([]byte, error) {
if value != "" {
return json.Marshal(&refProps{
Ref: value,
})
}
return json.Marshal(otherwise)
}
func UnmarshalRef(data []byte, destRef *string, destOtherwise interface{}) error {
refProps := &refProps{}
if err := json.Unmarshal(data, refProps); err == nil {
ref := refProps.Ref
if ref != "" {
*destRef = ref
return nil
}
}
return json.Unmarshal(data, destOtherwise)
}
type refProps struct {
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}