Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nkristek/go-senml
A go library to parse SenML messages (RFC 8428)
https://github.com/nkristek/go-senml
go golang hacktoberfest rfc-8428 senml
Last synced: 3 months ago
JSON representation
A go library to parse SenML messages (RFC 8428)
- Host: GitHub
- URL: https://github.com/nkristek/go-senml
- Owner: nkristek
- License: mit
- Created: 2017-06-19T12:52:23.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-11-06T23:57:38.000Z (about 5 years ago)
- Last Synced: 2024-10-06T10:12:13.295Z (3 months ago)
- Topics: go, golang, hacktoberfest, rfc-8428, senml
- Language: Go
- Homepage:
- Size: 42 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-senml
[![CI Status](https://github.com/nkristek/go-senml/workflows/CI/badge.svg)](https://github.com/nkristek/go-senml/actions)
[![GoDoc](https://godoc.org/github.com/nkristek/go-senml?status.svg)](https://godoc.org/github.com/nkristek/go-senml)
[![Go Report Card](https://goreportcard.com/badge/github.com/nkristek/go-senml)](https://goreportcard.com/report/github.com/nkristek/go-senml)A go library to parse SenML records. It currently supports JSON and XML.
This library implements [RFC 8428](https://tools.ietf.org/rfc/rfc8428.txt) (SenML version 10).
## Install
```sh
go get github.com/nkristek/go-senml
```## Import
```go
import(
"github.com/nkristek/go-senml"
)
```## Usage
```go
// parse using the encoding format
message, err := senml.Decode(payload, senml.JSON)
if err != nil {
// process error
}// resolve the message (resolve base attributes, convert relative to absolute time etc.)
resolvedMessage, err := message.Resolve()
if err != nil {
// process error
}// encode a new message
encodedMessage, err := message.Encode(senml.JSON)
if err != nil {
// process error
}
```## Error handling
If `Resolve()` returns an error it can have one of the following types:
- `InvalidNameError`
- `UnsupportedVersionError`
- `DifferentVersionError`
- `MissingValueError`Likewise, the `Encode()` and `Decode()` functions return an error of type `UnsupportedFormatError` if it was called with an unsupported format.
The error types provide extra values to parse the exact reason in code. If you need to check on the specific reason on why resolving the message has failed, the following `switch` statement should suffice:
```go
_, err := message.Resolve()
if err != nil {
switch err.(type) {
case *senml.InvalidNameError:
// do something
// for example:
invalidNameError := err.(*senml.InvalidNameError)
switch invalidNameError.Reason {
case senml.FirstCharacterInvalid:
break
case senml.ContainsInvalidCharacter:
break
case senml.Empty:
break
}
case *senml.UnsupportedVersionError:
// do something
break
case *senml.DifferentVersionError:
// do something
break
case *senml.MissingValueError:
// do something
break
default:
break
}
}
```