go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/flagpb/resolver.go (about)

     1  // Copyright 2016 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package flagpb
    16  
    17  import (
    18  	"google.golang.org/protobuf/types/descriptorpb"
    19  
    20  	"go.chromium.org/luci/common/proto/google/descutil"
    21  )
    22  
    23  // Resolver resolves type names.
    24  type Resolver interface {
    25  	// Resolve resolves a type name to
    26  	// *descriptor.DescriptorProto or *descriptor.EnumDescriptorProto.
    27  	Resolve(name string) any
    28  }
    29  
    30  // NewResolver creates a resolver for all types in a file descriptor set.
    31  // Resolving time complexity is linear.
    32  func NewResolver(set *descriptorpb.FileDescriptorSet) Resolver {
    33  	return &descriptorSetResolver{set}
    34  }
    35  
    36  type descriptorSetResolver struct {
    37  	set *descriptorpb.FileDescriptorSet
    38  }
    39  
    40  func (r *descriptorSetResolver) Resolve(name string) any {
    41  	_, o, _ := descutil.Resolve(r.set, name)
    42  	switch o := o.(type) {
    43  	case *descriptorpb.DescriptorProto, *descriptorpb.EnumDescriptorProto:
    44  		return o
    45  	default:
    46  		return nil
    47  	}
    48  }