nodegopher/nodes.go

117 lines
3.2 KiB
Go
Raw Permalink Normal View History

2025-01-12 14:59:32 +01:00
// Node implement Item interface
package main
import (
"fmt"
"strconv"
)
// TODO: handle arc__ and details__ fields
type Node struct {
Name string `yaml:"name" json:"name"`
Id string `yaml:"id" json:"id"`
Title string `yaml:"title" json:"title",omitempty`
Subtitle string `yaml:"subtitle,omitempty" json:"subtitle,omitempty"`
MainStat string `yaml:"mainstat,omitempty" json:"mainstat,omitempty"`
MainStatQuery string `yaml:"mainstatquery,omitempty"`
MainStatFormat string `yaml:"mainstatformat,omitempty" json:"mainstatformat,omitempty"`
SecondaryStat string `yaml:"secondarystat,omitempty" json:"secondarystat,omitempty"`
SecondaryStatQuery string `yaml:"secondarystatquery,omitempty" json:"secondarystatquery,omitempty"`
SecondaryStatFormat string `yaml:"secondarystatformat,omitempty" json:"secondarystatformat,omitempty"`
Color string `yaml:"color,omitempty" json:"color,omitempty"`
Icon string `yaml:"icon,omitempty" json:"icon,omitempty"`
NodeRadius int `yaml:"noderadius,omitempty" json:"noderadius,omitempty"`
Highlighted bool `yaml:"highlighted,omitempty" json:"hightlighted,omitempty"`
HighlightedQuery string `yaml:"highlightedquery,omitempty" json:"highlightedquery,omitempty"`
2025-01-12 14:59:32 +01:00
}
func (n Node) GetId() string {
return n.Id
}
func (n Node) GetMainStat() string {
return n.MainStat
}
func (n Node) GetSecondaryStat() string {
return n.SecondaryStat
}
func (n Node) GetMainStatQuery() string {
return n.MainStatQuery
}
func (n Node) GetSecondaryStatQuery() string {
return n.SecondaryStatQuery
}
func (n Node) GetMainStatFormat() string {
return n.MainStatFormat
}
func (n Node) GetSecondaryStatFormat() string {
return n.SecondaryStatFormat
}
func (n *Node) SetMainStat(stat string) {
n.MainStat = fmt.Sprintf("%s", stat)
}
func (n *Node) SetSecondaryStat(stat string) {
n.SecondaryStat = fmt.Sprintf("%s", stat)
}
// Have to be implemented to satisfy Item interface
func (n *Node) GetThicknessQuery() string {
panic("Not an Edge")
return ""
}
// Have to be implemented to satisfy Item interface
func (n *Node) SetThickness(thickness float64) {
panic("Not an Edge")
}
func (n *Node) GetHighlightedQuery() string {
return n.HighlightedQuery
}
func (n *Node) SetHighlighted(highlighted bool) {
n.Highlighted = highlighted
}
2025-01-12 14:59:32 +01:00
// Custom marshaler to not send (main|secondary)statquery
func (n Node) MarshalJSON() ([]byte, error) {
jsonRes := `{"name":"` + n.Name + `","id":"` + n.Id + `"`
if len(n.Title) > 0 {
jsonRes += `,"title":"` + n.Title + `"`
}
if len(n.Subtitle) > 0 {
jsonRes += `,"subtitle":"` + n.Subtitle + `"`
}
if len(n.MainStat) > 0 {
jsonRes += `,"mainstat":"` + n.MainStat + `"`
}
if len(n.SecondaryStat) > 0 {
jsonRes += `,"secondarystat":"` + n.SecondaryStat + `"`
}
if len(n.Color) > 0 {
jsonRes += `,"color":"` + n.Color + `"`
}
if len(n.Icon) > 0 {
jsonRes += `,"icon":"` + n.Icon + `"`
}
if n.NodeRadius > 0 {
jsonRes += `,"noderadius":` + strconv.Itoa(n.NodeRadius)
}
if n.Highlighted {
2025-01-12 14:59:32 +01:00
jsonRes += `,"highlighted":true`
}
// TODO : n.StrokeDashArray
jsonRes += `}`
return []byte(jsonRes), nil
}