[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnunet-go-plugins] 02/02: all: use Utility function from caller.
From: |
gnunet |
Subject: |
[gnunet-go-plugins] 02/02: all: use Utility function from caller. |
Date: |
Thu, 03 Nov 2022 18:36:03 +0100 |
This is an automated email from the git hooks/post-receive script.
bernd-fix pushed a commit to branch master
in repository gnunet-go-plugins.
commit 52ce7081afaa9314d6c6f9326dd2d22fad2ef5bb
Author: Bernd Fix <brf@hoi-polloi.org>
AuthorDate: Thu Nov 3 17:22:38 2022 +0100
all: use Utility function from caller.
---
example-c/main.go | 5 +++++
example-go/main.go | 28 +++++++++++++++++++++-------
example-go/records.go | 12 ++++++------
reclaimid/main.go | 22 ++++++++++++++++++----
reclaimid/records.go | 33 +++++++++++++++++++--------------
5 files changed, 69 insertions(+), 31 deletions(-)
diff --git a/example-c/main.go b/example-c/main.go
index dcec5a3..aa0e10a 100644
--- a/example-c/main.go
+++ b/example-c/main.go
@@ -73,6 +73,11 @@ func NewCustomPlugin() CustomPlugin {
return CustomPlugin{}
}
+// SetUtility passes a utility function to the plugin
+// Not used in C-implementation
+func (p *CustomPlugin) SetUtility(arg any) {
+}
+
//go:embed gui.htpl
var tpl []byte
diff --git a/example-go/main.go b/example-go/main.go
index 7a6f66a..0b46c3d 100644
--- a/example-go/main.go
+++ b/example-go/main.go
@@ -95,19 +95,33 @@ func (p *CustomPlugin) Prefix(t uint32) string {
// No need to customize code beond this point...
//======================================================================
+// Utility function
+type Utility func(fcn string, args ...any) any
+
// Plugin is the instance of a custom implementation accessed by the
// gnunet-go framework
var Plugin = NewCustomPlugin()
// ExamplePlugin is an example plugin for custom records that implements
// the zonemaster.Plugin interface.
-type CustomPlugin struct{}
+type CustomPlugin struct {
+ utl Utility
+}
// NewCustomPlugin creates an initialized plugin instance
func NewCustomPlugin() CustomPlugin {
return CustomPlugin{}
}
+// SetUtility passes a utility function to the plugin
+func (p *CustomPlugin) SetUtility(arg any) {
+ var ok bool
+ p.utl, ok = arg.(func(fcn string, args ...any) any)
+ if !ok {
+ p.utl = nil
+ }
+}
+
//go:embed gui.htpl
var tpl []byte
@@ -123,7 +137,7 @@ func (p *CustomPlugin) Value(t uint32, rr []byte) (string,
error) {
if err != nil {
return "", err
}
- return rec.Value()
+ return rec.Value(p.utl)
}
// ToMap converts resource record data into GUI template variables
@@ -132,7 +146,7 @@ func (p *CustomPlugin) ToMap(t uint32, rr []byte)
(map[string]string, error) {
if err != nil {
return nil, err
}
- return rec.ToMap()
+ return rec.ToMap(p.utl)
}
// FromMap converts a GUI template variables into resource record data
@@ -141,7 +155,7 @@ func (p *CustomPlugin) FromMap(t uint32, vars
map[string]string) ([]byte, error)
if err != nil {
return nil, err
}
- if err = rec.FromMap(vars); err != nil {
+ if err = rec.FromMap(p.utl, vars); err != nil {
return nil, err
}
return data.Marshal(rec)
@@ -149,9 +163,9 @@ func (p *CustomPlugin) FromMap(t uint32, vars
map[string]string) ([]byte, error)
// CustomRecord interface impemented by custom types.
type CustomRecord interface {
- Value() (string, error)
- ToMap() (map[string]string, error)
- FromMap(map[string]string) error
+ Value(Utility) (string, error)
+ ToMap(Utility) (map[string]string, error)
+ FromMap(Utility, map[string]string) error
}
// Get record instance for given type and data
diff --git a/example-go/records.go b/example-go/records.go
index 9053a4c..e81d21e 100644
--- a/example-go/records.go
+++ b/example-go/records.go
@@ -40,19 +40,19 @@ type MyRecord1 struct {
}
// Value returns a human-readable representation of the record
-func (rec *MyRecord1) Value() (string, error) {
+func (rec *MyRecord1) Value(_ Utility) (string, error) {
return string(rec.Text), nil
}
// ToMap returns the parameter set for the record
-func (rec *MyRecord1) ToMap() (params map[string]string, err error) {
+func (rec *MyRecord1) ToMap(_ Utility) (params map[string]string, err error) {
params = make(map[string]string)
params["rectype1_text"] = string(rec.Text)
return
}
// FromMap reconstructs the record attributes from parameter list
-func (rec *MyRecord1) FromMap(params map[string]string) error {
+func (rec *MyRecord1) FromMap(_ Utility, params map[string]string) error {
text, ok := params["rectype1_text"]
if !ok {
return errors.New("rectype1_text missing")
@@ -74,12 +74,12 @@ type MyRecord2 struct {
}
// Value returns a human-readable representation of the record
-func (rec *MyRecord2) Value() (string, error) {
+func (rec *MyRecord2) Value(_ Utility) (string, error) {
return fmt.Sprintf("var1=%d,<br>var2=%d", rec.Var1, rec.Var2), nil
}
// ToMap returns the parameter set for the record
-func (rec *MyRecord2) ToMap() (params map[string]string, err error) {
+func (rec *MyRecord2) ToMap(_ Utility) (params map[string]string, err error) {
params = make(map[string]string)
params["rectype2_var1"] = strconv.Itoa(int(rec.Var1))
params["rectype2_var2"] = strconv.Itoa(int(rec.Var2))
@@ -87,7 +87,7 @@ func (rec *MyRecord2) ToMap() (params map[string]string, err
error) {
}
// FromMap reconstructs the record attributes from parameter list
-func (rec *MyRecord2) FromMap(params map[string]string) (err error) {
+func (rec *MyRecord2) FromMap(_ Utility, params map[string]string) (err error)
{
if rec.Var1, err = asInt[uint32](params, "rectype2_var1"); err != nil {
return
}
diff --git a/reclaimid/main.go b/reclaimid/main.go
index fc24ecd..34db340 100644
--- a/reclaimid/main.go
+++ b/reclaimid/main.go
@@ -93,19 +93,33 @@ func (p *CustomPlugin) Prefix(t uint32) string {
// No need to customize code beond this point...
//======================================================================
+// Utility function
+type Utility func(fcn string, args ...any) any
+
// Plugin is the instance of a custom implementation accessed by the
// gnunet-go framework
var Plugin = NewCustomPlugin()
// ExamplePlugin is an example plugin for custom records that implements
// the zonemaster.Plugin interface.
-type CustomPlugin struct{}
+type CustomPlugin struct {
+ utl Utility
+}
// NewCustomPlugin creates an initialized plugin instance
func NewCustomPlugin() CustomPlugin {
return CustomPlugin{}
}
+// SetUtility passes a utility function to the plugin
+func (p *CustomPlugin) SetUtility(arg any) {
+ var ok bool
+ p.utl, ok = arg.(func(fcn string, args ...any) any)
+ if !ok {
+ p.utl = nil
+ }
+}
+
//go:embed gui.htpl
var tpl []byte
@@ -121,7 +135,7 @@ func (p *CustomPlugin) Value(t uint32, rr []byte) (string,
error) {
if err != nil {
return "", err
}
- return rec.Value()
+ return rec.Value(p.utl)
}
// ToMap converts resource record data into GUI template variables
@@ -130,7 +144,7 @@ func (p *CustomPlugin) ToMap(t uint32, rr []byte)
(map[string]string, error) {
if err != nil {
return nil, err
}
- return rec.ToMap()
+ return rec.ToMap(p.utl)
}
// FromMap converts a GUI template variables into resource record data
@@ -139,7 +153,7 @@ func (p *CustomPlugin) FromMap(t uint32, vars
map[string]string) ([]byte, error)
if err != nil {
return nil, err
}
- if err = rec.FromMap(vars); err != nil {
+ if err = rec.FromMap(p.utl, vars); err != nil {
return nil, err
}
return data.Marshal(rec)
diff --git a/reclaimid/records.go b/reclaimid/records.go
index 6886b31..f08f04d 100644
--- a/reclaimid/records.go
+++ b/reclaimid/records.go
@@ -35,9 +35,9 @@ import (
// ReclaimRecord interface impemented by custom types.
type ReclaimRecord interface {
- Value() (string, error)
- ToMap() (map[string]string, error)
- FromMap(map[string]string) error
+ Value(Utility) (string, error)
+ ToMap(Utility) (map[string]string, error)
+ FromMap(Utility, map[string]string) error
}
// Get record instance for given type and data
@@ -82,14 +82,23 @@ type ReclaimAttribute struct {
}
// Value returns a human-readable representation of the record
-func (rec *ReclaimAttribute) Value() (string, error) {
+func (rec *ReclaimAttribute) Value(inv Utility) (string, error) {
wrt := new(bytes.Buffer)
wrt.WriteString(fmt.Sprintf("ID=%s,<br>", encodeBase32GNS(rec.ID)))
cred := encodeBase32GNS(rec.Credential)
if len(strings.ReplaceAll(cred, "0", "")) > 0 {
wrt.WriteString(fmt.Sprintf("Credential=%s,<br>", cred))
}
- wrt.WriteString(fmt.Sprintf("Type=%d,<br>", rec.Type))
+ var ok bool
+ var rtype string
+ if inv != nil {
+ rtype, ok = inv("gns_type_name", rec.Type).(string)
+ }
+ if ok {
+ wrt.WriteString(fmt.Sprintf("Type=%s,<br>", rtype))
+ } else {
+ wrt.WriteString(fmt.Sprintf("Type=%d,<br>", rec.Type))
+ }
wrt.WriteString(fmt.Sprintf("Flags=%d,<br>", rec.Flags))
name := string(rec.Name)
val := string(rec.Data)[:rec.DataLen-1]
@@ -98,7 +107,7 @@ func (rec *ReclaimAttribute) Value() (string, error) {
}
// ToMap returns the parameter set for the record
-func (rec *ReclaimAttribute) ToMap() (params map[string]string, err error) {
+func (rec *ReclaimAttribute) ToMap(inv Utility) (params map[string]string, err
error) {
params = make(map[string]string)
params["reclaim_attribute_id"] = encodeBase32GNS(rec.ID)
params["reclaim_attribute_credential"] = encodeBase32GNS(rec.Credential)
@@ -110,7 +119,7 @@ func (rec *ReclaimAttribute) ToMap() (params
map[string]string, err error) {
}
// FromMap reconstructs the record attributes from parameter list
-func (rec *ReclaimAttribute) FromMap(params map[string]string) error {
+func (rec *ReclaimAttribute) FromMap(inv Utility, params map[string]string)
error {
return nil
}
@@ -132,9 +141,7 @@ type ReclaimCredential struct {
}
// Value returns a human-readable representation of the record
-//
-//nolint:unparam // error-free
-func (rec *ReclaimCredential) Value() (string, error) {
+func (rec *ReclaimCredential) Value(inv Utility) (string, error) {
wrt := new(bytes.Buffer)
wrt.WriteString(fmt.Sprintf("ID=%s,<br>", encodeBase32GNS(rec.ID)))
wrt.WriteString(fmt.Sprintf("Type=%d,<br>", rec.Type))
@@ -146,9 +153,7 @@ func (rec *ReclaimCredential) Value() (string, error) {
}
// ToMap returns the parameter set for the record
-//
-//nolint:unparam // error-free
-func (rec *ReclaimCredential) ToMap() (params map[string]string, err error) {
+func (rec *ReclaimCredential) ToMap(inv Utility) (params map[string]string,
err error) {
params = make(map[string]string)
params["reclaim_credential_id"] = encodeBase32GNS(rec.ID)
params["reclaim_credential_type"] = fmt.Sprintf("%d", rec.Type)
@@ -159,7 +164,7 @@ func (rec *ReclaimCredential) ToMap() (params
map[string]string, err error) {
}
// FromMap reconstructs the record attributes from parameter list
-func (rec *ReclaimCredential) FromMap(params map[string]string) error {
+func (rec *ReclaimCredential) FromMap(inv Utility, params map[string]string)
error {
return nil
}
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.