Mgokit implements a code generator which automatically generates go packages for mongodb implementations for annotated structs.
go get -u github.com/gokit/mgokit
@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.
See Examples for all usage of provided annotations for code generation with associated results.
Running the following commands instantly generates all necessary files and packages for giving code gen.
> mgokit generate
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
- 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"`
}
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
}