package encoderimport ()// The functions of marshaling of WithMarshalers of encoding/json/v2: a value of a type which a function takes is// written by the function, before its method or its kind. The opcodes of a type depend on the functions, so they// are compiled for the functions and cached with them, apart from the tables shared by every goroutine.// MarshalFunc is a function of marshaling, for the values of the types castable to Type ( see runtime.CastableTo ).typeMarshalFuncstruct { Type reflect.Type// Append appends the value at p, of the type from: ErrUseDefault if the function declined to write it, which // lets the next function, the method or the kind of the type write it. Append func(ctx *RuntimeContext, b []byte, p unsafe.Pointer, from reflect.Type) ([]byte, error)}// MarshalFuncs are the functions of marshaling of a call, in the order they are tried, with the opcodes compiled// for them.typeMarshalFuncsstruct { Funcs []MarshalFunc codeSets sync.Map// map[modeKey]*OpcodeSet castable sync.Map// map[reflect.Type][]*MarshalFunc defaults sync.Map// map[defaultKey]*OpcodeSet: the default representations ( see appendDefault )}// codeSet returns the opcodes of the type for the functions, compiling them if the type is new.func ( *MarshalFuncs) ( uintptr, compileMode) (*OpcodeSet, error) {returncodeSetIn(&.codeSets, , , )}// funcsOf returns the functions which take a value of the type, in their order.func ( *MarshalFuncs) ( reflect.Type) []*MarshalFunc {if == nil {returnnil }if , := .castable.Load(); {return .([]*MarshalFunc) }var []*MarshalFuncfor := range .Funcs {ifruntime.CastableTo(, .Funcs[].Type) { = append(, &.Funcs[]) } } , := .castable.LoadOrStore(, )return .([]*MarshalFunc)}// appendByFuncs appends the value at p, of typ, by the first of the functions which doesn't decline it, or// returns ErrUseDefault if they all do.func appendByFuncs( *RuntimeContext, []byte, unsafe.Pointer, reflect.Type, []*MarshalFunc) ([]byte, error) {for , := range { , := .Append(, , , )if != ErrUseDefault {return , } }return , ErrUseDefault}// v2FuncsCode returns the code of a value of typ, addressable or not, written by the functions of marshaling, and// true, if a function takes it. A value which they all decline is written by its default representation ( see// appendDefault ).func ( *Compiler) ( reflect.Type, bool) (Code, bool) { := .funcs.funcsOf()iflen() == 0 {returnnil, false }return .appendFuncCode(, func( *RuntimeContext, []byte, unsafe.Pointer) ([]byte, error) { , := appendByFuncs(, , , , )if == ErrUseDefault {returnappendDefault(, , , , noMethod, ) }return , }), true}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.