Giter Site home page Giter Site logo

gs2's Introduction

GS2 decoder/encoder

Tool for decoding and encoding data according to the GS2 format (https://sintef.brage.unit.no/sintef-xmlui/handle/11250/2391930).

Current version is mostly forgiving. Mainly because there is a a lot of variation in the applications of this format in pratice.

Inspired by go json decoding/encoding, but does not support custom types as of yet.

Usage

package main

import (
	"github.com/rejlersembriq/gs2"
	"log"
	"os"
)

func main() {
	file, _ := os.Open("someGS2File.gs2")

	// Decode
	g, err := gs2.NewDecoder(file).Decode()
	if err != nil {
		log.Fatalf("error decoding: %v", err)
	}

	// Encode
	if err = gs2.NewEncoder(file).Encode(g); err != nil {
		log.Fatalf("error encoding: %v", err)
	}
}

Encoder/Decoder Options

Current options supported:

  • Decoder
    • DecodeValidators (slice of Validator to be run on GS2 object after decoding)
  • Encoder
    • EncodeValidators (slice of Validator to be run on GS2 object before encoding)
    • EncodeFloatPrecision (sets float precision when encoding floats. Default -1 = auto)

Validator

A validator is simply a function with a pointer to a GS2 object as an argument and an error as a return value.

type Validator func(*GS2) error

If you need some kind of validation that is not already defined you can define your own Validators and add them to the Encoder/Decoder before encoding/decoding. NB: When adding Validators manually remeber to also add the default validators if they are needed. Validators can also be disabled by providing an empty slice.

Example

package main

import (
	"fmt"
	"github.com/rejlersembriq/gs2"
	"log"
	"os"
)

func customValidator(g *gs2.GS2) error {
	if g.StartMessage.Description == "" {
		return fmt.Errorf("startmessage needs a description")
	}

	return nil
}

func main() {
	file, _ := os.Open("someGS2File.gs2")

	g := gs2.GS2{}

	options := []gs2.EncoderOption{
		gs2.EncodeFloatPrecision(4),
		gs2.EncodeValidators(
			gs2.ValidateNoOfObjects,
			gs2.ValidateTimeSeriesValues,
			customValidator,
		),
	}

	// Encode
	encoder := gs2.NewEncoder(file, options...)
	if err := encoder.Encode(&g); err != nil {
		log.Fatalf("error encoding: %v", err)
	}
}

gs2's People

Contributors

larwef avatar

Watchers

James Cloos avatar  avatar

Forkers

erifol 3lvia

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.