]> git repos ~mattia - go-utility.git/commitdiff
Add comment v0.0.6
authorMattia Cabrini <dev@mattiacabrini.com>
Sat, 2 Mar 2024 13:32:21 +0000 (14:32 +0100)
committerMattia Cabrini <dev@mattiacabrini.com>
Sat, 2 Mar 2024 13:32:21 +0000 (14:32 +0100)
colour.go
db.go
editor.go
error.go
less.go
log.go
method.go
reflect.go
sync.go
sync_fmt.go

index f3cd5dfe3d2ac1bf433f948b330f1ffc14e6e0f7..59fe5cc93f4f52311ada29ef0d83aedcea8a599e 100644 (file)
--- a/colour.go
+++ b/colour.go
@@ -15,6 +15,7 @@ var (
        White         = "\033[97m"
 )
 
+// Set colours strings to ""
 func NotInteractive() {
        Reset = ""
        Red = ""
@@ -27,6 +28,7 @@ func NotInteractive() {
        White = ""
 }
 
+// Reset colours strings to their values
 func Interactive() {
        Reset = "\033[0m"
        Red = "\033[31m"
diff --git a/db.go b/db.go
index 6970bbd122df39b6b47f70919e68b463eb44c49f..4fddc115d7e48a1f82c4af6aef06403ed67f7f0a 100644 (file)
--- a/db.go
+++ b/db.go
@@ -5,11 +5,26 @@ package utility
 
 import "database/sql"
 
+/*
+To commit or rollback a transaction:
+
+  - If err is nil, tx is committed;
+  - If err is not nil, tx is rolled back.
+
+Parameters:
+
+  - tx *sql.Tx;
+  - err error.
+
+Returns:
+
+  - err error: an error generated by tx.Commit or by tx.Rollback.
+*/
 func CommitTx(tx *sql.Tx, err error) (_ error) {
        if err == nil {
                err = tx.Commit()
        } else {
-               tx.Rollback()
+               err = tx.Rollback()
        }
 
        return AppendError(err)
index a5a17b6acdbd6e06b40552129fe38000c85025bc..6d9d63210d6460e7f33239932f34f6d2d3ef2df3 100644 (file)
--- a/editor.go
+++ b/editor.go
@@ -8,6 +8,20 @@ import (
        "os/exec"
 )
 
+/*
+To run an editor on the terminal.
+
+Editor merely runs the editor program (param editorPath) by passing as first parameter a file path (param path)
+
+Parameters:
+
+  - editorPath string: the path to the editor binary;
+  - path string: the path to wich the edited file will be saved.
+
+Returns:
+
+  - err error: an error if exec.Command.Run shoud generate one.
+*/
 func Editor(editorPath string, path string) (err error) {
        editor := exec.Command(editorPath, path)
        editor.Stdin = os.Stdin
index 5440d2d9b97d1ac703a2634133f7b24c78009a67..960157d2961fbcc682be9b3fe72f5820e306f312 100644 (file)
--- a/error.go
+++ b/error.go
@@ -8,6 +8,8 @@ import (
        "runtime"
 )
 
+// Mypanic
+// If err is not nil logs a FATAL error message
 func Mypanic(err error) {
        if err == nil {
                return
@@ -16,6 +18,29 @@ func Mypanic(err error) {
        Logf(FATAL, "%v", err)
 }
 
+/*
+Takes a funcion `f` that might return an error, a void function `pre`, a void function `post`
+
+It is supposed to be used to handle deferred function calls that returns an error without ugly sintax such as:
+
+       defer func() {
+               err := fp.Close()
+
+               if err != nil {
+                       panic(err)
+               }
+       }()
+
+Using Deferrable:
+
+       defer Deferrable(fp.Close, nil, nil)
+
+What it does:
+
+  - If pre is not nil, executes pre();
+  - Executes f() and panic (using Mypanic) if `f` result is not nil;
+  - If does not panic and post is not nil, executes post.
+*/
 func Deferrable(f func() error, pre func(), post func()) {
        if pre != nil {
                pre()
@@ -26,6 +51,7 @@ func Deferrable(f func() error, pre func(), post func()) {
        }
 }
 
+// Appends to error the funcion in wich the error has been generated
 func AppendError(err error) error {
        if err == nil {
                return nil
@@ -34,6 +60,8 @@ func AppendError(err error) error {
        return fmt.Errorf("%s: %v", fileName, err)
 }
 
+// Returns the third to last function on the stack
+// Should be used only in AppendError
 func trace() (receipts []string) {
        var p = make([]uintptr, 10)
        runtime.Callers(2, p)
diff --git a/less.go b/less.go
index fe3b8636e614f3c10dc36b9f14fd24f4e1c04f73..e99a0066007a4c6edc0d2876041f022df571a483 100644 (file)
--- a/less.go
+++ b/less.go
@@ -9,11 +9,16 @@ import (
        "os/exec"
 )
 
+// Show a message on the terminal via less by executing
+// echo {string} piped through less.
+// The string is generated using Printf-like format and arguments
 func Lessf(format string, args ...interface{}) error {
        str := fmt.Sprintf(format, args...)
        return less(str)
 }
 
+// Show the string `str` on the terminal via less by executing
+// echo {str} piped through less
 func less(str string) (err error) {
        echo := exec.Command("echo", str)
        echo.Stdin = os.Stdin
diff --git a/log.go b/log.go
index e7cce03a1cab8581fc4c9bc13c1b2cfc7889ccc5..36da64c61a10a739afa690a5f66d07195980ce20 100644 (file)
--- a/log.go
+++ b/log.go
@@ -13,13 +13,14 @@ import (
 type LogLevel int
 
 const (
-       FATAL LogLevel = (1 << iota) >> 1
-       ERROR
-       WARNING
-       INFO
-       VERBOSE
+       FATAL   LogLevel = (1 << iota) >> 1 // To log an error and exit
+       ERROR                               // To log and error
+       WARNING                             // To log a warning
+       INFO                                // To log some information
+       VERBOSE                             // To log some detailed information (meant to debug)
 )
 
+// Maps log levels to strings
 var levelToString = map[LogLevel]string{
        FATAL:   " FATAL ",
        ERROR:   " ERROR ",
@@ -28,6 +29,7 @@ var levelToString = map[LogLevel]string{
        VERBOSE: "VERBOSE",
 }
 
+// Maps log levels to colours
 func getLevelColor(level LogLevel) string {
        switch level {
        case FATAL:
@@ -43,14 +45,21 @@ func getLevelColor(level LogLevel) string {
        return Gray
 }
 
+// To log synchronously
 var logGuard *sync.Mutex = &sync.Mutex{}
-var MaximumLevel LogLevel = WARNING
 
+// Minimum level to log
+// If it is WARNING, INFO and VERBOSE are not logged
+// If it is ERROR, WARNING, INFO and WARNING are not logged
+// ... and so on ...
+var MinimumLevel LogLevel = WARNING
+
+// Log a message with a specific log level; Format and args are Printf-like
 func Logf(level LogLevel, format string, args ...interface{}) {
        logGuard.Lock()
        defer logGuard.Unlock()
 
-       if level > MaximumLevel {
+       if level > MinimumLevel {
                return
        }
 
@@ -75,6 +84,7 @@ func logfCompose(level LogLevel, format string) string {
        return format
 }
 
+// Log a message on *testing.T with a specific log level; Format and args are Printf-like
 func Tlogf(t *testing.T, level LogLevel, format string, args ...interface{}) {
        logGuard.Lock()
        defer logGuard.Unlock()
index aec23667613de0d94576f35d07b77c1e173d2cd7..2d247e45daff838865ffc70056be232c66e63970 100644 (file)
--- a/method.go
+++ b/method.go
@@ -7,6 +7,8 @@ import (
        "reflect"
 )
 
+// Represent an object method, callable by invoking F and passing the very same
+// parameters that would be passed to that object method (which means NOT the receiver)
 type Method struct {
        to    reflect.Method
        numIn int
@@ -24,10 +26,16 @@ func newMethod(to reflect.Method, callable GenericFunc) (m *Method) {
        return
 }
 
+// Retuns the numer of parameters that F expects
 func (m *Method) NumIn() int {
        return m.numIn
 }
 
+/*
+Takes an integer `i` and retuns the i-th parameter kind that F would expect.
+
+Note that F do take as parameters the object method's parameters but NOT the method receiver
+*/
 func (m *Method) ParamKind(i int) reflect.Kind {
        return m.to.Type.In(i + 1).Kind()
 }
index 12b3ee0117d656337690f8d155ce023081e703af..142ca84a0b4f0a5b735d7781c28a5e03a2c625c5 100644 (file)
@@ -52,6 +52,9 @@ func newGenericFunc(obj interface{}, methodTO reflect.Method, methodVO reflect.V
        }
 }
 
+// Giveng an obj interface{}, returns a *Method from that obj.
+// GetMethod seeks a method that has name equal to name + suffix.
+// `suffix` might just be an empty string.
 func GetMethod(obj interface{}, name string, suffix string) *Method {
        to := reflect.TypeOf(obj)
        vo := reflect.ValueOf(obj)
@@ -67,6 +70,9 @@ func GetMethod(obj interface{}, name string, suffix string) *Method {
        return nil
 }
 
+// Giveng an obj interface{}, returns an interface{} which is a propery value of that onj.
+// GetProperty seeks a property that has name equal to name + suffix.
+// `suffix` might just be an empty string.
 func GetProperty(obj interface{}, name string, suffix string, tags ...string) interface{} {
        to := reflect.TypeOf(obj)
        vo := reflect.ValueOf(obj)
diff --git a/sync.go b/sync.go
index 146ae99d369575f10bcd9b3d5cbadb6ad26c8d18..3d3e77dd20f440d1004035303c51d526f18db92f 100644 (file)
--- a/sync.go
+++ b/sync.go
@@ -13,6 +13,18 @@ type rlockable interface {
        RUnlock()
 }
 
+/*
+Lock a lockable and retun a funcion to unlock the lockable.
+
+The meaning is to avoid ugly syntax such as:
+
+       mu.Lock()
+       defer mu.Unlock()
+
+Which became:
+
+       defer Monitor(mu)()
+*/
 func Monitor(mu lockable) func() {
        mu.Lock()
        return func() {
@@ -20,6 +32,18 @@ func Monitor(mu lockable) func() {
        }
 }
 
+/*
+Lock a rlockable and retun a funcion to unlock the rlockable.
+
+The meaning is to avoid ugly syntax such as:
+
+       mu.RLock()
+       defer mu.RUnlock()
+
+Which became:
+
+       defer RMonitor(mu)()
+*/
 func RMonitor(mu rlockable) func() {
        mu.RLock()
        return func() {
index db77bd372b965b77f30ba266a019f36f4a990e60..d8d0fed5f0361b650362a8251243769ffc327184 100644 (file)
@@ -12,12 +12,14 @@ import (
 
 var syncIoGuard = &sync.Mutex{}
 
+// Fprintf but synced
 func Fprintf(fp io.Writer, format string, args ...any) {
        defer Monitor(syncIoGuard)()
 
        fmt.Fprint(fp, format, args)
 }
 
+// Printf but synced
 func Printf(format string, args ...any) {
        defer Monitor(syncIoGuard)()