decode_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 proto
   8  
   9  import (
  10  	"math"
  11  	"unicode/utf8"
  12  
  13  	"google.golang.org/protobuf/encoding/protowire"
  14  	"google.golang.org/protobuf/internal/errors"
  15  	"google.golang.org/protobuf/internal/strs"
  16  	"google.golang.org/protobuf/reflect/protoreflect"
  17  )
  18  
  19  // unmarshalScalar decodes a value of the given kind.
  20  //
  21  // Message values are decoded into a []byte which aliases the input data.
  22  func (o UnmarshalOptions) unmarshalScalar(b []byte, wtyp protowire.Type, fd protoreflect.FieldDescriptor) (val protoreflect.Value, n int, err error) {
  23  	switch fd.Kind() {
  24  	case protoreflect.BoolKind:
  25  		if wtyp != protowire.VarintType {
  26  			return val, 0, errUnknown
  27  		}
  28  		v, n := protowire.ConsumeVarint(b)
  29  		if n < 0 {
  30  			return val, 0, errDecode
  31  		}
  32  		return protoreflect.ValueOfBool(protowire.DecodeBool(v)), n, nil
  33  	case protoreflect.EnumKind:
  34  		if wtyp != protowire.VarintType {
  35  			return val, 0, errUnknown
  36  		}
  37  		v, n := protowire.ConsumeVarint(b)
  38  		if n < 0 {
  39  			return val, 0, errDecode
  40  		}
  41  		return protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)), n, nil
  42  	case protoreflect.Int32Kind:
  43  		if wtyp != protowire.VarintType {
  44  			return val, 0, errUnknown
  45  		}
  46  		v, n := protowire.ConsumeVarint(b)
  47  		if n < 0 {
  48  			return val, 0, errDecode
  49  		}
  50  		return protoreflect.ValueOfInt32(int32(v)), n, nil
  51  	case protoreflect.Sint32Kind:
  52  		if wtyp != protowire.VarintType {
  53  			return val, 0, errUnknown
  54  		}
  55  		v, n := protowire.ConsumeVarint(b)
  56  		if n < 0 {
  57  			return val, 0, errDecode
  58  		}
  59  		return protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32))), n, nil
  60  	case protoreflect.Uint32Kind:
  61  		if wtyp != protowire.VarintType {
  62  			return val, 0, errUnknown
  63  		}
  64  		v, n := protowire.ConsumeVarint(b)
  65  		if n < 0 {
  66  			return val, 0, errDecode
  67  		}
  68  		return protoreflect.ValueOfUint32(uint32(v)), n, nil
  69  	case protoreflect.Int64Kind:
  70  		if wtyp != protowire.VarintType {
  71  			return val, 0, errUnknown
  72  		}
  73  		v, n := protowire.ConsumeVarint(b)
  74  		if n < 0 {
  75  			return val, 0, errDecode
  76  		}
  77  		return protoreflect.ValueOfInt64(int64(v)), n, nil
  78  	case protoreflect.Sint64Kind:
  79  		if wtyp != protowire.VarintType {
  80  			return val, 0, errUnknown
  81  		}
  82  		v, n := protowire.ConsumeVarint(b)
  83  		if n < 0 {
  84  			return val, 0, errDecode
  85  		}
  86  		return protoreflect.ValueOfInt64(protowire.DecodeZigZag(v)), n, nil
  87  	case protoreflect.Uint64Kind:
  88  		if wtyp != protowire.VarintType {
  89  			return val, 0, errUnknown
  90  		}
  91  		v, n := protowire.ConsumeVarint(b)
  92  		if n < 0 {
  93  			return val, 0, errDecode
  94  		}
  95  		return protoreflect.ValueOfUint64(v), n, nil
  96  	case protoreflect.Sfixed32Kind:
  97  		if wtyp != protowire.Fixed32Type {
  98  			return val, 0, errUnknown
  99  		}
 100  		v, n := protowire.ConsumeFixed32(b)
 101  		if n < 0 {
 102  			return val, 0, errDecode
 103  		}
 104  		return protoreflect.ValueOfInt32(int32(v)), n, nil
 105  	case protoreflect.Fixed32Kind:
 106  		if wtyp != protowire.Fixed32Type {
 107  			return val, 0, errUnknown
 108  		}
 109  		v, n := protowire.ConsumeFixed32(b)
 110  		if n < 0 {
 111  			return val, 0, errDecode
 112  		}
 113  		return protoreflect.ValueOfUint32(uint32(v)), n, nil
 114  	case protoreflect.FloatKind:
 115  		if wtyp != protowire.Fixed32Type {
 116  			return val, 0, errUnknown
 117  		}
 118  		v, n := protowire.ConsumeFixed32(b)
 119  		if n < 0 {
 120  			return val, 0, errDecode
 121  		}
 122  		return protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))), n, nil
 123  	case protoreflect.Sfixed64Kind:
 124  		if wtyp != protowire.Fixed64Type {
 125  			return val, 0, errUnknown
 126  		}
 127  		v, n := protowire.ConsumeFixed64(b)
 128  		if n < 0 {
 129  			return val, 0, errDecode
 130  		}
 131  		return protoreflect.ValueOfInt64(int64(v)), n, nil
 132  	case protoreflect.Fixed64Kind:
 133  		if wtyp != protowire.Fixed64Type {
 134  			return val, 0, errUnknown
 135  		}
 136  		v, n := protowire.ConsumeFixed64(b)
 137  		if n < 0 {
 138  			return val, 0, errDecode
 139  		}
 140  		return protoreflect.ValueOfUint64(v), n, nil
 141  	case protoreflect.DoubleKind:
 142  		if wtyp != protowire.Fixed64Type {
 143  			return val, 0, errUnknown
 144  		}
 145  		v, n := protowire.ConsumeFixed64(b)
 146  		if n < 0 {
 147  			return val, 0, errDecode
 148  		}
 149  		return protoreflect.ValueOfFloat64(math.Float64frombits(v)), n, nil
 150  	case protoreflect.StringKind:
 151  		if wtyp != protowire.BytesType {
 152  			return val, 0, errUnknown
 153  		}
 154  		v, n := protowire.ConsumeBytes(b)
 155  		if n < 0 {
 156  			return val, 0, errDecode
 157  		}
 158  		if strs.EnforceUTF8(fd) && !utf8.Valid(v) {
 159  			return protoreflect.Value{}, 0, errors.InvalidUTF8(string(fd.FullName()))
 160  		}
 161  		return protoreflect.ValueOfString(string(v)), n, nil
 162  	case protoreflect.BytesKind:
 163  		if wtyp != protowire.BytesType {
 164  			return val, 0, errUnknown
 165  		}
 166  		v, n := protowire.ConsumeBytes(b)
 167  		if n < 0 {
 168  			return val, 0, errDecode
 169  		}
 170  		return protoreflect.ValueOfBytes(append(emptyBuf[:], v...)), n, nil
 171  	case protoreflect.MessageKind:
 172  		if wtyp != protowire.BytesType {
 173  			return val, 0, errUnknown
 174  		}
 175  		v, n := protowire.ConsumeBytes(b)
 176  		if n < 0 {
 177  			return val, 0, errDecode
 178  		}
 179  		return protoreflect.ValueOfBytes(v), n, nil
 180  	case protoreflect.GroupKind:
 181  		if wtyp != protowire.StartGroupType {
 182  			return val, 0, errUnknown
 183  		}
 184  		v, n := protowire.ConsumeGroup(fd.Number(), b)
 185  		if n < 0 {
 186  			return val, 0, errDecode
 187  		}
 188  		return protoreflect.ValueOfBytes(v), n, nil
 189  	default:
 190  		return val, 0, errUnknown
 191  	}
 192  }
 193  
 194  func (o UnmarshalOptions) unmarshalList(b []byte, wtyp protowire.Type, list protoreflect.List, fd protoreflect.FieldDescriptor) (n int, err error) {
 195  	switch fd.Kind() {
 196  	case protoreflect.BoolKind:
 197  		if wtyp == protowire.BytesType {
 198  			buf, n := protowire.ConsumeBytes(b)
 199  			if n < 0 {
 200  				return 0, errDecode
 201  			}
 202  			for len(buf) > 0 {
 203  				v, n := protowire.ConsumeVarint(buf)
 204  				if n < 0 {
 205  					return 0, errDecode
 206  				}
 207  				buf = buf[n:]
 208  				list.Append(protoreflect.ValueOfBool(protowire.DecodeBool(v)))
 209  			}
 210  			return n, nil
 211  		}
 212  		if wtyp != protowire.VarintType {
 213  			return 0, errUnknown
 214  		}
 215  		v, n := protowire.ConsumeVarint(b)
 216  		if n < 0 {
 217  			return 0, errDecode
 218  		}
 219  		list.Append(protoreflect.ValueOfBool(protowire.DecodeBool(v)))
 220  		return n, nil
 221  	case protoreflect.EnumKind:
 222  		if wtyp == protowire.BytesType {
 223  			buf, n := protowire.ConsumeBytes(b)
 224  			if n < 0 {
 225  				return 0, errDecode
 226  			}
 227  			for len(buf) > 0 {
 228  				v, n := protowire.ConsumeVarint(buf)
 229  				if n < 0 {
 230  					return 0, errDecode
 231  				}
 232  				buf = buf[n:]
 233  				list.Append(protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)))
 234  			}
 235  			return n, nil
 236  		}
 237  		if wtyp != protowire.VarintType {
 238  			return 0, errUnknown
 239  		}
 240  		v, n := protowire.ConsumeVarint(b)
 241  		if n < 0 {
 242  			return 0, errDecode
 243  		}
 244  		list.Append(protoreflect.ValueOfEnum(protoreflect.EnumNumber(v)))
 245  		return n, nil
 246  	case protoreflect.Int32Kind:
 247  		if wtyp == protowire.BytesType {
 248  			buf, n := protowire.ConsumeBytes(b)
 249  			if n < 0 {
 250  				return 0, errDecode
 251  			}
 252  			for len(buf) > 0 {
 253  				v, n := protowire.ConsumeVarint(buf)
 254  				if n < 0 {
 255  					return 0, errDecode
 256  				}
 257  				buf = buf[n:]
 258  				list.Append(protoreflect.ValueOfInt32(int32(v)))
 259  			}
 260  			return n, nil
 261  		}
 262  		if wtyp != protowire.VarintType {
 263  			return 0, errUnknown
 264  		}
 265  		v, n := protowire.ConsumeVarint(b)
 266  		if n < 0 {
 267  			return 0, errDecode
 268  		}
 269  		list.Append(protoreflect.ValueOfInt32(int32(v)))
 270  		return n, nil
 271  	case protoreflect.Sint32Kind:
 272  		if wtyp == protowire.BytesType {
 273  			buf, n := protowire.ConsumeBytes(b)
 274  			if n < 0 {
 275  				return 0, errDecode
 276  			}
 277  			for len(buf) > 0 {
 278  				v, n := protowire.ConsumeVarint(buf)
 279  				if n < 0 {
 280  					return 0, errDecode
 281  				}
 282  				buf = buf[n:]
 283  				list.Append(protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32))))
 284  			}
 285  			return n, nil
 286  		}
 287  		if wtyp != protowire.VarintType {
 288  			return 0, errUnknown
 289  		}
 290  		v, n := protowire.ConsumeVarint(b)
 291  		if n < 0 {
 292  			return 0, errDecode
 293  		}
 294  		list.Append(protoreflect.ValueOfInt32(int32(protowire.DecodeZigZag(v & math.MaxUint32))))
 295  		return n, nil
 296  	case protoreflect.Uint32Kind:
 297  		if wtyp == protowire.BytesType {
 298  			buf, n := protowire.ConsumeBytes(b)
 299  			if n < 0 {
 300  				return 0, errDecode
 301  			}
 302  			for len(buf) > 0 {
 303  				v, n := protowire.ConsumeVarint(buf)
 304  				if n < 0 {
 305  					return 0, errDecode
 306  				}
 307  				buf = buf[n:]
 308  				list.Append(protoreflect.ValueOfUint32(uint32(v)))
 309  			}
 310  			return n, nil
 311  		}
 312  		if wtyp != protowire.VarintType {
 313  			return 0, errUnknown
 314  		}
 315  		v, n := protowire.ConsumeVarint(b)
 316  		if n < 0 {
 317  			return 0, errDecode
 318  		}
 319  		list.Append(protoreflect.ValueOfUint32(uint32(v)))
 320  		return n, nil
 321  	case protoreflect.Int64Kind:
 322  		if wtyp == protowire.BytesType {
 323  			buf, n := protowire.ConsumeBytes(b)
 324  			if n < 0 {
 325  				return 0, errDecode
 326  			}
 327  			for len(buf) > 0 {
 328  				v, n := protowire.ConsumeVarint(buf)
 329  				if n < 0 {
 330  					return 0, errDecode
 331  				}
 332  				buf = buf[n:]
 333  				list.Append(protoreflect.ValueOfInt64(int64(v)))
 334  			}
 335  			return n, nil
 336  		}
 337  		if wtyp != protowire.VarintType {
 338  			return 0, errUnknown
 339  		}
 340  		v, n := protowire.ConsumeVarint(b)
 341  		if n < 0 {
 342  			return 0, errDecode
 343  		}
 344  		list.Append(protoreflect.ValueOfInt64(int64(v)))
 345  		return n, nil
 346  	case protoreflect.Sint64Kind:
 347  		if wtyp == protowire.BytesType {
 348  			buf, n := protowire.ConsumeBytes(b)
 349  			if n < 0 {
 350  				return 0, errDecode
 351  			}
 352  			for len(buf) > 0 {
 353  				v, n := protowire.ConsumeVarint(buf)
 354  				if n < 0 {
 355  					return 0, errDecode
 356  				}
 357  				buf = buf[n:]
 358  				list.Append(protoreflect.ValueOfInt64(protowire.DecodeZigZag(v)))
 359  			}
 360  			return n, nil
 361  		}
 362  		if wtyp != protowire.VarintType {
 363  			return 0, errUnknown
 364  		}
 365  		v, n := protowire.ConsumeVarint(b)
 366  		if n < 0 {
 367  			return 0, errDecode
 368  		}
 369  		list.Append(protoreflect.ValueOfInt64(protowire.DecodeZigZag(v)))
 370  		return n, nil
 371  	case protoreflect.Uint64Kind:
 372  		if wtyp == protowire.BytesType {
 373  			buf, n := protowire.ConsumeBytes(b)
 374  			if n < 0 {
 375  				return 0, errDecode
 376  			}
 377  			for len(buf) > 0 {
 378  				v, n := protowire.ConsumeVarint(buf)
 379  				if n < 0 {
 380  					return 0, errDecode
 381  				}
 382  				buf = buf[n:]
 383  				list.Append(protoreflect.ValueOfUint64(v))
 384  			}
 385  			return n, nil
 386  		}
 387  		if wtyp != protowire.VarintType {
 388  			return 0, errUnknown
 389  		}
 390  		v, n := protowire.ConsumeVarint(b)
 391  		if n < 0 {
 392  			return 0, errDecode
 393  		}
 394  		list.Append(protoreflect.ValueOfUint64(v))
 395  		return n, nil
 396  	case protoreflect.Sfixed32Kind:
 397  		if wtyp == protowire.BytesType {
 398  			buf, n := protowire.ConsumeBytes(b)
 399  			if n < 0 {
 400  				return 0, errDecode
 401  			}
 402  			for len(buf) > 0 {
 403  				v, n := protowire.ConsumeFixed32(buf)
 404  				if n < 0 {
 405  					return 0, errDecode
 406  				}
 407  				buf = buf[n:]
 408  				list.Append(protoreflect.ValueOfInt32(int32(v)))
 409  			}
 410  			return n, nil
 411  		}
 412  		if wtyp != protowire.Fixed32Type {
 413  			return 0, errUnknown
 414  		}
 415  		v, n := protowire.ConsumeFixed32(b)
 416  		if n < 0 {
 417  			return 0, errDecode
 418  		}
 419  		list.Append(protoreflect.ValueOfInt32(int32(v)))
 420  		return n, nil
 421  	case protoreflect.Fixed32Kind:
 422  		if wtyp == protowire.BytesType {
 423  			buf, n := protowire.ConsumeBytes(b)
 424  			if n < 0 {
 425  				return 0, errDecode
 426  			}
 427  			for len(buf) > 0 {
 428  				v, n := protowire.ConsumeFixed32(buf)
 429  				if n < 0 {
 430  					return 0, errDecode
 431  				}
 432  				buf = buf[n:]
 433  				list.Append(protoreflect.ValueOfUint32(uint32(v)))
 434  			}
 435  			return n, nil
 436  		}
 437  		if wtyp != protowire.Fixed32Type {
 438  			return 0, errUnknown
 439  		}
 440  		v, n := protowire.ConsumeFixed32(b)
 441  		if n < 0 {
 442  			return 0, errDecode
 443  		}
 444  		list.Append(protoreflect.ValueOfUint32(uint32(v)))
 445  		return n, nil
 446  	case protoreflect.FloatKind:
 447  		if wtyp == protowire.BytesType {
 448  			buf, n := protowire.ConsumeBytes(b)
 449  			if n < 0 {
 450  				return 0, errDecode
 451  			}
 452  			for len(buf) > 0 {
 453  				v, n := protowire.ConsumeFixed32(buf)
 454  				if n < 0 {
 455  					return 0, errDecode
 456  				}
 457  				buf = buf[n:]
 458  				list.Append(protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))))
 459  			}
 460  			return n, nil
 461  		}
 462  		if wtyp != protowire.Fixed32Type {
 463  			return 0, errUnknown
 464  		}
 465  		v, n := protowire.ConsumeFixed32(b)
 466  		if n < 0 {
 467  			return 0, errDecode
 468  		}
 469  		list.Append(protoreflect.ValueOfFloat32(math.Float32frombits(uint32(v))))
 470  		return n, nil
 471  	case protoreflect.Sfixed64Kind:
 472  		if wtyp == protowire.BytesType {
 473  			buf, n := protowire.ConsumeBytes(b)
 474  			if n < 0 {
 475  				return 0, errDecode
 476  			}
 477  			for len(buf) > 0 {
 478  				v, n := protowire.ConsumeFixed64(buf)
 479  				if n < 0 {
 480  					return 0, errDecode
 481  				}
 482  				buf = buf[n:]
 483  				list.Append(protoreflect.ValueOfInt64(int64(v)))
 484  			}
 485  			return n, nil
 486  		}
 487  		if wtyp != protowire.Fixed64Type {
 488  			return 0, errUnknown
 489  		}
 490  		v, n := protowire.ConsumeFixed64(b)
 491  		if n < 0 {
 492  			return 0, errDecode
 493  		}
 494  		list.Append(protoreflect.ValueOfInt64(int64(v)))
 495  		return n, nil
 496  	case protoreflect.Fixed64Kind:
 497  		if wtyp == protowire.BytesType {
 498  			buf, n := protowire.ConsumeBytes(b)
 499  			if n < 0 {
 500  				return 0, errDecode
 501  			}
 502  			for len(buf) > 0 {
 503  				v, n := protowire.ConsumeFixed64(buf)
 504  				if n < 0 {
 505  					return 0, errDecode
 506  				}
 507  				buf = buf[n:]
 508  				list.Append(protoreflect.ValueOfUint64(v))
 509  			}
 510  			return n, nil
 511  		}
 512  		if wtyp != protowire.Fixed64Type {
 513  			return 0, errUnknown
 514  		}
 515  		v, n := protowire.ConsumeFixed64(b)
 516  		if n < 0 {
 517  			return 0, errDecode
 518  		}
 519  		list.Append(protoreflect.ValueOfUint64(v))
 520  		return n, nil
 521  	case protoreflect.DoubleKind:
 522  		if wtyp == protowire.BytesType {
 523  			buf, n := protowire.ConsumeBytes(b)
 524  			if n < 0 {
 525  				return 0, errDecode
 526  			}
 527  			for len(buf) > 0 {
 528  				v, n := protowire.ConsumeFixed64(buf)
 529  				if n < 0 {
 530  					return 0, errDecode
 531  				}
 532  				buf = buf[n:]
 533  				list.Append(protoreflect.ValueOfFloat64(math.Float64frombits(v)))
 534  			}
 535  			return n, nil
 536  		}
 537  		if wtyp != protowire.Fixed64Type {
 538  			return 0, errUnknown
 539  		}
 540  		v, n := protowire.ConsumeFixed64(b)
 541  		if n < 0 {
 542  			return 0, errDecode
 543  		}
 544  		list.Append(protoreflect.ValueOfFloat64(math.Float64frombits(v)))
 545  		return n, nil
 546  	case protoreflect.StringKind:
 547  		if wtyp != protowire.BytesType {
 548  			return 0, errUnknown
 549  		}
 550  		v, n := protowire.ConsumeBytes(b)
 551  		if n < 0 {
 552  			return 0, errDecode
 553  		}
 554  		if strs.EnforceUTF8(fd) && !utf8.Valid(v) {
 555  			return 0, errors.InvalidUTF8(string(fd.FullName()))
 556  		}
 557  		list.Append(protoreflect.ValueOfString(string(v)))
 558  		return n, nil
 559  	case protoreflect.BytesKind:
 560  		if wtyp != protowire.BytesType {
 561  			return 0, errUnknown
 562  		}
 563  		v, n := protowire.ConsumeBytes(b)
 564  		if n < 0 {
 565  			return 0, errDecode
 566  		}
 567  		list.Append(protoreflect.ValueOfBytes(append(emptyBuf[:], v...)))
 568  		return n, nil
 569  	case protoreflect.MessageKind:
 570  		if wtyp != protowire.BytesType {
 571  			return 0, errUnknown
 572  		}
 573  		v, n := protowire.ConsumeBytes(b)
 574  		if n < 0 {
 575  			return 0, errDecode
 576  		}
 577  		m := list.NewElement()
 578  		if err := o.unmarshalMessage(v, m.Message()); err != nil {
 579  			return 0, err
 580  		}
 581  		list.Append(m)
 582  		return n, nil
 583  	case protoreflect.GroupKind:
 584  		if wtyp != protowire.StartGroupType {
 585  			return 0, errUnknown
 586  		}
 587  		v, n := protowire.ConsumeGroup(fd.Number(), b)
 588  		if n < 0 {
 589  			return 0, errDecode
 590  		}
 591  		m := list.NewElement()
 592  		if err := o.unmarshalMessage(v, m.Message()); err != nil {
 593  			return 0, err
 594  		}
 595  		list.Append(m)
 596  		return n, nil
 597  	default:
 598  		return 0, errUnknown
 599  	}
 600  }
 601  
 602  // We append to an empty array rather than a nil []byte to get non-nil zero-length byte slices.
 603  var emptyBuf [0]byte
 604