From 48aad344bd0e17d5cefdcb04594fa333cb680f97 Mon Sep 17 00:00:00 2001 From: colinlyguo Date: Thu, 22 Aug 2024 02:46:54 +0800 Subject: [PATCH] move same implementations to util package --- bridge-history-api/internal/config/config.go | 94 +------------------- common/utils/utils.go | 88 ++++++++++++++++++ coordinator/internal/config/config.go | 93 +------------------ rollup/internal/config/config.go | 92 +------------------ 4 files changed, 95 insertions(+), 272 deletions(-) diff --git a/bridge-history-api/internal/config/config.go b/bridge-history-api/internal/config/config.go index 912e2d7bca..4b738e63b6 100644 --- a/bridge-history-api/internal/config/config.go +++ b/bridge-history-api/internal/config/config.go @@ -2,21 +2,18 @@ package config import ( "encoding/json" - "fmt" "os" "path/filepath" - "reflect" - "strconv" - "strings" "scroll-tech/common/database" + "scroll-tech/common/utils" ) // FetcherConfig is the configuration of Layer1 or Layer2 fetcher. type FetcherConfig struct { Confirmation uint64 `json:"confirmation"` Endpoint string `json:"endpoint"` - StartHeight uint64 `json:"startHeight"` + StartHeight uint64 `json:"startHeight"` // Can only be configured to contract deployment height, message proof should be updated from the very beginning. BlockTime int64 `json:"blockTime"` FetchLimit uint64 `json:"fetchLimit"` MessengerAddr string `json:"MessengerAddr"` @@ -71,95 +68,10 @@ func NewConfig(file string) (*Config, error) { } // Override config with environment variables - err = overrideConfigWithEnv(cfg, "SCROLL_BRIDGE_HISTORY") + err = utils.OverrideConfigWithEnv(cfg, "SCROLL_BRIDGE_HISTORY") if err != nil { return nil, err } return cfg, nil } - -// overrideConfigWithEnv recursively overrides config values with environment variables -func overrideConfigWithEnv(cfg interface{}, prefix string) error { - v := reflect.ValueOf(cfg) - if v.Kind() != reflect.Ptr || v.IsNil() { - return nil - } - v = v.Elem() - - t := v.Type() - for i := 0; i < t.NumField(); i++ { - field := t.Field(i) - fieldValue := v.Field(i) - - if !fieldValue.CanSet() { - continue - } - - tag := field.Tag.Get("json") - if tag == "" { - tag = strings.ToLower(field.Name) - } - - envKey := prefix + "_" + strings.ToUpper(tag) - - switch fieldValue.Kind() { - case reflect.Ptr: - if !fieldValue.IsNil() { - err := overrideConfigWithEnv(fieldValue.Interface(), envKey) - if err != nil { - return err - } - } - case reflect.Struct: - err := overrideConfigWithEnv(fieldValue.Addr().Interface(), envKey) - if err != nil { - return err - } - default: - if envValue, exists := os.LookupEnv(envKey); exists { - err := setField(fieldValue, envValue) - if err != nil { - return err - } - } - } - } - - return nil -} - -// setField sets the value of a field based on the environment variable value -func setField(field reflect.Value, value string) error { - switch field.Kind() { - case reflect.String: - field.SetString(value) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - intValue, err := strconv.ParseInt(value, 10, 64) - if err != nil { - return err - } - field.SetInt(intValue) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - uintValue, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return err - } - field.SetUint(uintValue) - case reflect.Float32, reflect.Float64: - floatValue, err := strconv.ParseFloat(value, 64) - if err != nil { - return err - } - field.SetFloat(floatValue) - case reflect.Bool: - boolValue, err := strconv.ParseBool(value) - if err != nil { - return err - } - field.SetBool(boolValue) - default: - return fmt.Errorf("unsupported type: %v", field.Kind()) - } - return nil -} diff --git a/common/utils/utils.go b/common/utils/utils.go index db3552179b..d4409dd7c3 100644 --- a/common/utils/utils.go +++ b/common/utils/utils.go @@ -9,6 +9,9 @@ import ( "math/big" "os" "path/filepath" + "reflect" + "strconv" + "strings" "time" "github.com/modern-go/reflect2" @@ -78,3 +81,88 @@ func ReadGenesis(genesisPath string) (*core.Genesis, error) { } return genesis, file.Close() } + +// OverrideConfigWithEnv recursively overrides config values with environment variables +func OverrideConfigWithEnv(cfg interface{}, prefix string) error { + v := reflect.ValueOf(cfg) + if v.Kind() != reflect.Ptr || v.IsNil() { + return nil + } + v = v.Elem() + + t := v.Type() + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + fieldValue := v.Field(i) + + if !fieldValue.CanSet() { + continue + } + + tag := field.Tag.Get("json") + if tag == "" { + tag = strings.ToLower(field.Name) + } + + envKey := prefix + "_" + strings.ToUpper(tag) + + switch fieldValue.Kind() { + case reflect.Ptr: + if !fieldValue.IsNil() { + err := OverrideConfigWithEnv(fieldValue.Interface(), envKey) + if err != nil { + return err + } + } + case reflect.Struct: + err := OverrideConfigWithEnv(fieldValue.Addr().Interface(), envKey) + if err != nil { + return err + } + default: + if envValue, exists := os.LookupEnv(envKey); exists { + err := setField(fieldValue, envValue) + if err != nil { + return err + } + } + } + } + + return nil +} + +// setField sets the value of a field based on the environment variable value +func setField(field reflect.Value, value string) error { + switch field.Kind() { + case reflect.String: + field.SetString(value) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + intValue, err := strconv.ParseInt(value, 10, 64) + if err != nil { + return err + } + field.SetInt(intValue) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + uintValue, err := strconv.ParseUint(value, 10, 64) + if err != nil { + return err + } + field.SetUint(uintValue) + case reflect.Float32, reflect.Float64: + floatValue, err := strconv.ParseFloat(value, 64) + if err != nil { + return err + } + field.SetFloat(floatValue) + case reflect.Bool: + boolValue, err := strconv.ParseBool(value) + if err != nil { + return err + } + field.SetBool(boolValue) + default: + return fmt.Errorf("unsupported type: %v", field.Kind()) + } + return nil +} diff --git a/coordinator/internal/config/config.go b/coordinator/internal/config/config.go index 95fbbae6f0..2f45d68ff8 100644 --- a/coordinator/internal/config/config.go +++ b/coordinator/internal/config/config.go @@ -2,14 +2,11 @@ package config import ( "encoding/json" - "fmt" "os" "path/filepath" - "reflect" - "strconv" - "strings" "scroll-tech/common/database" + "scroll-tech/common/utils" ) // ProverManager loads sequencer configuration items. @@ -77,96 +74,10 @@ func NewConfig(file string) (*Config, error) { } // Override config with environment variables - err = overrideConfigWithEnv(cfg, "SCROLL_COORDINATOR") + err = utils.OverrideConfigWithEnv(cfg, "SCROLL_COORDINATOR") if err != nil { return nil, err } return cfg, nil } - -// overrideConfigWithEnv recursively overrides config values with environment variables -func overrideConfigWithEnv(cfg interface{}, prefix string) error { - v := reflect.ValueOf(cfg) - if v.Kind() != reflect.Ptr || v.IsNil() { - return nil - } - v = v.Elem() - - t := v.Type() - for i := 0; i < t.NumField(); i++ { - field := t.Field(i) - fieldValue := v.Field(i) - - if !fieldValue.CanSet() { - continue - } - - tag := field.Tag.Get("json") - if tag == "" { - tag = strings.ToLower(field.Name) - } - - envKey := prefix + "_" + strings.ToUpper(tag) - - switch fieldValue.Kind() { - case reflect.Ptr: - if !fieldValue.IsNil() { - err := overrideConfigWithEnv(fieldValue.Interface(), envKey) - if err != nil { - return err - } - } - case reflect.Struct: - err := overrideConfigWithEnv(fieldValue.Addr().Interface(), envKey) - if err != nil { - return err - } - default: - if envValue, exists := os.LookupEnv(envKey); exists { - err := setField(fieldValue, envValue) - if err != nil { - return err - } - } - } - } - - return nil -} - -// setField sets the value of a field based on the environment variable value -func setField(field reflect.Value, value string) error { - switch field.Kind() { - case reflect.String: - field.SetString(value) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - intValue, err := strconv.ParseInt(value, 10, 64) - if err != nil { - return err - } - field.SetInt(intValue) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - uintValue, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return err - } - field.SetUint(uintValue) - case reflect.Float32, reflect.Float64: - floatValue, err := strconv.ParseFloat(value, 64) - if err != nil { - return err - } - field.SetFloat(floatValue) - case reflect.Bool: - boolValue, err := strconv.ParseBool(value) - if err != nil { - return err - } - field.SetBool(boolValue) - default: - return fmt.Errorf("unsupported type: %v", field.Kind()) - } - return nil - -} diff --git a/rollup/internal/config/config.go b/rollup/internal/config/config.go index 9c88bbe653..673f414f12 100644 --- a/rollup/internal/config/config.go +++ b/rollup/internal/config/config.go @@ -2,14 +2,11 @@ package config import ( "encoding/json" - "fmt" "os" "path/filepath" - "reflect" - "strconv" - "strings" "scroll-tech/common/database" + "scroll-tech/common/utils" ) // Config load configuration items. @@ -33,95 +30,10 @@ func NewConfig(file string) (*Config, error) { } // Override config with environment variables - err = overrideConfigWithEnv(cfg, "SCROLL_ROLLUP") + err = utils.OverrideConfigWithEnv(cfg, "SCROLL_ROLLUP") if err != nil { return nil, err } return cfg, nil } - -// overrideConfigWithEnv recursively overrides config values with environment variables -func overrideConfigWithEnv(cfg interface{}, prefix string) error { - v := reflect.ValueOf(cfg) - if v.Kind() != reflect.Ptr || v.IsNil() { - return nil - } - v = v.Elem() - - t := v.Type() - for i := 0; i < t.NumField(); i++ { - field := t.Field(i) - fieldValue := v.Field(i) - - if !fieldValue.CanSet() { - continue - } - - tag := field.Tag.Get("json") - if tag == "" { - tag = strings.ToLower(field.Name) - } - - envKey := prefix + "_" + strings.ToUpper(tag) - - switch fieldValue.Kind() { - case reflect.Ptr: - if !fieldValue.IsNil() { - err := overrideConfigWithEnv(fieldValue.Interface(), envKey) - if err != nil { - return err - } - } - case reflect.Struct: - err := overrideConfigWithEnv(fieldValue.Addr().Interface(), envKey) - if err != nil { - return err - } - default: - if envValue, exists := os.LookupEnv(envKey); exists { - err := setField(fieldValue, envValue) - if err != nil { - return err - } - } - } - } - - return nil -} - -// setField sets the value of a field based on the environment variable value -func setField(field reflect.Value, value string) error { - switch field.Kind() { - case reflect.String: - field.SetString(value) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - intValue, err := strconv.ParseInt(value, 10, 64) - if err != nil { - return err - } - field.SetInt(intValue) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - uintValue, err := strconv.ParseUint(value, 10, 64) - if err != nil { - return err - } - field.SetUint(uintValue) - case reflect.Float32, reflect.Float64: - floatValue, err := strconv.ParseFloat(value, 64) - if err != nil { - return err - } - field.SetFloat(floatValue) - case reflect.Bool: - boolValue, err := strconv.ParseBool(value) - if err != nil { - return err - } - field.SetBool(boolValue) - default: - return fmt.Errorf("unsupported type: %v", field.Kind()) - } - return nil -}