[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnunet-go-plugins] 02/03: example-go: Utility handler added.
From: |
gnunet |
Subject: |
[gnunet-go-plugins] 02/03: example-go: Utility handler added. |
Date: |
Fri, 11 Nov 2022 11:59:24 +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 925c1f974405fba6d3bf5e10e2dea3cd7bf1b0d5
Author: Bernd Fix <brf@hoi-polloi.org>
AuthorDate: Fri Nov 11 09:46:31 2022 +0100
example-go: Utility handler added.
---
example-go/Makefile | 2 +-
example-go/main.go | 24 -----------
example-go/utility.go | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 111 insertions(+), 25 deletions(-)
diff --git a/example-go/Makefile b/example-go/Makefile
index 4c96c72..79755fb 100644
--- a/example-go/Makefile
+++ b/example-go/Makefile
@@ -29,5 +29,5 @@ clean:
test:
go test -v ./...
-${GOPATH}/bin/plugin_example_go.so: main.go gui.htpl
+${GOPATH}/bin/plugin_example_go.so: main.go records.go utility.go gui.htpl
go build -buildmode=plugin -trimpath -gcflags="-N -l" -o $@ ./...
diff --git a/example-go/main.go b/example-go/main.go
index 0b46c3d..953b73a 100644
--- a/example-go/main.go
+++ b/example-go/main.go
@@ -24,8 +24,6 @@ package main
import (
_ "embed"
- "fmt"
- "strconv"
"github.com/bfix/gospel/data"
)
@@ -95,9 +93,6 @@ 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()
@@ -182,22 +177,3 @@ func (p *CustomPlugin) GetInstance(t uint32, buf []byte)
(rec CustomRecord, err
}
return
}
-
-// Numbers convertable from parameter value (string)
-type Number interface {
- uint16 | uint32 | int | int16 | int32 | float32 | float64
-}
-
-// convert parameter value (string) to given number type
-func asInt[T Number](params map[string]string, key string) (val T, err error) {
- vs, ok := params[key]
- if !ok {
- err = fmt.Errorf("%s missing", key)
- return
- }
- var v int
- if v, err = strconv.Atoi(vs); err != nil {
- return
- }
- return T(v), nil
-}
diff --git a/example-go/utility.go b/example-go/utility.go
new file mode 100644
index 0000000..091e4e8
--- /dev/null
+++ b/example-go/utility.go
@@ -0,0 +1,110 @@
+// This file is part of gnunet-go-plugins, a collection of plugins for
+// the GNUnet-implementation in Golang (gnunet.go). Plugins are used to
+// implement type-specific processing of resource records (e.g. GUI).
+// Copyright (C) 2022 by the authors:
+//
+// * Bernd Fix <brf@hoi-polloi.org> >Y<
+//
+// gnunet-go-plugins is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// gnunet-go-plugons is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+// SPDX-License-Identifier: AGPL3.0-or-later
+
+package main
+
+import (
+ "encoding/base32"
+ "fmt"
+ "strconv"
+ "strings"
+)
+
+// Utility function for exported ZoneMaster utility helpers
+type Utility func(fcn string, args ...any) any
+
+// GNSType returns the name of a GNSType
+func (u Utility) GNSType(t uint32) string {
+ rtype, ok := u("gns_type_name", t).(string)
+ if !ok {
+ return fmt.Sprintf("GNSType(%d)", t)
+ }
+ return rtype
+}
+
+// GNSFlags returns the list of GNS flags by name
+func (u Utility) GNSFlags(f uint32) string {
+ flags, ok := u("gns_flags", f).([]string)
+ if !ok {
+ return fmt.Sprintf("GNSFlags(%d)", f)
+ }
+ if len(flags) == 0 {
+ return "None"
+ }
+ return strings.Join(flags, ",")
+}
+
+// Base32GNS encoding character set
+var cf32Enc = base32.NewEncoding("0123456789ABCDEFGHJKMNPQRSTVWXYZ")
+
+// Return a byte array encoded with Base32GNS
+//
+//nolint:deadcode // might be used, might be not...
+func encodeBase32GNS(buf []byte) string {
+ return strings.TrimRight(cf32Enc.EncodeToString(buf), "=")
+}
+
+// Convert string to byte array using Base32GNS
+//
+//nolint:deadcode // might be used, might be not...
+func decodeBase32GNS(s string) ([]byte, error) {
+ s = strings.ToUpper(s)
+ s = strings.NewReplacer("O", "0", "I", "1", "L", "1", "U",
"V").Replace(s)
+ return cf32Enc.DecodeString(s)
+}
+
+// Numbers convertable from parameter value (string)
+type Number interface {
+ uint16 | uint32 | int | int16 | int32 | float32 | float64
+}
+
+// convert parameter value (string) to given number type
+func asInt[T Number](params map[string]string, key string) (val T, err error) {
+ vs, ok := params[key]
+ if !ok {
+ err = fmt.Errorf("%s missing", key)
+ return
+ }
+ var v int
+ if v, err = strconv.Atoi(vs); err != nil {
+ return
+ }
+ return T(v), nil
+}
+
+// Split a string into lines of given length
+//
+//nolint:deadcode // might be unused
+func split(s, prefix string, chunk int) (out string) {
+ for len(s) > chunk {
+ if len(out) > 0 {
+ out += ",<br>"
+ }
+ out += prefix + s[:chunk]
+ s = s[chunk:]
+ }
+ if len(out) > 0 {
+ out += ",<br>"
+ }
+ out += prefix + s
+ return
+}
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.