Go FRM Module

In-memory database package

Usage

Install module in your project:

go get github.com/xpartacvs/go-frm

Use the packages:

package main

import (
	"log"

	"github.com/xpartacvs/go-frm/item"
	"github.com/xpartacvs/go-frm/repository"
)

func main() {
	// Create a new repository
	repo, err := repository.NewRepository("your-repository-name")
	if err != nil {
		log.Fatalln(err.Error())
	}

	// Add a JSON item into repository
	if err := repo.Register("json-item", `{"key":"value"}`, item.JSONType); err != nil {
		log.Fatalln(err.Error())
	}

	// Add a XML item into repository
	if err := repo.Register("xml-item", `<key>value</key>`, item.XMLType); err != nil {
		log.Fatalln(err.Error())
	}

	// Retrieve a JSON item with the given key
	jsonItem, err := repo.Retrieve("json-item")
	if err != nil {
		log.Fatalln(err.Error())
	}
	log.Default().Printf("JSON Name\t: %s\n", jsonItem.Name)
	log.Default().Printf("JSON Value\t: %s\n", jsonItem.Value)

	// Retrieve a XML item with the given key
	xmlItem, err := repo.Retrieve("xml-item")
	if err != nil {
		log.Fatalln(err.Error())
	}
	log.Default().Printf("XML Name\t: %s\n", xmlItem.Name)
	log.Default().Printf("XML Value\t: %s\n", xmlItem.Value)

	// Get type of an intem
	jsonType, err := repo.GetType("json-item")
	if err != nil {
		log.Fatalln(err.Error())
	}
	switch jsonType {
	case item.JSONType:
		log.Default().Println("Its a JSONType")
	case item.XMLType:
		log.Default().Println("Its a XMLType")
	}

	// Delete a XML item with the given key
	if err := repo.Deregister("xml-item"); err != nil {
		log.Fatalln(err.Error())
	}

}

Unit Testing

To run unit testing, execute the following command:

go test -v ./...

GitHub

View Github