mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
Summary: Pull Request resolved: https://github.com/facebookexternal/fbc/pull/1192 Pull Request resolved: https://github.com/facebookincubator/ent/pull/11 Reviewed By: alexsn Differential Revision: D16377224 fbshipit-source-id: 07ca7436eb9b64fbe2299568560b91466b2417ba
106 lines
2.7 KiB
Go
106 lines
2.7 KiB
Go
package graphson
|
|
|
|
import (
|
|
"io"
|
|
"reflect"
|
|
|
|
"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())
|
|
}
|
|
}
|