Skip to content

Commit

Permalink
v1.3 - close #26 #31
Browse files Browse the repository at this point in the history
  • Loading branch information
bobiverse committed Jun 15, 2024
1 parent baecb66 commit a864613
Show file tree
Hide file tree
Showing 18 changed files with 683 additions and 534 deletions.
10 changes: 5 additions & 5 deletions Param.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Param struct {
}

// NewParam ..
func NewParam(key interface{}) *Param {
func NewParam(key any) *Param {
p := &Param{
Key: fmt.Sprintf("%v", key),
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func (p *Param) replaceIn(buf []byte) []byte {
}

// SetValue - any value to string
func (p *Param) SetValue(val interface{}) {
func (p *Param) SetValue(val any) {
switch v := val.(type) {
case string:
p.Value = v
Expand Down Expand Up @@ -239,7 +239,7 @@ func (p *Param) RunTrigger(xnode *xmlNode) {

n := xnode.closestUp(ntypes)
if n == nil || n.isDeleted {
// color.Red("EMPTY parent of %v", xnode.Tag())
// aurora.Red("EMPTY parent of %v", xnode.Tag())
return
}

Expand All @@ -253,7 +253,7 @@ func (p *Param) RunTrigger(xnode *xmlNode) {
for _, wpNode := range n.parent.Nodes {
isitem, listid := wpNode.IsListItem()
if !isitem || listid != listID {
// color.Red("--- %s [%s]", wpNode, wpNode.AllContents())
// aurora.Red("--- %s [%s]", wpNode, wpNode.AllContents())
continue
}
if p.Trigger.Command == TriggerCommandRemove {
Expand All @@ -266,7 +266,7 @@ func (p *Param) RunTrigger(xnode *xmlNode) {

// Simple cases
if p.Trigger.Command == TriggerCommandRemove {
// n.printTree("TRIGGER: " + p.Trigger.String() + " " + p.Trigger.Command)
// fmt.Printf("Trigger: [%s] [%s]\t Command=[%s]\n", aurora.Blue(p.AbsoluteKey), aurora.Magenta(p.Trigger.String()), aurora.BgMagenta(p.Trigger.Command))
n.Nodes = nil
n.delete()
return
Expand Down
49 changes: 32 additions & 17 deletions ParamList.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,26 @@ import (
// ParamList ..
type ParamList []*Param

// StructParams - load params from given any struct
// Get method returns the value associated with the given name
func (plist ParamList) Get(key string) any {
for _, p := range plist {
if p.Key == key {
return p.Value
}
}
return nil
}

// Len ..
func (p ParamList) Len() int {
return len(p)
}

// AnyToParams - load params from given any struct
// 1) Convert struct to JSON
// 2) Now convert JSON to map[string]interface{}
// 2) Now convert JSON to map[string]any
// 3) Clear params from nil
func StructParams(v interface{}) ParamList {
func AnyToParams(v any) ParamList {
// to JSON output
buf, _ := json.MarshalIndent(v, "", "\t")
return JSONToParams(buf)
Expand All @@ -23,7 +38,7 @@ func StructParams(v interface{}) ParamList {
// JSONToParams - load params from JSON
func JSONToParams(buf []byte) ParamList {
// to map
m := map[string]interface{}{}
m := map[string]any{}
if err := json.Unmarshal(buf, &m); err != nil {
log.Printf("JSONToParams: %s", err)
return nil
Expand All @@ -37,17 +52,17 @@ func JSONToParams(buf []byte) ParamList {
return params
}

// walk map[string]interface{} and collect valid params
func mapToParams(m map[string]interface{}) ParamList {
// walk map[string]any and collect valid params
func mapToParams(m map[string]any) ParamList {
var params ParamList
for mKey, mVal := range m {
p := NewParam(mKey)

switch v := mVal.(type) {
case map[string]interface{}:
case map[string]any:
p.Type = StructParam
p.Params = mapToParams(v)
case []interface{}:
case []any:
p.Type = SliceParam
p.Params = sliceToParams(v)
default:
Expand All @@ -66,7 +81,7 @@ func mapToParams(m map[string]interface{}) ParamList {
}

// sliceToParams - slice of unknown - simple slice or complex
func sliceToParams(arr []interface{}) ParamList {
func sliceToParams(arr []any) ParamList {
var params ParamList

for i, val := range arr {
Expand All @@ -75,7 +90,7 @@ func sliceToParams(arr []interface{}) ParamList {
p := NewParam(i + 1)

switch v := val.(type) {
case map[string]interface{}:
case map[string]any:
p.Type = StructParam
p.Params = mapToParams(v)
default:
Expand All @@ -93,7 +108,7 @@ func sliceToParams(arr []interface{}) ParamList {
}

// StructToParams - walk struct and collect valid params
func StructToParams(paramStruct interface{}) ParamList {
func StructToParams(paramStruct any) ParamList {
var params ParamList
var keys reflect.Type
var vals reflect.Value
Expand Down Expand Up @@ -158,12 +173,12 @@ func reflectStructToParams(p *Param, val reflect.Value) {
}
p.Type = ImageParam
p.SetValue(imgVal)
} else {
p.Type = StructParam
p.Params = StructToParams(val)
return
}

return
p.Type = StructParam
p.Params = StructToParams(val)

}

// reflectSliceToParams - map slice of reflect to params
Expand Down Expand Up @@ -198,18 +213,18 @@ func reflectSliceToParams(p *Param, val reflect.Value) {

p.Params = append(p.Params, itemParam)
}

return
}

// Parse row content to param list
func rowParams(row []byte) ParamList {
// extract from raw contents
re := regexp.MustCompile(ParamPattern)
matches := re.FindAllSubmatch(row, -1)

if matches == nil || matches[0] == nil {
return nil
}

var list []*Param
for _, match := range matches {
p := NewParam(string(match[2]))
Expand Down
Loading

0 comments on commit a864613

Please sign in to comment.