Copyright 2018 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package impl

import (
	
	
	
	
	
	

	
	
	pref 
)
MessageInfo provides protobuf related functionality for a given Go type that represents a message. A given instance of MessageInfo is tied to exactly one Go type, which must be a pointer to a struct type. The exported fields must be populated before any methods are called and cannot be mutated after set.
GoReflectType is the underlying message Go type and must be populated.
	GoReflectType reflect.Type // pointer to struct
Desc is the underlying message descriptor type and must be populated.
Exporter must be provided in a purego environment in order to provide access to unexported fields.
OneofWrappers is list of pointers to oneof wrapper struct types.
	OneofWrappers []interface{}

	initMu   sync.Mutex // protects all unexported fields
	initDone uint32

	reflectMessageInfo // for reflection implementation
	coderMessageInfo   // for fast-path method implementations
}
exporter is a function that returns a reference to the ith field of v, where v is a pointer to a struct. It returns nil if it does not support exporting the requested field (e.g., already exported).
type exporter func(v interface{}, i int) interface{}
getMessageInfo returns the MessageInfo for any message type that is generated by our implementation of protoc-gen-go (for v2 and on). If it is unable to obtain a MessageInfo, it returns nil.
func ( reflect.Type) *MessageInfo {
	,  := reflect.Zero().Interface().(pref.ProtoMessage)
	if ! {
		return nil
	}
	,  := .ProtoReflect().(interface{ () *MessageInfo })
	if ! {
		return nil
	}
	return .()
}

This function is called in the hot path. Inline the sync.Once logic, since allocating a closure for Once.Do is expensive. Keep init small to ensure that it can be inlined.
	if atomic.LoadUint32(&.initDone) == 0 {
		.initOnce()
	}
}

func ( *MessageInfo) () {
	.initMu.Lock()
	defer .initMu.Unlock()
	if .initDone == 1 {
		return
	}

	 := .GoReflectType
	if .Kind() != reflect.Ptr && .Elem().Kind() != reflect.Struct {
		panic(fmt.Sprintf("got %v, want *struct kind", ))
	}
	 = .Elem()

	 := .makeStructInfo()
	.makeReflectFuncs(, )
	.makeCoderMethods(, )

	atomic.StoreUint32(&.initDone, 1)
}
getPointer returns the pointer for a message, which should be of the type of the MessageInfo. If the message is of a different type, it returns ok==false.
func ( *MessageInfo) ( pref.Message) ( pointer,  bool) {
	switch m := .(type) {
	case *messageState:
		return .pointer(), .messageInfo() == 
	case *messageReflectWrapper:
		return .pointer(), .messageInfo() == 
	}
	return pointer{}, false
}

type (
	SizeCache       = int32
	WeakFields      = map[int32]protoreflect.ProtoMessage
	UnknownFields   = []byte
	ExtensionFields = map[int32]ExtensionField
)

var (
	sizecacheType       = reflect.TypeOf(SizeCache(0))
	weakFieldsType      = reflect.TypeOf(WeakFields(nil))
	unknownFieldsType   = reflect.TypeOf(UnknownFields(nil))
	extensionFieldsType = reflect.TypeOf(ExtensionFields(nil))
)

type structInfo struct {
	sizecacheOffset offset
	weakOffset      offset
	unknownOffset   offset
	extensionOffset offset

	fieldsByNumber        map[pref.FieldNumber]reflect.StructField
	oneofsByName          map[pref.Name]reflect.StructField
	oneofWrappersByType   map[reflect.Type]pref.FieldNumber
	oneofWrappersByNumber map[pref.FieldNumber]reflect.Type
}

func ( *MessageInfo) ( reflect.Type) structInfo {
	 := structInfo{
		sizecacheOffset: invalidOffset,
		weakOffset:      invalidOffset,
		unknownOffset:   invalidOffset,
		extensionOffset: invalidOffset,

		fieldsByNumber:        map[pref.FieldNumber]reflect.StructField{},
		oneofsByName:          map[pref.Name]reflect.StructField{},
		oneofWrappersByType:   map[reflect.Type]pref.FieldNumber{},
		oneofWrappersByNumber: map[pref.FieldNumber]reflect.Type{},
	}

:
	for  := 0;  < .NumField(); ++ {
		switch  := .Field(); .Name {
		case genid.SizeCache_goname, genid.SizeCacheA_goname:
			if .Type == sizecacheType {
				.sizecacheOffset = offsetOf(, .Exporter)
			}
		case genid.WeakFields_goname, genid.WeakFieldsA_goname:
			if .Type == weakFieldsType {
				.weakOffset = offsetOf(, .Exporter)
			}
		case genid.UnknownFields_goname, genid.UnknownFieldsA_goname:
			if .Type == unknownFieldsType {
				.unknownOffset = offsetOf(, .Exporter)
			}
		case genid.ExtensionFields_goname, genid.ExtensionFieldsA_goname, genid.ExtensionFieldsB_goname:
			if .Type == extensionFieldsType {
				.extensionOffset = offsetOf(, .Exporter)
			}
		default:
			for ,  := range strings.Split(.Tag.Get("protobuf"), ",") {
				if len() > 0 && strings.Trim(, "0123456789") == "" {
					,  := strconv.ParseUint(, 10, 64)
					.fieldsByNumber[pref.FieldNumber()] = 
					continue 
				}
			}
			if  := .Tag.Get("protobuf_oneof"); len() > 0 {
				.oneofsByName[pref.Name()] = 
				continue 
			}
		}
	}
Derive a mapping of oneof wrappers to fields.
	 := .OneofWrappers
	for ,  := range []string{"XXX_OneofFuncs", "XXX_OneofWrappers"} {
		if ,  := reflect.PtrTo().MethodByName();  {
			for ,  := range .Func.Call([]reflect.Value{reflect.Zero(.Type.In(0))}) {
				if ,  := .Interface().([]interface{});  {
					 = 
				}
			}
		}
	}
	for ,  := range  {
		 := reflect.TypeOf().Elem()
		 := .Field(0)
		for ,  := range strings.Split(.Tag.Get("protobuf"), ",") {
			if len() > 0 && strings.Trim(, "0123456789") == "" {
				,  := strconv.ParseUint(, 10, 64)
				.oneofWrappersByType[] = pref.FieldNumber()
				.oneofWrappersByNumber[pref.FieldNumber()] = 
				break
			}
		}
	}

	return 
}

func ( *MessageInfo) () protoreflect.Message {
	return .MessageOf(reflect.New(.GoReflectType.Elem()).Interface())
}
func ( *MessageInfo) () protoreflect.Message {
	return .MessageOf(reflect.Zero(.GoReflectType).Interface())
}