Files
ent/dialect/gremlin/encoding/graphson/decode.go
Ariel Mashraki 7988d3084d entc/hooks: initial work for mutations and hooks (#377)
* entc/hooks: initial work for mutations and hooks

* ent/schema: adding policy to schema

* ent: change op string to uint

* entc: move entschema to runtime and enable smooth transition

* entc/privacy: adding privacy template

* all: goimports

* intg/hooks: mutation client/tx and basic schema tests

* ent/privacy: adding more verbose decisions

* entc/gen: edge-ids getter and additional tests

* all: regen assets

* entc/gen: fix client hookd propagation

* intg: add deletion example

* intg/privacy: remove old entschema package

* typed privacy

* ent/privacy: hooks shouldn't be called on privacy deny

* entc/gen: fix schema hooks invocation order

* remove read policy from public api

* update circleci go orb

Co-authored-by: Ariel Mashraki <ariel@mashraki.co.il>
2020-03-16 00:19:27 +02:00

110 lines
2.9 KiB
Go

// 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())
}
}