github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/v2jsonpb/follower.go (about)

     1  package v2jsonpb
     2  
     3  import (
     4  	"google.golang.org/protobuf/proto"
     5  	"google.golang.org/protobuf/reflect/protoreflect"
     6  	"google.golang.org/protobuf/types/known/anypb"
     7  
     8  	"github.com/v2fly/v2ray-core/v5/common/serial"
     9  )
    10  
    11  type V2JsonProtobufFollowerFieldDescriptor struct {
    12  	protoreflect.FieldDescriptor
    13  }
    14  
    15  type V2JsonProtobufFollower struct {
    16  	protoreflect.Message
    17  }
    18  
    19  func (v *V2JsonProtobufFollower) Type() protoreflect.MessageType {
    20  	panic("implement me")
    21  }
    22  
    23  func (v *V2JsonProtobufFollower) New() protoreflect.Message {
    24  	panic("implement me")
    25  }
    26  
    27  func (v *V2JsonProtobufFollower) Interface() protoreflect.ProtoMessage {
    28  	return v.Message.Interface()
    29  }
    30  
    31  func (v *V2JsonProtobufFollower) Range(f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) {
    32  	v.Message.Range(func(descriptor protoreflect.FieldDescriptor, value protoreflect.Value) bool {
    33  		name := descriptor.FullName()
    34  		fullname := v.Message.Descriptor().FullName()
    35  		if fullname == "google.protobuf.Any" {
    36  			switch name {
    37  			case "google.protobuf.Any.type_url":
    38  				fd := V2JsonProtobufAnyTypeFieldDescriptor{descriptor}
    39  				return f(fd, value)
    40  			case "google.protobuf.Any.value":
    41  				url := v.Message.Get(v.Message.Descriptor().Fields().ByName("type_url")).String()
    42  				fd := &V2JsonProtobufAnyValueField{descriptor, url}
    43  
    44  				bytesout := v.Message.Get(v.Message.Descriptor().Fields().Get(1)).Bytes()
    45  				v2type := serial.V2TypeFromURL(url)
    46  				instance, err := serial.GetInstance(v2type)
    47  				if err != nil {
    48  					panic(err)
    49  				}
    50  				unmarshaler := proto.UnmarshalOptions{AllowPartial: true, Resolver: anyresolverv2{backgroundResolver: serial.GetResolver()}}
    51  				err = unmarshaler.Unmarshal(bytesout, instance.(proto.Message))
    52  				if err != nil {
    53  					panic(err)
    54  				}
    55  
    56  				return f(fd, protoreflect.ValueOfMessage(&V2JsonProtobufFollower{instance.(proto.Message).ProtoReflect()}))
    57  			default:
    58  				panic("unexpected any value")
    59  			}
    60  		}
    61  		return followValue(descriptor, value, f)
    62  	})
    63  }
    64  
    65  func followValue(descriptor protoreflect.FieldDescriptor, value protoreflect.Value, f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) bool {
    66  	fd := V2JsonProtobufFollowerFieldDescriptor{descriptor}
    67  	if descriptor.Kind() == protoreflect.MessageKind {
    68  		if descriptor.IsList() {
    69  			value2 := protoreflect.ValueOfList(V2JsonProtobufListFollower{value.List()})
    70  			return f(fd, value2)
    71  		}
    72  		if descriptor.IsMap() {
    73  			value2 := protoreflect.ValueOfMap(V2JsonProtobufMapFollower{value.Map(), descriptor.MapValue()})
    74  			return f(fd, value2)
    75  		}
    76  		value2 := protoreflect.ValueOfMessage(&V2JsonProtobufFollower{value.Message()})
    77  		return f(fd, value2)
    78  	}
    79  
    80  	return f(fd, value)
    81  }
    82  
    83  func (v *V2JsonProtobufFollower) Has(descriptor protoreflect.FieldDescriptor) bool {
    84  	panic("implement me")
    85  }
    86  
    87  func (v *V2JsonProtobufFollower) Clear(descriptor protoreflect.FieldDescriptor) {
    88  	v.Message.Clear(descriptor)
    89  }
    90  
    91  func (v *V2JsonProtobufFollower) Set(descriptor protoreflect.FieldDescriptor, value protoreflect.Value) {
    92  	switch descriptor := descriptor.(type) {
    93  	case V2JsonProtobufFollowerFieldDescriptor:
    94  		v.Message.Set(descriptor.FieldDescriptor, value)
    95  	case *V2JsonProtobufFollowerFieldDescriptor:
    96  		v.Message.Set(descriptor.FieldDescriptor, value)
    97  	case *V2JsonProtobufAnyValueField:
    98  		protodata := value.Message()
    99  		bytesw, err := proto.MarshalOptions{AllowPartial: true}.Marshal(&V2JsonProtobufAnyValueFieldReturn{protodata})
   100  		if err != nil {
   101  			panic(err)
   102  		}
   103  		v.Message.Set(descriptor.FieldDescriptor, protoreflect.ValueOfBytes(bytesw))
   104  	default:
   105  		v.Message.Set(descriptor, value)
   106  	}
   107  }
   108  
   109  func (v *V2JsonProtobufFollower) Mutable(descriptor protoreflect.FieldDescriptor) protoreflect.Value {
   110  	value := v.Message.Mutable(descriptor.(V2JsonProtobufFollowerFieldDescriptor).FieldDescriptor)
   111  	if descriptor.IsList() {
   112  		return protoreflect.ValueOfList(&V2JsonProtobufListFollower{value.List()})
   113  	}
   114  	if descriptor.IsMap() {
   115  		return protoreflect.ValueOfMap(&V2JsonProtobufMapFollower{value.Map(), descriptor})
   116  	}
   117  	if descriptor.Kind() == protoreflect.MessageKind {
   118  		return protoreflect.ValueOfMessage(&V2JsonProtobufFollower{value.Message()})
   119  	}
   120  	return value
   121  }
   122  
   123  func (v *V2JsonProtobufFollower) NewField(descriptor protoreflect.FieldDescriptor) protoreflect.Value {
   124  	if _, ok := descriptor.(*V2JsonProtobufAnyValueField); ok {
   125  		url := v.Message.Get(v.Message.Descriptor().Fields().ByName("type_url")).String()
   126  		v2type := serial.V2TypeFromURL(url)
   127  		instance, err := serial.GetInstance(v2type)
   128  		if err != nil {
   129  			panic(err)
   130  		}
   131  		newvalue := protoreflect.ValueOfMessage(&V2JsonProtobufFollower{instance.(proto.Message).ProtoReflect()})
   132  		return newvalue
   133  	}
   134  
   135  	value := v.Message.NewField(descriptor.(V2JsonProtobufFollowerFieldDescriptor).FieldDescriptor)
   136  	newvalue := protoreflect.ValueOfMessage(&V2JsonProtobufFollower{value.Message()})
   137  	return newvalue
   138  }
   139  
   140  func (v *V2JsonProtobufFollower) WhichOneof(descriptor protoreflect.OneofDescriptor) protoreflect.FieldDescriptor {
   141  	panic("implement me")
   142  }
   143  
   144  func (v *V2JsonProtobufFollower) GetUnknown() protoreflect.RawFields {
   145  	panic("implement me")
   146  }
   147  
   148  func (v *V2JsonProtobufFollower) SetUnknown(fields protoreflect.RawFields) {
   149  	v.Message.SetUnknown(fields)
   150  }
   151  
   152  func (v *V2JsonProtobufFollower) IsValid() bool {
   153  	return v.Message.IsValid()
   154  }
   155  
   156  func (v *V2JsonProtobufFollower) ProtoReflect() protoreflect.Message {
   157  	return v
   158  }
   159  
   160  func (v *V2JsonProtobufFollower) Descriptor() protoreflect.MessageDescriptor {
   161  	fullname := v.Message.Descriptor().FullName()
   162  	if fullname == "google.protobuf.Any" {
   163  		desc := &V2JsonProtobufAnyTypeDescriptor{(&anypb.Any{}).ProtoReflect().Descriptor()}
   164  		return desc
   165  	}
   166  	return &V2JsonProtobufFollowerDescriptor{v.Message.Descriptor()}
   167  }
   168  
   169  func (v *V2JsonProtobufFollower) Get(fd protoreflect.FieldDescriptor) protoreflect.Value {
   170  	panic("implement me")
   171  }
   172  
   173  type V2JsonProtobufFollowerDescriptor struct {
   174  	protoreflect.MessageDescriptor
   175  }
   176  
   177  func (v *V2JsonProtobufFollowerDescriptor) Fields() protoreflect.FieldDescriptors {
   178  	return &V2JsonProtobufFollowerFields{v.MessageDescriptor.Fields()}
   179  }
   180  
   181  type V2JsonProtobufFollowerFields struct {
   182  	protoreflect.FieldDescriptors
   183  }
   184  
   185  func (v *V2JsonProtobufFollowerFields) ByJSONName(s string) protoreflect.FieldDescriptor {
   186  	return V2JsonProtobufFollowerFieldDescriptor{v.FieldDescriptors.ByJSONName(s)}
   187  }
   188  
   189  func (v *V2JsonProtobufFollowerFields) ByTextName(s string) protoreflect.FieldDescriptor {
   190  	return V2JsonProtobufFollowerFieldDescriptor{v.FieldDescriptors.ByTextName(s)}
   191  }