github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/examples/components/paralleldemo/comp.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 paralleldemo
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/labstack/gommon/random"
    21  
    22  	"github.com/erda-project/erda-infra/base/servicehub"
    23  	"github.com/erda-project/erda-infra/providers/component-protocol/components/kv"
    24  	"github.com/erda-project/erda-infra/providers/component-protocol/components/kv/impl"
    25  	"github.com/erda-project/erda-infra/providers/component-protocol/cpregister"
    26  	"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
    27  	"github.com/erda-project/erda-infra/providers/component-protocol/utils/cputil"
    28  	"github.com/erda-project/erda-infra/providers/i18n"
    29  )
    30  
    31  type comp struct {
    32  	impl.DefaultKV
    33  
    34  	InParams *InParams
    35  
    36  	Tran i18n.Translator `translator:"hello"`
    37  }
    38  
    39  func (c *comp) Init(ctx servicehub.Context) error {
    40  	c.InParams = &InParams{}
    41  	return nil
    42  }
    43  
    44  // InParams .
    45  type InParams struct {
    46  	StartTime uint64 `json:"startTime,omitempty"`
    47  	EndTime   uint64 `json:"endTime,omitempty"`
    48  }
    49  
    50  func (c *comp) RegisterInitializeOp() (opFunc cptype.OperationFunc) {
    51  	return func(sdk *cptype.SDK) cptype.IStdStructuredPtr {
    52  		fmt.Printf("%p\n", c.InParams)
    53  		if c.InParams.StartTime == 0 {
    54  			panic("missing InParams.StartTime")
    55  		}
    56  		if c.InParams.EndTime == 0 {
    57  			panic("missing InParams.EndTime")
    58  		}
    59  		fmt.Println("startTime", c.InParams.StartTime)
    60  		fmt.Println("endTime", c.InParams.EndTime)
    61  		return &impl.StdStructuredPtr{
    62  			StdDataPtr: &kv.Data{List: []*kv.KV{{
    63  				Key:   sdk.Comp.Name,
    64  				Value: random.String(4),
    65  			}}},
    66  		}
    67  	}
    68  }
    69  
    70  func (c *comp) CustomInParamsPtr() interface{} {
    71  	return c.InParams
    72  }
    73  
    74  func (c *comp) EncodeFromCustomInParams(customInParamsPtr interface{}, stdInParamsPtr *cptype.ExtraMap) {
    75  	cputil.MustObjJSONTransfer(customInParamsPtr, stdInParamsPtr)
    76  }
    77  
    78  func (c *comp) DecodeToCustomInParams(stdInParamsPtr *cptype.ExtraMap, customInParamsPtr interface{}) {
    79  	cputil.MustObjJSONTransfer(stdInParamsPtr, customInParamsPtr)
    80  }
    81  
    82  func init() {
    83  	cpregister.RegisterProviderComponent("parallel-demo", "kv", &comp{})
    84  }