changeset 1371:5b9b8eabcd01

Backend: Added the API documentation of the misc package.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 27 Nov 2018 12:49:53 +0100
parents 92bce41c4fa2
children 553aadd97087 84e78d2e2d95
files pkg/misc/encode.go pkg/misc/mail.go pkg/misc/tmpfiles.go
diffstat 3 files changed, 37 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/misc/encode.go	Tue Nov 27 11:09:03 2018 +0100
+++ b/pkg/misc/encode.go	Tue Nov 27 12:49:53 2018 +0100
@@ -18,11 +18,15 @@
 	"io"
 )
 
+// BinReader is a io.Reader to support an error tolerant way
+// to read big endian encoded binary data.
 type BinReader struct {
 	io.Reader
+	// Err is the first encountered error.
 	Err error
 }
 
+// Read implements io.Reader without any endian awareness.
 func (r *BinReader) Read(buf []byte) (int, error) {
 	if r.Err != nil {
 		return 0, r.Err
@@ -32,12 +36,18 @@
 	return n, r.Err
 }
 
+// ReadBin reads big endian encodes binary data into x.
+// If an error was encountered before no data is read.
 func (r *BinReader) ReadBin(x interface{}) {
 	if r.Err == nil {
 		r.Err = binary.Read(r.Reader, binary.BigEndian, x)
 	}
 }
 
+// ReadString reads a big endian encoded string from
+// the underlying io.Reader into *s.
+// If an error was encountered before no data is read
+// and *s is unmodified.
 func (r *BinReader) ReadString(s *string) {
 	if r.Err != nil {
 		return
@@ -53,11 +63,15 @@
 	*s = string(b)
 }
 
+// BinWriter is a io.Writer to support an error tolerant way
+// to write big endian encoded binary data.
 type BinWriter struct {
 	io.Writer
+	// Err is the first encountered error.
 	Err error
 }
 
+// Write implements io.Writer without any endian awareness.
 func (w *BinWriter) Write(buf []byte) (int, error) {
 	if w.Err != nil {
 		return 0, w.Err
@@ -67,12 +81,17 @@
 	return n, w.Err
 }
 
+// WriteBin writes x big endian encoded to the underlying io.Writer.
+// If an error was encountered before no data is written.
 func (w *BinWriter) WriteBin(x interface{}) {
 	if w.Err == nil {
 		w.Err = binary.Write(w.Writer, binary.BigEndian, x)
 	}
 }
 
+// WriteString writes a big endian encoded string to
+// the underlying io.Writer.
+// If an error was encountered before no data is written.
 func (w *BinWriter) WriteString(s string) {
 	if w.Err == nil {
 		w.Err = binary.Write(w.Writer, binary.BigEndian, uint32(len(s)))
--- a/pkg/misc/mail.go	Tue Nov 27 11:09:03 2018 +0100
+++ b/pkg/misc/mail.go	Tue Nov 27 12:49:53 2018 +0100
@@ -19,10 +19,14 @@
 	"gemma.intevation.de/gemma/pkg/config"
 )
 
-func SendMail(email, subject, body string) error {
+// SendMail sends an email to a given address with a given subject
+// and body.
+// The credentials to contact the SMPT server are taken from the
+// configuration.
+func SendMail(address, subject, body string) error {
 	m := gomail.NewMessage()
 	m.SetHeader("From", config.MailFrom())
-	m.SetHeader("To", email)
+	m.SetHeader("To", address)
 	m.SetHeader("Subject", subject)
 	m.SetBody("text/plain", body)
 
--- a/pkg/misc/tmpfiles.go	Tue Nov 27 11:09:03 2018 +0100
+++ b/pkg/misc/tmpfiles.go	Tue Nov 27 12:49:53 2018 +0100
@@ -110,6 +110,8 @@
 	return nil
 }
 
+// DeleteTempFile deletes a file identified by its token
+// from the special folder of temporary files.
 func DeleteTempFile(token string) error {
 	tmpfilesMu.Lock()
 	defer tmpfilesMu.Unlock()
@@ -117,6 +119,9 @@
 	return os.RemoveAll(filepath.Join(tmp, token))
 }
 
+// UnmakeTempFile moves a file identified by a token
+// back from the special folder to a normal path.
+// This only will succeed if the token is not expired.
 func UnmakeTempFile(token, path string) error {
 	tmpfilesMu.Lock()
 	defer tmpfilesMu.Unlock()
@@ -124,6 +129,13 @@
 	return os.Rename(filepath.Join(tmp, token), path)
 }
 
+// MakeTempFile moves a file into a special temporary file folder
+// and returns a token for later accessing the file again.
+// Files in this special folder are only quaranteed to be
+// kept alive among of time. After this time expires file
+// will be likely to be deleted. There are only a limited
+// amount of temporary files allowed. If you push more
+// files into the special folder the oldest will be erased.
 func MakeTempFile(fname string) (string, error) {
 	tmpfilesMu.Lock()
 	defer tmpfilesMu.Unlock()