func checkStructuralSubsetImpl()

in pkg/cloud/api/check.go [184:220]


func checkStructuralSubsetImpl(p Path, from, to reflect.Type) error {
	if from.Kind() != to.Kind() {
		return fmt.Errorf("%s has different type: %v != %v", p, from.Kind(), to.Kind())
	}
	if isBasicT(from) {
		return nil
	}
	switch from.Kind() {
	case reflect.Pointer:
		return checkStructuralSubsetImpl(p.Pointer(), from.Elem(), to.Elem())

	case reflect.Struct:
		for i := 0; i < from.NumField(); i++ {
			af := from.Field(i)
			bf, exist := to.FieldByName(af.Name)
			if !exist {
				return fmt.Errorf("%s: type %T does not have field %v", p.String(), to, af.Name)
			}
			if err := checkStructuralSubsetImpl(p.Field(af.Name), af.Type, bf.Type); err != nil {
				return err
			}
		}
		return nil

	case reflect.Slice, reflect.Array:
		return checkStructuralSubsetImpl(p.AnySliceIndex(), from.Elem(), to.Elem())

	case reflect.Map:
		path := p.AnyMapIndex()
		err := checkStructuralSubsetImpl(path, from.Key(), to.Key())
		if err != nil {
			return err
		}
		return checkStructuralSubsetImpl(path, from.Elem(), to.Elem())
	}
	return fmt.Errorf("%s Unsupported type %v", p.String(), from.Kind())
}