message_reflect_field_gen.go raw

   1  // Copyright 2018 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  // Code generated by generate-types. DO NOT EDIT.
   6  
   7  package impl
   8  
   9  import (
  10  	"reflect"
  11  
  12  	"google.golang.org/protobuf/reflect/protoreflect"
  13  )
  14  
  15  func getterForNullableScalar(fd protoreflect.FieldDescriptor, fs reflect.StructField, conv Converter, fieldOffset offset) func(p pointer) protoreflect.Value {
  16  	ft := fs.Type
  17  	if ft.Kind() == reflect.Ptr {
  18  		ft = ft.Elem()
  19  	}
  20  	if fd.Kind() == protoreflect.EnumKind {
  21  		elemType := fs.Type.Elem()
  22  		// Enums for nullable types.
  23  		return func(p pointer) protoreflect.Value {
  24  			if p.IsNil() {
  25  				return conv.Zero()
  26  			}
  27  			rv := p.Apply(fieldOffset).Elem().AsValueOf(elemType)
  28  			if rv.IsNil() {
  29  				return conv.Zero()
  30  			}
  31  			return conv.PBValueOf(rv.Elem())
  32  		}
  33  	}
  34  	switch ft.Kind() {
  35  	case reflect.Bool:
  36  		return func(p pointer) protoreflect.Value {
  37  			if p.IsNil() {
  38  				return conv.Zero()
  39  			}
  40  			x := p.Apply(fieldOffset).BoolPtr()
  41  			if *x == nil {
  42  				return conv.Zero()
  43  			}
  44  			return protoreflect.ValueOfBool(**x)
  45  		}
  46  	case reflect.Int32:
  47  		return func(p pointer) protoreflect.Value {
  48  			if p.IsNil() {
  49  				return conv.Zero()
  50  			}
  51  			x := p.Apply(fieldOffset).Int32Ptr()
  52  			if *x == nil {
  53  				return conv.Zero()
  54  			}
  55  			return protoreflect.ValueOfInt32(**x)
  56  		}
  57  	case reflect.Uint32:
  58  		return func(p pointer) protoreflect.Value {
  59  			if p.IsNil() {
  60  				return conv.Zero()
  61  			}
  62  			x := p.Apply(fieldOffset).Uint32Ptr()
  63  			if *x == nil {
  64  				return conv.Zero()
  65  			}
  66  			return protoreflect.ValueOfUint32(**x)
  67  		}
  68  	case reflect.Int64:
  69  		return func(p pointer) protoreflect.Value {
  70  			if p.IsNil() {
  71  				return conv.Zero()
  72  			}
  73  			x := p.Apply(fieldOffset).Int64Ptr()
  74  			if *x == nil {
  75  				return conv.Zero()
  76  			}
  77  			return protoreflect.ValueOfInt64(**x)
  78  		}
  79  	case reflect.Uint64:
  80  		return func(p pointer) protoreflect.Value {
  81  			if p.IsNil() {
  82  				return conv.Zero()
  83  			}
  84  			x := p.Apply(fieldOffset).Uint64Ptr()
  85  			if *x == nil {
  86  				return conv.Zero()
  87  			}
  88  			return protoreflect.ValueOfUint64(**x)
  89  		}
  90  	case reflect.Float32:
  91  		return func(p pointer) protoreflect.Value {
  92  			if p.IsNil() {
  93  				return conv.Zero()
  94  			}
  95  			x := p.Apply(fieldOffset).Float32Ptr()
  96  			if *x == nil {
  97  				return conv.Zero()
  98  			}
  99  			return protoreflect.ValueOfFloat32(**x)
 100  		}
 101  	case reflect.Float64:
 102  		return func(p pointer) protoreflect.Value {
 103  			if p.IsNil() {
 104  				return conv.Zero()
 105  			}
 106  			x := p.Apply(fieldOffset).Float64Ptr()
 107  			if *x == nil {
 108  				return conv.Zero()
 109  			}
 110  			return protoreflect.ValueOfFloat64(**x)
 111  		}
 112  	case reflect.String:
 113  		if fd.Kind() == protoreflect.BytesKind {
 114  			return func(p pointer) protoreflect.Value {
 115  				if p.IsNil() {
 116  					return conv.Zero()
 117  				}
 118  				x := p.Apply(fieldOffset).StringPtr()
 119  				if *x == nil {
 120  					return conv.Zero()
 121  				}
 122  				if len(**x) == 0 {
 123  					return protoreflect.ValueOfBytes(nil)
 124  				}
 125  				return protoreflect.ValueOfBytes([]byte(**x))
 126  			}
 127  		}
 128  		return func(p pointer) protoreflect.Value {
 129  			if p.IsNil() {
 130  				return conv.Zero()
 131  			}
 132  			x := p.Apply(fieldOffset).StringPtr()
 133  			if *x == nil {
 134  				return conv.Zero()
 135  			}
 136  			return protoreflect.ValueOfString(**x)
 137  		}
 138  	case reflect.Slice:
 139  		if fd.Kind() == protoreflect.StringKind {
 140  			return func(p pointer) protoreflect.Value {
 141  				if p.IsNil() {
 142  					return conv.Zero()
 143  				}
 144  				x := p.Apply(fieldOffset).Bytes()
 145  				if len(*x) == 0 {
 146  					return conv.Zero()
 147  				}
 148  				return protoreflect.ValueOfString(string(*x))
 149  			}
 150  		}
 151  		return func(p pointer) protoreflect.Value {
 152  			if p.IsNil() {
 153  				return conv.Zero()
 154  			}
 155  			x := p.Apply(fieldOffset).Bytes()
 156  			if *x == nil {
 157  				return conv.Zero()
 158  			}
 159  			return protoreflect.ValueOfBytes(*x)
 160  		}
 161  	}
 162  	panic("unexpected protobuf kind: " + ft.Kind().String())
 163  }
 164  
 165  func getterForDirectScalar(fd protoreflect.FieldDescriptor, fs reflect.StructField, conv Converter, fieldOffset offset) func(p pointer) protoreflect.Value {
 166  	ft := fs.Type
 167  	if fd.Kind() == protoreflect.EnumKind {
 168  		// Enums for non nullable types.
 169  		return func(p pointer) protoreflect.Value {
 170  			if p.IsNil() {
 171  				return conv.Zero()
 172  			}
 173  			rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
 174  			return conv.PBValueOf(rv)
 175  		}
 176  	}
 177  	switch ft.Kind() {
 178  	case reflect.Bool:
 179  		return func(p pointer) protoreflect.Value {
 180  			if p.IsNil() {
 181  				return conv.Zero()
 182  			}
 183  			x := p.Apply(fieldOffset).Bool()
 184  			return protoreflect.ValueOfBool(*x)
 185  		}
 186  	case reflect.Int32:
 187  		return func(p pointer) protoreflect.Value {
 188  			if p.IsNil() {
 189  				return conv.Zero()
 190  			}
 191  			x := p.Apply(fieldOffset).Int32()
 192  			return protoreflect.ValueOfInt32(*x)
 193  		}
 194  	case reflect.Uint32:
 195  		return func(p pointer) protoreflect.Value {
 196  			if p.IsNil() {
 197  				return conv.Zero()
 198  			}
 199  			x := p.Apply(fieldOffset).Uint32()
 200  			return protoreflect.ValueOfUint32(*x)
 201  		}
 202  	case reflect.Int64:
 203  		return func(p pointer) protoreflect.Value {
 204  			if p.IsNil() {
 205  				return conv.Zero()
 206  			}
 207  			x := p.Apply(fieldOffset).Int64()
 208  			return protoreflect.ValueOfInt64(*x)
 209  		}
 210  	case reflect.Uint64:
 211  		return func(p pointer) protoreflect.Value {
 212  			if p.IsNil() {
 213  				return conv.Zero()
 214  			}
 215  			x := p.Apply(fieldOffset).Uint64()
 216  			return protoreflect.ValueOfUint64(*x)
 217  		}
 218  	case reflect.Float32:
 219  		return func(p pointer) protoreflect.Value {
 220  			if p.IsNil() {
 221  				return conv.Zero()
 222  			}
 223  			x := p.Apply(fieldOffset).Float32()
 224  			return protoreflect.ValueOfFloat32(*x)
 225  		}
 226  	case reflect.Float64:
 227  		return func(p pointer) protoreflect.Value {
 228  			if p.IsNil() {
 229  				return conv.Zero()
 230  			}
 231  			x := p.Apply(fieldOffset).Float64()
 232  			return protoreflect.ValueOfFloat64(*x)
 233  		}
 234  	case reflect.String:
 235  		if fd.Kind() == protoreflect.BytesKind {
 236  			return func(p pointer) protoreflect.Value {
 237  				if p.IsNil() {
 238  					return conv.Zero()
 239  				}
 240  				x := p.Apply(fieldOffset).String()
 241  				if len(*x) == 0 {
 242  					return protoreflect.ValueOfBytes(nil)
 243  				}
 244  				return protoreflect.ValueOfBytes([]byte(*x))
 245  			}
 246  		}
 247  		return func(p pointer) protoreflect.Value {
 248  			if p.IsNil() {
 249  				return conv.Zero()
 250  			}
 251  			x := p.Apply(fieldOffset).String()
 252  			return protoreflect.ValueOfString(*x)
 253  		}
 254  	case reflect.Slice:
 255  		if fd.Kind() == protoreflect.StringKind {
 256  			return func(p pointer) protoreflect.Value {
 257  				if p.IsNil() {
 258  					return conv.Zero()
 259  				}
 260  				x := p.Apply(fieldOffset).Bytes()
 261  				return protoreflect.ValueOfString(string(*x))
 262  			}
 263  		}
 264  		return func(p pointer) protoreflect.Value {
 265  			if p.IsNil() {
 266  				return conv.Zero()
 267  			}
 268  			x := p.Apply(fieldOffset).Bytes()
 269  			return protoreflect.ValueOfBytes(*x)
 270  		}
 271  	}
 272  	panic("unexpected protobuf kind: " + ft.Kind().String())
 273  }
 274