github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/types/wasm/wasm_config.go (about)

     1  package wasm
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/machinefi/w3bstream/pkg/enums"
     9  )
    10  
    11  func NewUserConfigurationByType(t enums.ConfigType) (Configuration, error) {
    12  	switch t {
    13  	case enums.CONFIG_TYPE__PROJECT_DATABASE:
    14  		return &Database{}, nil
    15  	case enums.CONFIG_TYPE__INSTANCE_CACHE:
    16  		return &Cache{}, nil
    17  	case enums.CONFIG_TYPE__PROJECT_ENV:
    18  		return &Env{}, nil
    19  	case enums.CONFIG_TYPE__PROJECT_FLOW:
    20  		return &Flow{}, nil
    21  	default:
    22  		return nil, errors.Errorf("invalid config type: %d", t)
    23  	}
    24  }
    25  
    26  type Configuration interface {
    27  	ConfigType() enums.ConfigType
    28  	WithContext(context.Context) context.Context
    29  }
    30  
    31  type CanBeUninit interface {
    32  	Uninit(context.Context) error
    33  }
    34  
    35  type CanBeInit interface {
    36  	Init(context.Context) error
    37  }
    38  
    39  func InitConfiguration(parent context.Context, c Configuration) error {
    40  	if canBeInit, ok := c.(CanBeInit); ok {
    41  		return canBeInit.Init(parent)
    42  	}
    43  	return nil
    44  }
    45  
    46  func UninitConfiguration(parent context.Context, c Configuration) error {
    47  	if canBeUninit, ok := c.(CanBeUninit); ok {
    48  		return canBeUninit.Uninit(parent)
    49  	}
    50  	return nil
    51  }
    52  
    53  type ConfigType string
    54  
    55  const (
    56  	ConfigLogger     ConfigType = "LOGGER"
    57  	ConfigMqttClient ConfigType = "MQTT_CLIENT"
    58  	ConfigChains     ConfigType = "CHAINS"
    59  	ConfigMetrics    ConfigType = "METRICS"
    60  )
    61  
    62  var ConfigTypes = []ConfigType{
    63  	ConfigLogger,
    64  	ConfigMqttClient,
    65  	ConfigChains,
    66  	ConfigMetrics,
    67  }
    68  
    69  func NewGlobalConfigurationByType(t ConfigType) (GlobalConfiguration, error) {
    70  	switch t {
    71  	case ConfigLogger:
    72  		return &Logger{}, nil
    73  	case ConfigMqttClient:
    74  		return &MqttClient{}, nil
    75  	case ConfigChains:
    76  		return &ChainClient{}, nil
    77  	default: // TODO case ConfigMetrics:
    78  		return nil, nil // errors.Errorf("invalid global config type: %d", t)
    79  	}
    80  }
    81  
    82  type GlobalConfiguration interface {
    83  	GlobalConfigType() ConfigType
    84  	WithContext(context.Context) context.Context
    85  }
    86  
    87  func InitGlobalConfiguration(parent context.Context, c GlobalConfiguration) error {
    88  	if canBeInit, ok := c.(CanBeInit); ok {
    89  		return canBeInit.Init(parent)
    90  	}
    91  	return nil
    92  }