File: decode.go

package info (click to toggle)
golang-github-facebook-ent 0.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,284 kB
  • sloc: javascript: 349; makefile: 8
file content (109 lines) | stat: -rw-r--r-- 2,971 bytes parent folder | download | duplicates (2)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package graphson

import (
	"io"
	"reflect"

	jsoniter "github.com/json-iterator/go"
	"github.com/modern-go/reflect2"
)

type decodeExtension struct {
	jsoniter.DummyExtension
}

// Unmarshal parses the graphson encoded data and stores the result
// in the value pointed to by v.
func Unmarshal(data []byte, v interface{}) error {
	return config.Unmarshal(data, v)
}

// UnmarshalFromString parses the graphson encoded str and stores the result
// in the value pointed to by v.
func UnmarshalFromString(str string, v interface{}) error {
	return config.UnmarshalFromString(str, v)
}

// Decoder defines a graphson decoder.
type Decoder interface {
	Decode(interface{}) error
}

// NewDecoder create a graphson decoder.
func NewDecoder(r io.Reader) Decoder {
	return config.NewDecoder(r)
}

// Unmarshaler is the interface implemented by types
// that can unmarshal a graphson description of themselves.
type Unmarshaler interface {
	UnmarshalGraphson([]byte) error
}

// UpdateStructDescriptor decorates struct field encoders for graphson tags.
func (ext decodeExtension) UpdateStructDescriptor(desc *jsoniter.StructDescriptor) {
	for _, binding := range desc.Fields {
		if tag, ok := binding.Field.Tag().Lookup("graphson"); ok && tag != "-" {
			if dec := ext.DecoratorOfStructField(binding.Decoder, tag); dec != nil {
				binding.Decoder = dec
			}
		}
	}
}

// CreateDecoder returns a value decoder for type.
func (ext decodeExtension) CreateDecoder(typ reflect2.Type) jsoniter.ValDecoder {
	if dec := ext.DecoderOfRegistered(typ); dec != nil {
		return dec
	}
	if dec := ext.DecoderOfUnmarshaler(typ); dec != nil {
		return dec
	}
	if dec := ext.DecoderOfNative(typ); dec != nil {
		return dec
	}
	switch typ.Kind() {
	case reflect.Array:
		return ext.DecoderOfArray(typ)
	case reflect.Slice:
		return ext.DecoderOfSlice(typ)
	case reflect.Map:
		return ext.DecoderOfMap(typ)
	default:
		return nil
	}
}

// DecorateDecoder decorates an passed in value decoder for type.
func (ext decodeExtension) DecorateDecoder(typ reflect2.Type, dec jsoniter.ValDecoder) jsoniter.ValDecoder {
	if dec := ext.DecoratorOfRegistered(dec); dec != nil {
		return dec
	}
	if dec := ext.DecoratorOfUnmarshaler(typ, dec); dec != nil {
		return dec
	}
	if dec := ext.DecoratorOfTyper(typ, dec); dec != nil {
		return dec
	}
	if dec := ext.DecoratorOfNative(typ, dec); dec != nil {
		return dec
	}
	switch typ.Kind() {
	case reflect.Ptr, reflect.Struct:
		return dec
	case reflect.Interface:
		return ext.DecoratorOfInterface(typ, dec)
	case reflect.Slice:
		return ext.DecoratorOfSlice(typ, dec)
	case reflect.Array:
		return ext.DecoratorOfArray(dec)
	case reflect.Map:
		return ext.DecoratorOfMap(dec)
	default:
		return ext.DecoderOfError("graphson: unsupported type: " + typ.String())
	}
}