istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/schema/collection/schema.go (about)

     1  // Copyright Istio 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 collection
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"istio.io/istio/pkg/config"
    21  	"istio.io/istio/pkg/config/schema/resource"
    22  )
    23  
    24  // Schema for a collection.
    25  type Schema interface {
    26  	fmt.Stringer
    27  
    28  	// VariableName is a utility method used to help with codegen. It provides the name of a Schema instance variable.
    29  	VariableName() string
    30  
    31  	// Resource is the schema for resources contained in this collection.
    32  	Resource() resource.Schema
    33  
    34  	// Equal is a helper function for testing equality between Schema instances. This supports comparison
    35  	// with the cmp library.
    36  	Equal(other Schema) bool
    37  }
    38  
    39  // Builder is config for the creation of a Schema
    40  type Builder struct {
    41  	VariableName string
    42  	Resource     resource.Schema
    43  }
    44  
    45  // Build a Schema instance.
    46  func (b Builder) Build() (Schema, error) {
    47  	if b.Resource == nil {
    48  		return nil, fmt.Errorf("collection %s: resource must be non-nil", b.VariableName)
    49  	}
    50  
    51  	return &schemaImpl{
    52  		variableName: b.VariableName,
    53  		resource:     b.Resource,
    54  	}, nil
    55  }
    56  
    57  // MustBuild calls Build and panics if it fails.
    58  func (b Builder) MustBuild() Schema {
    59  	s, err := b.Build()
    60  	if err != nil {
    61  		panic(fmt.Sprintf("MustBuild: %v", err))
    62  	}
    63  
    64  	return s
    65  }
    66  
    67  type schemaImpl struct {
    68  	resource     resource.Schema
    69  	name         config.GroupVersionKind
    70  	variableName string
    71  }
    72  
    73  // String interface method implementation.
    74  func (s *schemaImpl) String() string {
    75  	return fmt.Sprintf("[Schema](%s, %q, %s)", s.name, s.resource.ProtoPackage(), s.resource.Proto())
    76  }
    77  
    78  func (s *schemaImpl) VariableName() string {
    79  	return s.variableName
    80  }
    81  
    82  func (s *schemaImpl) Resource() resource.Schema {
    83  	return s.resource
    84  }
    85  
    86  func (s *schemaImpl) Equal(o Schema) bool {
    87  	return s.variableName == o.VariableName() &&
    88  		s.Resource().Equal(o.Resource())
    89  }