Ejemplo n.º 1
0
func mapStruct(t reflect.Type, n schema.Node) (structProps, error) {
	fields, err := n.StructNode().Fields()
	if err != nil {
		return structProps{}, err
	}
	sp := structProps{
		fields:   make([]fieldLoc, fields.Len()),
		whichLoc: fieldLoc{i: -1},
	}
	for i := range sp.fields {
		sp.fields[i] = fieldLoc{i: -1}
	}
	sm := structMapper{
		sp:         &sp,
		t:          t,
		hasDiscrim: hasDiscriminant(n),
		fields:     fields,
	}
	if err := sm.visit(fieldLoc{i: -1}); err != nil {
		return structProps{}, err
	}
	for len(sm.embedQueue) > 0 {
		loc := sm.embedQueue[0]
		copy(sm.embedQueue, sm.embedQueue[1:])
		sm.embedQueue = sm.embedQueue[:len(sm.embedQueue)-1]
		if err := sm.visit(loc); err != nil {
			return structProps{}, err
		}
	}
	return sp, nil
}
Ejemplo n.º 2
0
func hasDiscriminant(n schema.Node) bool {
	return n.Which() == schema.Node_Which_structNode && n.StructNode().DiscriminantCount() > 0
}