File: marshal_ref.go

package info (click to toggle)
golang-github-getkin-kin-openapi 0.1.0+git20181119.fa639d0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 480 kB
  • sloc: makefile: 2
file content (30 lines) | stat: -rw-r--r-- 583 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package jsoninfo

import (
	"encoding/json"
)

func MarshalRef(value string, otherwise interface{}) ([]byte, error) {
	if len(value) > 0 {
		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 len(ref) > 0 {
			*destRef = ref
			return nil
		}
	}
	return json.Unmarshal(data, destOtherwise)
}

type refProps struct {
	Ref string `json:"$ref,omitempty"`
}