Skip to content

A go library to parse SenML messages (RFC 8428)

License

Notifications You must be signed in to change notification settings

nkristek/go-senml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-senml

CI Status GoDoc Go Report Card

A go library to parse SenML records. It currently supports JSON and XML.

This library implements RFC 8428 (SenML version 10).

Install

go get github.com/nkristek/go-senml

Import

import(
	"github.com/nkristek/go-senml"
)

Usage

// 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:

_, 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
	}
}