Documentation
¶
Overview ¶
Package types provides common type definitions, constants, and interfaces used throughout the ncore framework and extensions.
This package includes:
- Generic type aliases (JSON, JSONArray, StringArray)
- Extension lifecycle interfaces
- Service discovery types
- Event system types
- Platform-specific constants
- Common data structures
Type Aliases ¶
Convenient type aliases for common patterns:
type JSON = map[string]any // Generic JSON object type JSONArray = []JSON // Array of JSON objects type StringArray = []string // String slice type AnyArray = []any // Generic slice
Usage:
data := types.JSON{
"name": "John",
"age": 30,
}
items := types.JSONArray{
{"id": 1, "name": "Item 1"},
{"id": 2, "name": "Item 2"},
}
Extension Lifecycle ¶
Extensions implement these interfaces for lifecycle management:
type Extension interface {
Name() string
Version() string
Init() error
Start() error
Stop() error
}
type OptionalLifecycle interface {
PreInit() error // Before Init
PostInit() error // After Init
PreStart() error // Before Start
PostStart() error // After Start
PreStop() error // Before Stop
PostStop() error // After Stop
}
Service Discovery ¶
Types for service registration and discovery:
type ServiceInfo struct {
ID string
Name string
Address string
Port int
Tags []string
Meta map[string]string
Health ServiceHealth
}
type ServiceHealth struct {
Status string
LastCheck time.Time
FailureRate float64
}
Event System ¶
Event routing and handling types:
type EventTarget int
const (
EventTargetLocal EventTarget = 1 // Local only
EventTargetRemote EventTarget = 2 // Remote only
EventTargetAll EventTarget = 3 // Both
)
type Event struct {
Type string
Payload any
Target EventTarget
Timestamp time.Time
}
Platform Constants ¶
Platform-specific file extensions and identifiers:
const (
ExtDarwin = ".dylib" // macOS
ExtLinux = ".so" // Linux
ExtWindows = ".dll" // Windows
)
Usage:
ext := types.ExtDarwin
if runtime.GOOS == "linux" {
ext = types.ExtLinux
}
pluginPath := "plugin" + ext
Dependency Management ¶
Define extension dependencies:
type DependencyType string
const (
StrongDependency DependencyType = "strong" // Required
WeakDependency DependencyType = "weak" // Optional
)
type Dependency struct {
Name string
Version string
Type DependencyType
}
Select Options ¶
Standard structure for dropdown/select options:
type SelectOption struct {
Label string
Value string
Icon string
}
options := []types.SelectOption{
{Label: "Active", Value: "active", Icon: "check"},
{Label: "Inactive", Value: "inactive", Icon: "x"},
}
Best Practices ¶
- Use type aliases for consistency across codebase
- Implement all required lifecycle methods in extensions
- Use strong dependencies for critical components
- Define clear event types for pub/sub patterns
- Leverage SelectOption for UI consistency
Index ¶
- Constants
- func BuildTree[T TreeNode](nodes []T, sortField string) []T
- func CompareInt(a, b int) int
- func CompareString(a, b string) int
- func CompareValues(a, b any) int
- type AnyArray
- type Criterion
- type DynamicSorter
- type JSON
- type JSONArray
- type JSONData
- type MultiCriteria
- type Order
- type SelectOption
- type Sortable
- type StringArray
- type Struct
- type TreeNode
Constants ¶
const ( ExtWindows = ".dll" ExtDarwin = ".dylib" ExtLinux = ".so" )
Platform-specific extensions
const ( PlatformWindows = "windows" PlatformDarwin = "darwin" PlatformLinux = "linux" )
Platform-specific names
Variables ¶
This section is empty.
Functions ¶
func CompareInt ¶
CompareInt compares two integers. Returns -1 if a < b. Returns 1 if a > b. Returns 0 if a == b.
func CompareString ¶
CompareString compares two strings lexicographically. Returns -1 if a < b. Returns 1 if a > b. Returns 0 if a == b.
func CompareValues ¶
CompareValues compares two values and returns -1, 0, or 1. Supports int and string types. Returns -1 if a < b. Returns 1 if a > b. Returns 0 if a == b or types are not comparable.
Types ¶
type AnyArray ¶
type AnyArray = []any
AnyArray represents a slice of any type, useful for generic lists.
type Criterion ¶
type Criterion struct {
Field string `json:"field"` // Field to sort by
Order Order `json:"order"` // Sort direction
}
Criterion represents a single sorting criterion.
type DynamicSorter ¶
type DynamicSorter struct {
Data []map[string]any // Dataset to be sorted
Getter func(item map[string]any, field string) (any, error) // Field value getter
}
DynamicSorter provides a generic implementation for sorting based on criteria.
func (*DynamicSorter) Sort ¶
func (ds *DynamicSorter) Sort(criteria MultiCriteria) error
Sort sorts the dataset based on the given MultiCriteria.
type JSONData ¶
type JSONData struct {
Data JSON `json:"data,omitempty"`
}
JSONData provides utility methods for working with JSON objects.
type MultiCriteria ¶
type MultiCriteria struct {
Criteria []Criterion `json:"criteria"` // List of sorting criteria
}
MultiCriteria supports multi-field sorting.
type SelectOption ¶
type SelectOption struct {
Label string `json:"label"`
Value string `json:"value"`
Icon string `json:"icon"`
}
SelectOption represents a select option
type Sortable ¶
type Sortable interface {
Sort(criteria MultiCriteria) error // Sort method to be implemented
}
Sortable represents a sortable dataset interface.
type StringArray ¶
type StringArray = []string
StringArray represents a slice of strings, typically used for lists of string data.