-
Notifications
You must be signed in to change notification settings - Fork 30
/
schema.go
42 lines (33 loc) · 885 Bytes
/
schema.go
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
31
32
33
34
35
36
37
38
39
40
41
42
package utils
import (
"encoding/json"
"regexp"
)
var (
reSchemaReplace = regexp.MustCompile(`[\n\t]`)
)
// SchemaType defines the schema type
type SchemaType string
// String implements fmt.Stringer
func (schema SchemaType) String() string {
return string(schema)
}
// MarshalJSON returns the JSON representation
func (schema SchemaType) MarshalJSON() ([]byte, error) {
return []byte(schema.String()), nil
}
// MarshalYAML returns the YAML representation
func (schema SchemaType) MarshalYAML() (interface{}, error) {
rawStr := schema.String()
prettyStr := reSchemaReplace.ReplaceAllString(rawStr, "")
return prettyStr, nil
}
// UnmarshalJSON unmarshals raw JSON bytes into a SchemaType.
func (schema *SchemaType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return nil
}
*schema = SchemaType(s)
return nil
}