Source file src/net/http/h2_error.go

     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  
     7  package http
     8  
     9  import (
    10  	"reflect"
    11  )
    12  
    13  func (e http2StreamError) As(target any) bool {
    14  	dst := reflect.ValueOf(target).Elem()
    15  	dstType := dst.Type()
    16  	if dstType.Kind() != reflect.Struct {
    17  		return false
    18  	}
    19  	src := reflect.ValueOf(e)
    20  	srcType := src.Type()
    21  	numField := srcType.NumField()
    22  	if dstType.NumField() != numField {
    23  		return false
    24  	}
    25  	for i := 0; i < numField; i++ {
    26  		sf := srcType.Field(i)
    27  		df := dstType.Field(i)
    28  		if sf.Name != df.Name || !sf.Type.ConvertibleTo(df.Type) {
    29  			return false
    30  		}
    31  	}
    32  	for i := 0; i < numField; i++ {
    33  		df := dst.Field(i)
    34  		df.Set(src.Field(i).Convert(df.Type()))
    35  	}
    36  	return true
    37  }
    38  

View as plain text