另客网go项目公用的代码库

safe_struct.go 781B

123456789101112131415161718192021222324252627282930
  1. package reflect2
  2. type safeStructType struct {
  3. safeType
  4. }
  5. func (type2 *safeStructType) FieldByName(name string) StructField {
  6. field, found := type2.Type.FieldByName(name)
  7. if !found {
  8. panic("field " + name + " not found")
  9. }
  10. return &safeField{StructField: field}
  11. }
  12. func (type2 *safeStructType) Field(i int) StructField {
  13. return &safeField{StructField: type2.Type.Field(i)}
  14. }
  15. func (type2 *safeStructType) FieldByIndex(index []int) StructField {
  16. return &safeField{StructField: type2.Type.FieldByIndex(index)}
  17. }
  18. func (type2 *safeStructType) FieldByNameFunc(match func(string) bool) StructField {
  19. field, found := type2.Type.FieldByNameFunc(match)
  20. if !found {
  21. panic("field match condition not found in " + type2.Type.String())
  22. }
  23. return &safeField{StructField: field}
  24. }