mirror of
https://github.com/safing/portbase
synced 2025-09-04 03:29:59 +00:00
Improve documentation and formatting
This commit is contained in:
parent
db28b35c99
commit
0b6582dd15
4 changed files with 7 additions and 27 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/tevino/abool"
|
"github.com/tevino/abool"
|
||||||
)
|
)
|
||||||
|
|
|
@ -9,10 +9,12 @@ import (
|
||||||
"github.com/tevino/abool"
|
"github.com/tevino/abool"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Client errors
|
||||||
var (
|
var (
|
||||||
ErrMalformedMessage = errors.New("malformed message")
|
ErrMalformedMessage = errors.New("malformed message")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Message is an API message.
|
||||||
type Message struct {
|
type Message struct {
|
||||||
OpID string
|
OpID string
|
||||||
Type string
|
Type string
|
||||||
|
@ -22,6 +24,7 @@ type Message struct {
|
||||||
sent *abool.AtomicBool
|
sent *abool.AtomicBool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseMessage parses the given raw data and returns a Message.
|
||||||
func ParseMessage(data []byte) (*Message, error) {
|
func ParseMessage(data []byte) (*Message, error) {
|
||||||
parts := bytes.SplitN(data, apiSeperatorBytes, 4)
|
parts := bytes.SplitN(data, apiSeperatorBytes, 4)
|
||||||
if len(parts) < 2 {
|
if len(parts) < 2 {
|
||||||
|
@ -68,6 +71,7 @@ func ParseMessage(data []byte) (*Message, error) {
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pack serializes a message into a []byte slice.
|
||||||
func (m *Message) Pack() ([]byte, error) {
|
func (m *Message) Pack() ([]byte, error) {
|
||||||
c := container.New([]byte(m.OpID), apiSeperatorBytes, []byte(m.Type))
|
c := container.New([]byte(m.OpID), apiSeperatorBytes, []byte(m.Type))
|
||||||
|
|
||||||
|
@ -90,28 +94,3 @@ func (m *Message) Pack() ([]byte, error) {
|
||||||
|
|
||||||
return c.CompileData(), nil
|
return c.CompileData(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Message) IsOk() bool {
|
|
||||||
return m.Type == MsgOk
|
|
||||||
}
|
|
||||||
func (m *Message) IsDone() bool {
|
|
||||||
return m.Type == MsgDone
|
|
||||||
}
|
|
||||||
func (m *Message) IsError() bool {
|
|
||||||
return m.Type == MsgError
|
|
||||||
}
|
|
||||||
func (m *Message) IsUpdate() bool {
|
|
||||||
return m.Type == MsgUpdate
|
|
||||||
}
|
|
||||||
func (m *Message) IsNew() bool {
|
|
||||||
return m.Type == MsgNew
|
|
||||||
}
|
|
||||||
func (m *Message) IsDelete() bool {
|
|
||||||
return m.Type == MsgDelete
|
|
||||||
}
|
|
||||||
func (m *Message) IsWarning() bool {
|
|
||||||
return m.Type == MsgWarning
|
|
||||||
}
|
|
||||||
func (m *Message) GetMessage() string {
|
|
||||||
return m.Key
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,8 +2,8 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
)
|
)
|
||||||
|
|
|
@ -82,7 +82,7 @@ func MapToJSON(mapData map[string]interface{}) ([]byte, error) {
|
||||||
for key, value := range mapData {
|
for key, value := range mapData {
|
||||||
new[key] = value
|
new[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
expand(new)
|
expand(new)
|
||||||
return json.MarshalIndent(new, "", " ")
|
return json.MarshalIndent(new, "", " ")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue