Source File
union.go
Belonging Package
github.com/jcmturner/rpc/v2/ndr
package ndr
import (
)
// Union interface must be implemented by structs that will be unmarshaled into from the NDR byte stream union representation.
// The union's discriminating tag will be passed to the SwitchFunc method.
// The discriminating tag field must have the struct tag: `ndr:"unionTag"`
// If the union is encapsulated the discriminating tag field must have the struct tag: `ndr:"encapsulated"`
// The possible value fields that can be selected from must have the struct tag: `ndr:"unionField"`
type Union interface {
SwitchFunc(t interface{}) string
}
// Union related constants such as struct tag values
const (
unionSelectionFuncName = "SwitchFunc"
TagEncapsulated = "encapsulated"
TagUnionTag = "unionTag"
TagUnionField = "unionField"
)
func ( *Decoder) ( reflect.Value, reflect.StructTag) ( reflect.Value) {
:= parseTags()
if !.HasValue(TagUnionTag) {
return
}
=
// For a non-encapsulated union, the discriminant is marshalled into the transmitted data stream twice: once as the
// field or parameter, which is referenced by the switch_is construct, in the procedure argument list; and once as
// the first part of the union representation.
if !.HasValue(TagEncapsulated) {
.r.Discard(int(.Type().Size()))
}
return
}
// unionSelectedField returns the field name of which of the union values to fill
func unionSelectedField(, reflect.Value) (string, error) {
if !.Type().Implements(reflect.TypeOf(new(Union)).Elem()) {
return "", errors.New("struct does not implement union interface")
}
:= []reflect.Value{}
// Call the SelectFunc of the union struct to find the name of the field to fill with the value selected.
:= .MethodByName(unionSelectionFuncName)
if !.IsValid() {
return "", fmt.Errorf("could not find a selection function called %s in the unions struct representation", unionSelectionFuncName)
}
:= .Call()
if [0].Kind() != reflect.String || [0].String() == "" {
return "", fmt.Errorf("the union select function did not return a string for the name of the field to fill")
}
return [0].String(), nil
}
![]() |
The pages are generated with Golds v0.6.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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |