github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/cptype/sdk.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 cptype
    16  
    17  import (
    18  	"context"
    19  	"strconv"
    20  	"sync"
    21  
    22  	"github.com/erda-project/erda-infra/pkg/strutil"
    23  	"github.com/erda-project/erda-infra/providers/component-protocol/protobuf/proto-go/cp/pb"
    24  	"github.com/erda-project/erda-infra/providers/i18n"
    25  )
    26  
    27  // SDK .
    28  type SDK struct {
    29  	Ctx         context.Context
    30  	Scenario    string
    31  	Tran        i18n.Translator
    32  	Identity    *pb.IdentityInfo
    33  	InParams    InParams
    34  	Lang        i18n.LanguageCodes
    35  	GlobalState *GlobalStateData
    36  
    37  	// ONLY FOR STD COMPONENT USE
    38  	Event       ComponentEvent
    39  	CompOpFuncs map[OperationKey]OperationFunc
    40  	Comp        *Component
    41  
    42  	// for parallel use, it's request level
    43  	StdStructuredPtr IStdStructuredPtr
    44  
    45  	lock      sync.Mutex
    46  	originSDK *SDK
    47  }
    48  
    49  // Clone only return general-part of sdk to avoid concurrency issue.
    50  func (sdk *SDK) Clone() *SDK {
    51  	sdk.lock.Lock()
    52  	defer sdk.lock.Unlock()
    53  
    54  	clonedSDK := SDK{
    55  		Ctx:      sdk.Ctx,
    56  		Scenario: sdk.Scenario,
    57  		Tran:     sdk.Tran,
    58  		Identity: sdk.Identity,
    59  		Lang:     sdk.Lang,
    60  
    61  		originSDK: sdk,
    62  	}
    63  	// inParams
    64  	clonedInParams := make(InParams)
    65  	for k, v := range sdk.InParams {
    66  		clonedInParams[k] = v
    67  	}
    68  	clonedSDK.InParams = clonedInParams
    69  	// globalStates
    70  	clonedGlobalStates := make(GlobalStateData)
    71  	for k, v := range *sdk.GlobalState {
    72  		clonedGlobalStates[k] = v
    73  	}
    74  	clonedSDK.GlobalState = &clonedGlobalStates
    75  
    76  	return &clonedSDK
    77  }
    78  
    79  // I18n .
    80  func (sdk *SDK) I18n(key string, args ...interface{}) string {
    81  	if len(args) == 0 {
    82  		try := sdk.Tran.Text(sdk.Lang, key)
    83  		if try != key {
    84  			return try
    85  		}
    86  	}
    87  	return sdk.Tran.Sprintf(sdk.Lang, key, args...)
    88  }
    89  
    90  // String .
    91  func (p InParams) String(key string) string {
    92  	if p == nil {
    93  		return ""
    94  	}
    95  	return strutil.String(p[key])
    96  }
    97  
    98  // Int64 .
    99  func (p InParams) Int64(key string) int64 {
   100  	if p == nil {
   101  		return 0
   102  	}
   103  	i, ok := p[key]
   104  	if !ok {
   105  		return 0
   106  	}
   107  	switch v := i.(type) {
   108  	case string:
   109  		res, _ := strconv.ParseInt(v, 10, 64)
   110  		return res
   111  	default:
   112  		res, _ := i.(int64)
   113  		return res
   114  	}
   115  }
   116  
   117  // Uint64 .
   118  func (p InParams) Uint64(key string) uint64 {
   119  	return uint64(p.Int64(key))
   120  }
   121  
   122  // RegisterOperation .
   123  func (sdk *SDK) RegisterOperation(opKey OperationKey, opFunc OperationFunc) {
   124  	sdk.CompOpFuncs[opKey] = opFunc
   125  }
   126  
   127  // SetUserIDs .
   128  func (sdk *SDK) SetUserIDs(userIDs []string) {
   129  	sdk.lock.Lock()
   130  	defer sdk.lock.Unlock()
   131  	(*sdk.GlobalState)[GlobalInnerKeyUserIDs.String()] = strutil.DedupSlice(userIDs, true)
   132  }
   133  
   134  // MergeClonedGlobalState merge cloned global state to origin unified global state.
   135  func (sdk *SDK) MergeClonedGlobalState() {
   136  	sdk.originSDK.lock.Lock()
   137  	defer sdk.originSDK.lock.Unlock()
   138  	for k, v := range *sdk.GlobalState {
   139  		(*sdk.originSDK.GlobalState)[k] = v
   140  	}
   141  }