Documentation
¶
Overview ¶
Package cherryProfile provides profile configuration file loading and node config resolution for the Cherry framework.
Core responsibilities:
- Load profile JSON config files (with include file merging)
- Resolve per-node configuration (node identity, address, settings)
- Provide type-safe config reading via the ProfileJSON interface
Note: this package uses package-level global state (cfg). Only one Application instance per process is supported. Multiple instances will overwrite each other's global config.
Index ¶
- func Debug() bool
- func Env() string
- func GetConfig(path ...any) cfacade.ProfileJSON
- func GetNodeWithConfig(config *Config, nodeID string) (cfacade.INode, error)
- func Init(filePath, nodeID string) (cfacade.INode, error)
- func LoadNode(nodeID string) (cfacade.INode, error)
- func Name() string
- func Path() string
- func PrintLevel() string
- type Config
- func (p *Config) GetBool(path any, defaultVal ...bool) bool
- func (p *Config) GetConfig(path ...any) cfacade.ProfileJSON
- func (p *Config) GetDuration(path any, defaultVal ...time.Duration) time.Duration
- func (p *Config) GetFloat32(path any, defaultVal ...float32) float32
- func (p *Config) GetFloat64(path any, defaultVal ...float64) float64
- func (p *Config) GetInt(path any, defaultVal ...int) int
- func (p *Config) GetInt32(path any, defaultVal ...int32) int32
- func (p *Config) GetInt64(path any, defaultVal ...int64) int64
- func (p *Config) GetString(path any, defaultVal ...string) string
- func (p *Config) GetUint(path any, defaultVal ...uint) uint
- func (p *Config) GetUint32(path any, defaultVal ...uint32) uint32
- func (p *Config) GetUint64(path any, defaultVal ...uint64) uint64
- func (p *Config) Unmarshal(value any) error
- type Node
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Env ¶ added in v1.3.0
func Env() string
Env returns the current environment name (e.g. "dev", "test", "prod").
func GetConfig ¶ added in v1.1.0
func GetConfig(path ...any) cfacade.ProfileJSON
GetConfig reads a sub-config from the global config tree at the given path. Path semantics match jsoniter.Get. Must be called after Init(), otherwise cfg.jsonConfig is nil and this will panic.
func GetNodeWithConfig ¶ added in v1.2.3
GetNodeWithConfig searches the "node" section of the profile config for a node matching nodeID and returns it as an INode.
Matching logic (via findNodeID):
- Exact string match against the "node_id" field
- Regex match if the config value looks like a regex (^...$)
- Membership check if the config value is a JSON array of IDs
func Init ¶
Init loads the profile config file and returns the configuration for the specified node.
filePath is the path to the profile JSON file, nodeID is the target node identifier.
Loading steps:
- Read the main profile config file
- Merge files referenced by the "include" field (include keys are overridden by the main config)
- Search the "node" section for a node matching nodeID
The returned INode provides the node's address, type, settings, etc. Also initializes the package-level global config (cfg) for use by Path, Name, Env, Debug, PrintLevel, and GetConfig.
func LoadNode ¶ added in v1.1.5
LoadNode looks up a node from the already-loaded global config. Must be called after Init(); panics if cfg.jsonConfig is nil.
func Path ¶ added in v1.3.0
func Path() string
Path returns the absolute path to the profile config directory.
func PrintLevel ¶ added in v1.1.29
func PrintLevel() string
PrintLevel returns the cherry log output level (e.g. "debug", "info", "warn", "error").
Types ¶
type Config ¶
Config wraps jsoniter.Any and provides type-safe config reading methods with default value support.
By embedding jsoniter.Any, Config inherits all native jsoniter methods (Get, ToString, ToBool, etc.) while adding typed getters with fallback defaults (GetString, GetBool, GetInt64, etc.) and an Unmarshal helper.
Note: because jsoniter.Any is embedded, callers can still use its native methods directly, which bypass Config's default-value logic. Prefer the typed getter methods defined on Config.
func LoadFile ¶ added in v1.4.16
LoadFile loads and merges profile config files.
Merge strategy:
- Read the main config file (fileName) into profileMaps
- Read files listed in the "include" field of the main config into includeMaps
- Merge includeMaps into rootMaps first, then merge profileMaps into rootMaps Keys in the main config override matching keys from include files (deep merge)
Returns the merged Config object.
func Wrap ¶ added in v1.1.29
Wrap creates a Config from an arbitrary value by delegating to jsoniter.Wrap.
func (*Config) GetBool ¶ added in v1.1.29
GetBool reads a bool value at path. Returns defaultVal[0] if the path is missing or invalid; returns false if no default is provided.
func (*Config) GetConfig ¶ added in v1.1.29
func (p *Config) GetConfig(path ...any) cfacade.ProfileJSON
GetConfig returns a sub-config at the given path as a ProfileJSON. Path semantics match jsoniter.Get.
func (*Config) GetDuration ¶ added in v1.2.7
GetDuration reads an integer value at path and casts it to time.Duration. Returns defaultVal[0] if the path is missing or invalid; returns 0 if no default is provided.
IMPORTANT: the returned value is a raw time.Duration (nanoseconds). Callers must multiply by the intended unit, e.g.:
delay := config.GetDuration("reconnect_delay", 1) * time.Second
func (*Config) GetFloat32 ¶ added in v1.5.3
GetFloat32 reads a float32 value at path. Returns defaultVal[0] if the path is missing or invalid; returns 0 if no default is provided.
func (*Config) GetFloat64 ¶ added in v1.5.3
GetFloat64 reads a float64 value at path. Returns defaultVal[0] if the path is missing or invalid; returns 0 if no default is provided.
func (*Config) GetInt ¶ added in v1.1.29
GetInt reads an int value at path. Returns defaultVal[0] if the path is missing or invalid; returns 0 if no default is provided.
func (*Config) GetInt32 ¶ added in v1.1.29
GetInt32 reads an int32 value at path. Returns defaultVal[0] if the path is missing or invalid; returns 0 if no default is provided.
func (*Config) GetInt64 ¶ added in v1.1.29
GetInt64 reads an int64 value at path. Returns defaultVal[0] if the path is missing or invalid; returns 0 if no default is provided.
func (*Config) GetString ¶ added in v1.1.29
GetString reads a string value at path. Returns defaultVal[0] if the path is missing or invalid; returns "" if no default is provided.
func (*Config) GetUint ¶ added in v1.5.3
GetUint reads a uint value at path. Returns defaultVal[0] if the path is missing or invalid; returns 0 if no default is provided.
func (*Config) GetUint32 ¶ added in v1.5.3
GetUint32 reads a uint32 value at path. Returns defaultVal[0] if the path is missing or invalid; returns 0 if no default is provided.
type Node ¶ added in v1.1.5
type Node struct {
// contains filtered or unexported fields
}
Node is the concrete implementation of cfacade.INode. It holds a single cluster node's identity, network addresses, and per-node settings loaded from the profile config.
func (*Node) Address ¶ added in v1.1.5
Address returns the public listen address (for frontend nodes).
func (*Node) NodeType ¶ added in v1.1.5
NodeType returns the node type (e.g. "game", "gate", "map").
func (*Node) RpcAddress ¶ added in v1.1.5
RpcAddress returns the RPC listen address (reserved).
func (*Node) Settings ¶ added in v1.1.5
func (n *Node) Settings() cfacade.ProfileJSON
Settings returns the per-node settings subtree from the profile config.