ref: a086faa000343f005b17d4fdcb1886bc7135cee4
parent: 1583ecf3a24b1cd53baa574d8db34a3e6c13afb0
author: halfwit <michaelmisch1985@gmail.com>
date: Fri Nov 8 07:33:58 PST 2019
Test out first plugin, may break
--- a/plugins/countries.go
+++ b/plugins/countries.go
@@ -32,10 +32,12 @@
sort.Sort(cache)
}
+// Len - For Sort implementation
func (c *countries) Len() int {
return len(c.list)
}
+// Less - For Sort implementation
func (c *countries) Less(i, j int) bool {
switch strings.Compare(c.list[i].Name.Common, c.list[j].Name.Common) {
case -1:
@@ -45,6 +47,7 @@
}
}
+// Swap - For Sort implementation
func (c *countries) Swap(i, j int) {
tmp := c.list[i]
c.list[i] = c.list[j]
--- a/plugins/doctor.go
+++ b/plugins/doctor.go
@@ -1,5 +1,10 @@
package plugins
+import (
+ "github.com/olmaxmedical/olmax_go/router"
+ "golang.org/x/text/message"
+)
+
type doctor struct {
Image string
AlmaMater string
@@ -11,7 +16,19 @@
Rate string
}
-func listdoctors() []doctor {
+func init() {
+ b := &router.Plugin{
+ Name: "List doctors",
+ Run: ListDoctors,
+ Validate: ValidateListDoctors,
+ }
+}
+
+func ValidateListDoctors() error {
+ return nil
+}
+
+func ListDoctors(p *message.Printer) []doctor {
return []doctor{
{
Image: "AbuzamzamMD.jpg",
--- a/router/plugins.go
+++ b/router/plugins.go
@@ -1,14 +1,21 @@
package router
import (
+ "fmt"
+
"golang.org/x/text/message"
)
+// DEAD is a magic string to indicate a non-unique plugin key
+const DEAD = 666
+
var pluginCache map[int]*Plugin
// Plugin - Provide extra data or functionality from GET/POST pages
type Plugin struct {
- Run func(p *message.Printer) map[string]interface{}
+ Name string
+ Run func(p *message.Printer) map[string]interface{}
+ Validate func() error
}
func init() {
@@ -20,10 +27,22 @@
// Make sure the code runs accurately without error
func ValidatePlugins() []error {
errs := []error{}
+ for key, item := range pluginCache {
+ err := item.Validate()
+ if err != nil {
+ errs = append(errs, err)
+ }
+ if (key & DEAD) != 0 {
+ errs = append(errs, fmt.Errorf("Key requested already in use for plugin %s: %d", item.Name, key|DEAD))
+ }
+ }
return errs
}
// AddPlugin - Add Plugin to map by key
func AddPlugin(p *Plugin, key int) {
+ if pluginCache[key] != nil {
+ key &= DEAD
+ }
pluginCache[key] = p
}