mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
move lib/go/gremlin to ent/dialect (#1192)
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
This commit is contained in:
committed by
Facebook Github Bot
parent
8b2447b8eb
commit
1e47de5300
94
dialect/gremlin/encoding/graphson/encode.go
Normal file
94
dialect/gremlin/encoding/graphson/encode.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package graphson
|
||||
|
||||
import (
|
||||
"io"
|
||||
"reflect"
|
||||
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/modern-go/reflect2"
|
||||
)
|
||||
|
||||
type encodeExtension struct {
|
||||
jsoniter.DummyExtension
|
||||
}
|
||||
|
||||
// Marshal returns the graphson encoding of v.
|
||||
func Marshal(v interface{}) ([]byte, error) {
|
||||
return config.Marshal(v)
|
||||
}
|
||||
|
||||
// MarshalToString returns the graphson encoding of v as string.
|
||||
func MarshalToString(v interface{}) (string, error) {
|
||||
return config.MarshalToString(v)
|
||||
}
|
||||
|
||||
// Encoder defines a graphson encoder.
|
||||
type Encoder interface {
|
||||
Encode(interface{}) error
|
||||
}
|
||||
|
||||
// NewEncoder create a graphson encoder.
|
||||
func NewEncoder(w io.Writer) Encoder {
|
||||
return config.NewEncoder(w)
|
||||
}
|
||||
|
||||
// Marshaler is the interface implemented by types that
|
||||
// can marshal themselves as graphson.
|
||||
type Marshaler interface {
|
||||
MarshalGraphson() ([]byte, error)
|
||||
}
|
||||
|
||||
// UpdateStructDescriptor decorates struct field encoders for graphson tags.
|
||||
func (ext encodeExtension) UpdateStructDescriptor(desc *jsoniter.StructDescriptor) {
|
||||
for _, binding := range desc.Fields {
|
||||
if tag, ok := binding.Field.Tag().Lookup("graphson"); ok && tag != "-" {
|
||||
if enc := ext.DecoratorOfStructField(binding.Encoder, tag); enc != nil {
|
||||
binding.Encoder = enc
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CreateEncoder returns a value encoder for type.
|
||||
func (ext encodeExtension) CreateEncoder(typ reflect2.Type) jsoniter.ValEncoder {
|
||||
if enc := ext.EncoderOfRegistered(typ); enc != nil {
|
||||
return enc
|
||||
}
|
||||
if enc := ext.EncoderOfNative(typ); enc != nil {
|
||||
return enc
|
||||
}
|
||||
switch typ.Kind() {
|
||||
case reflect.Map:
|
||||
return ext.EncoderOfMap(typ)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// DecorateEncoder decorates an passed in value encoder for type.
|
||||
func (ext encodeExtension) DecorateEncoder(typ reflect2.Type, enc jsoniter.ValEncoder) jsoniter.ValEncoder {
|
||||
if enc := ext.DecoratorOfRegistered(enc); enc != nil {
|
||||
return enc
|
||||
}
|
||||
if enc := ext.DecoratorOfMarshaler(typ, enc); enc != nil {
|
||||
return enc
|
||||
}
|
||||
if enc := ext.DecoratorOfTyper(typ, enc); enc != nil {
|
||||
return enc
|
||||
}
|
||||
if enc := ext.DecoratorOfNative(typ, enc); enc != nil {
|
||||
return enc
|
||||
}
|
||||
switch typ.Kind() {
|
||||
case reflect.Ptr, reflect.Interface, reflect.Struct:
|
||||
return enc
|
||||
case reflect.Array:
|
||||
return ext.DecoratorOfArray(enc)
|
||||
case reflect.Slice:
|
||||
return ext.DecoratorOfSlice(typ, enc)
|
||||
case reflect.Map:
|
||||
return ext.DecoratorOfMap(enc)
|
||||
default:
|
||||
return ext.EncoderOfError("graphson: unsupported type: " + typ.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user