github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/cpregister/register_comp_provider.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     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 cpregister
    16  
    17  import (
    18  	"reflect"
    19  
    20  	"github.com/erda-project/erda-infra/base/servicehub"
    21  	"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
    22  	"github.com/erda-project/erda-infra/providers/component-protocol/utils/cputil"
    23  )
    24  
    25  // ComponentCreatorAndProvider used for RegisterProviderComponent.
    26  type ComponentCreatorAndProvider interface {
    27  	cptype.IComponent
    28  	servicehub.Provider
    29  }
    30  
    31  // Option represents options when register.
    32  type Option struct {
    33  	providerSpec *servicehub.Spec
    34  }
    35  
    36  // OpFunc do function for option.
    37  type OpFunc func(*Option)
    38  
    39  // WithCustomProviderSpecButCreator use custom spec as base but ignore creator.
    40  func (o *Option) WithCustomProviderSpecButCreator(spec *servicehub.Spec) {
    41  	o.providerSpec = spec
    42  }
    43  
    44  // RegisterProviderComponent register a provider to component. Normally this provider will use servicehub's auto dependency injection feature.
    45  func RegisterProviderComponent(scenario, componentName string, providerPtr ComponentCreatorAndProvider, opFuncs ...OpFunc) {
    46  	// handle options
    47  	opt := Option{
    48  		providerSpec: &servicehub.Spec{},
    49  	}
    50  	for _, opFunc := range opFuncs {
    51  		opFunc(&opt)
    52  	}
    53  
    54  	// generate provider name
    55  	providerName := cputil.MakeComponentProviderName(scenario, componentName)
    56  
    57  	// register component
    58  	RegisterComponent(scenario, componentName, func() cptype.IComponent {
    59  		return copyProvider(providerPtr)
    60  	})
    61  
    62  	// register as provider
    63  	opt.providerSpec.Creator = func() servicehub.Provider { return providerPtr }
    64  	servicehub.Register(providerName, opt.providerSpec)
    65  
    66  	// mark for auto servicehub config adding
    67  	AllExplicitProviderCreatorMap[providerName] = nil
    68  }
    69  
    70  // copyProvider return a copied provider:
    71  // - copied-ptr-value is original
    72  // - copied-non-ptr value is new
    73  func copyProvider(providerPtr ComponentCreatorAndProvider) ComponentCreatorAndProvider {
    74  	newProviderPtr := reflect.New(reflect.TypeOf(providerPtr).Elem())
    75  	newProviderPtr.Elem().Set(reflect.ValueOf(providerPtr).Elem())
    76  	copied := newProviderPtr.Interface()
    77  	return copied.(ComponentCreatorAndProvider)
    78  }