mgokit

command module
v0.0.0-...-633417b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2018 License: MIT Imports: 7 Imported by: 0

README

MgoKit

Go Report Card

Mgokit implements a code generator which automatically generates go packages for mongodb implementations for annotated structs.

Install

go get -u github.com/gokit/mgokit

Annotations

  • @mongapi

Generate API implementation structure for a struct.

  • @mongo_method

Generate package-level functions for CRUD operation with struct.

  • @mongo

Generate simple package with Exec function for interacting with mongodb.

Examples

See Examples for all usage of provided annotations for code generation with associated results.

Usage

Running the following commands instantly generates all necessary files and packages for giving code gen.

> mgokit generate

How It works

Package Annotation

You annotate a package with the @mongo annotation which generates a basic package to interact with an underline mongodb database.

Sample below:

// Package box does ....
//@mongo
package box

Struct Annotation

  • Generating CRUD methods implementation

You annotate any giving struct with @mongo_methods which marks giving struct has a target for code generation.

All struct must have a PublicID field.

Sample below:

// User is a type defining the given user related fields for a given.
// @mongo_methods
type User struct {
	Username      string    `json:"username"`
	PublicID      string    `json:"public_id"`
	PrivateID     string    `json:"private_id"`
	Hash          string    `json:"hash"`
	TwoFactorAuth bool      `json:"two_factor_auth"`
	Created       time.Time `json:"created_at"`
	Updated       time.Time `json:"updated_at"`
}

Mgokit will generate a package which contains functions suitable for performing all CRUD operation, it defers from the @mongoapi which creates a DB struct which encapsulates all methods to itself necessary to run against a given collection, where as this creates package level functions to perform such operations, that require more input.

  • Generating API CRUD implementation

You annotate any giving struct with @mongoapi which marks giving struct has a target for code generation.

All struct must have a PublicID field.

Sample below:

// User is a type defining the given user related fields for a given.
// @mongoapi
type User struct {
	Username      string    `json:"username"`
	PublicID      string    `json:"public_id"`
	PrivateID     string    `json:"private_id"`
	Hash          string    `json:"hash"`
	TwoFactorAuth bool      `json:"two_factor_auth"`
	Created       time.Time `json:"created_at"`
	Updated       time.Time `json:"updated_at"`
}

Interfaces

Sqlkit will generate specific interfaces where each allows the internal functions validate struct validity and are able to get and consume maps containing record details.

type Validate interface{
	Validate() error
}

These are required and must be added to target struct as they are the means the generated code uses to get data to be saved and sets internal struct's fields:

type UserFields  interface {
	Fields() (map[string]interface{}, error)
}

type UserConsumer interface {
	Consume(map[string]interface{}) error
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
example
api
api/usermgo
Package usermgo provides a auto-generated package which contains a sql CRUD API for the specific User struct in package api.
Package usermgo provides a auto-generated package which contains a sql CRUD API for the specific User struct in package api.
justdb
Package justdb contains sample of solo generated mong packages.
Package justdb contains sample of solo generated mong packages.
justdb/mdb
Package mongoapi provides a auto-generated package which contains a mongo base pkg for db operations.
Package mongoapi provides a auto-generated package which contains a mongo base pkg for db operations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL