github.com/Kolosok86/http@v0.1.2/h2_error.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !nethttpomithttp2
     6  // +build !nethttpomithttp2
     7  
     8  package http
     9  
    10  import (
    11  	"reflect"
    12  )
    13  
    14  func (e http2StreamError) As(target any) bool {
    15  	dst := reflect.ValueOf(target).Elem()
    16  	dstType := dst.Type()
    17  	if dstType.Kind() != reflect.Struct {
    18  		return false
    19  	}
    20  	src := reflect.ValueOf(e)
    21  	srcType := src.Type()
    22  	numField := srcType.NumField()
    23  	if dstType.NumField() != numField {
    24  		return false
    25  	}
    26  	for i := 0; i < numField; i++ {
    27  		sf := srcType.Field(i)
    28  		df := dstType.Field(i)
    29  		if sf.Name != df.Name || !sf.Type.ConvertibleTo(df.Type) {
    30  			return false
    31  		}
    32  	}
    33  	for i := 0; i < numField; i++ {
    34  		df := dst.Field(i)
    35  		df.Set(src.Field(i).Convert(df.Type()))
    36  	}
    37  	return true
    38  }