gomvc

package module
v0.0.0-...-e539cc0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: Apache-2.0 Imports: 38 Imported by: 1

README

GO-MVC

*** Disclaimer *** There are no guarantees of API stability until the first major version is released. Think of the current state as an alpha-phase product looking for validation from users about what features make sense and what the best UX is for those features.

Installation

  • set up your GOPATH
  • make sure your regular PATH includes the go binary folder e.g. /whereveryouinstalled/go/bin
  • download dependencies
go get golang.org/x/tools/cmd/goimports
  • download cli
go get -u github.com/go-generation/go-mvc/cmd/gomvc

Commands

  • gomvc application Generate application files
  • gomvc help Help about any command
  • gomvc oa Generate controllers from an OpenAPI yml file
  • gomvc resource Generate resource files
  • gomvc seed Generate seed files
  • gomvc swagger Generate controllers from a v2 Swagger yml file

Usage

Create an application:

$ mkdir yourapplication && cd yourapplication
$ gomvc application yourapplication

Optionally create controllers from your OpenAPI or Swagger 2.0 yaml spec (json support is coming):

$ gomvc oa --spec path/to/openapi.yml

or

$ gomvc swagger --spec path/to/swagger.yml

Create more resources:

$ gomvc resource dogs

Swagger vs OpenAPI Disclaimer

For ease of parsing, the gomvc swagger command currently converts the Swagger spec into an OpenAPI 3 spec. This is done by an underlying library gomvc uses which attempts to convert backwards compatible aspects of the spec. As such there may be some side effects or ignored properties. For example, a Swagger spec lists its reused parameters in a top level section called "parameters". In OA3, everything is nested under the "components" section. When resolving object/schema refs, you may find missing elements because it will try to look for parameters under "#/components/parameters" and not #/parameters".

Generated Application Dependencies

As of now, gomvc assumes you will want to use the following dependencies:

Example steps: using sqlboiler
  1. Generate application: gomvc application petstore --dest ~/Code/petstore
  2. Design your SQL schema or at least one table
  3. Create a migration per table: migrate create -ext sql -dir migrations -seq create_users_table
  4. Fill the migration file with your SQL commands e.g. CREATE USERS (etc...)
  5. Migrate your local database: migrate -database YOUR_DATBASE_URL -path PATH_TO_YOUR_MIGRATIONS up
  6. Install sqlboiler by running make dev-dependencies
  7. Create a sqlboiler configuration per https://github.com/volatiletech/sqlboiler#getting-started (will be automatically done for you in future GO-MVC release)
  8. Start your application so that you have postgres running: docker-compose up
  9. Run sqlboiler: sqlboiler psql
  10. Run gomvc resource {{tableName}} for each of the tables you want to create an endpoint for.
  11. Verify naming conventions align with what sqlboiler generated. You might need to edit the generated controllers.
  12. Generate seeder to fill DB with test data (assumes models dir exists in your app directory): gomvc seed.
  13. Continue dev-ing as you would normally

If you're managing your schema independently, you can completely remove the migrate dependency from both your workflow and the app but you can still use sqlboiler regardless.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddActionViaAST

func AddActionViaAST(data ControllerData, routerFilePath string, destDir string)

func Application

func Application() *cobra.Command

Application is the cli command that creates new application.

func Copy

func Copy(src, dst string) error

Copy the src file to dst. Any existing file will be overwritten and will not copy file attributes. https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang

func CreateFileFromLocalTemplate

func CreateFileFromLocalTemplate(data interface{}, templateDir, destPath string)

func CreateFileFromString

func CreateFileFromString(filepath string, contents string) error

CreateFileFromString takes a filepath as the destination of the file to be created as well as the contents to be written to this file.

func CreateRouter

func CreateRouter(data RouteData, relativeTemplatePath, destDir string)

CreateRouter creates a router.go file to be used in the controllers directory

func CreateSeedCommand

func CreateSeedCommand(dest string) error

CreateSeedCommand creates a single cmd that inserts records for all models

func CreateSeederWithName

func CreateSeederWithName(structName string, destDir string) error

CreateSeederWithName creates a file with a single function. This function connects to the DB and inserts records for each model using each the model factories.

func CreateSeedersFromModels

func CreateSeedersFromModels(dir string, dest string) error

CreateSeedersFromModels iterates through the given directory finds models the creates seed files in the given destination

func CreateStructFromParameterObject

func CreateStructFromParameterObject(o *ParameterObject) (string, error)

func CreateStructFromResponseObject

func CreateStructFromResponseObject(m *ResponseModel) (string, error)

func CreateStructFromSchemaObject

func CreateStructFromSchemaObject(o *SchemaObject) (string, error)

func G

func G() *cobra.Command

G is the cli command that creates new g

func GenerateFromOA

func GenerateFromOA(oa3 *openapi3.T, dest, templateDir, configDir string)

GenerateFromOA is the primary logic for the oa command, creating controllers

func GenerateFromUserTemplate

func GenerateFromUserTemplate(name string, templateType string) error

func GetGoPrimitiveType

func GetGoPrimitiveType(t string) string

func GetGoType

func GetGoType(name string, p *Property) string

GetGoType mutates the property type to determine GoType

func GetLastPathPart

func GetLastPathPart(path string) string

func GetPackageName

func GetPackageName() string

func LoadSwaggerV2AsV3

func LoadSwaggerV2AsV3(specPath string) *openapi3.T

LoadSwaggerV2AsV3 takes the file path of a v2 Swagger file and returns a V3 representation

func LoadWithKin

func LoadWithKin(specPath string) *openapi3.T

LoadWithKin loads an OpenAPI spec into memory using the kin-openapi library

func Model

func Model() *cobra.Command

Model is the cli command that creates new model

func NewControllerStatement

func NewControllerStatement(resource string) *dst.AssignStmt

func NewRouteRegistrationStatement

func NewRouteRegistrationStatement(action Action) *dst.ExprStmt

func OA

func OA() *cobra.Command

OA is the cli command that creates a router and controller functions from an OpenAPI file

func PrintJSON

func PrintJSON(v interface{})

func Resource

func Resource() *cobra.Command

Resource is the cli command that creates new resource

func Root

func Root() *cobra.Command

Root is the function called when running `gomvc`

func RunGoFmt

func RunGoFmt(appDir string)

func RunGoImports

func RunGoImports(appDir string)

func Seed

func Seed() *cobra.Command

Seed is the cli command that creates new seeders based on structs in your application's "models" directory

func Swagger

func Swagger() *cobra.Command

Swagger is the cli command that creates a router and controller functions from a swagger file

func Title

func Title(s string) string

Types

type Action

type Action struct {
	SingularResource string
	// Resource is loosely related with what the Controller is and has many actions
	Resource string
	// Name is the function name bound to the Controller
	Name string
	// Method is the HTTP verb
	Method string `json:"method,omitempty"`
	// Path is the associated url path
	Path string `json:"path,omitempty"`
	// Handler is the generic resource action name e.g. Index, Create
	Handler string `json:"handler,omitempty"`
}

func NewCRUDActions

func NewCRUDActions(name string) []Action

type AdditionalProperties

type AdditionalProperties struct {
	Type  string
	OneOf []Type
	AnyOf []Type
}

type ControllerData

type ControllerData struct {
	ModuleName     string
	Name           string
	PluralName     string
	Path           string
	Actions        []Action
	TestPaths      []TestPath
	ErrorResponses []Response
	ORM            string
}

type File

type File struct {
	Template string
	Name     string
}

File is a GoMVC specific type to store rendering meta data with the filenames

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator wraps functionality for reading and manipulating a single OpenAPI spec

func NewGenerator

func NewGenerator(oa3 *openapi3.T) Generator

NewGenerator is a constructor for Generator

func (*Generator) CreateControllerFiles

func (oag *Generator) CreateControllerFiles(path string, pathItem *openapi3.PathItem, dest string, templateDir string) error

CreateControllerFiles creates a controller for operations found in an OpenAPI file

type GoMVCConfig

type GoMVCConfig struct {
	Denylist []string
	// contains filtered or unexported fields
}

func NewGoMVCConfig

func NewGoMVCConfig(configDir string) GoMVCConfig

NewGoMVCConfig is a constructor for a gomvc configuration read into memory

func (*GoMVCConfig) IsDenylisted

func (c *GoMVCConfig) IsDenylisted(path string) bool

type Item

type Item struct {
	Type   string `json:"type,omitempty"`
	Format string `json:"format,omitempty"`
}

type ParameterObject

type ParameterObject struct {
	Description string   `json:"description,omitempty"`
	In          string   `json:"in,omitempty"`
	Name        string   `json:"name,omitempty"`
	Required    bool     `json:"required,omitempty"`
	Schema      Property `json:"schema,omitempty"`
}

func LoadParameterObject

func LoadParameterObject(name string, r *openapi3.ParameterRef) ParameterObject

type ParameterSchema

type ParameterSchema struct {
	Format string
	Type   string
	GoType string
}

type Property

type Property struct {
	Name                 string               `json:"name,omitempty"`
	Description          string               `json:"description,omitempty"`
	Type                 string               `json:"type,omitempty"`
	Items                Item                 `json:"items,omitempty"`
	Format               string               `json:"format,omitempty"`
	Required             bool                 `json:"required,omitempty"`
	GoType               string               `json:"go_type,omitempty"`
	Ref                  string               `json:"$ref,omitempty"`
	AdditionalProperties AdditionalProperties `json:"additionalProperties,omitempty"`
}

type Response

type Response struct {
	Name string
	Code int
	Ref  string
}

func NewResponse

func NewResponse(statusCode string, resRef *openapi3.ResponseRef) Response

NewResponse is a constructor for the custom Response object

func NewResponses

func NewResponses(specResponses map[string]*openapi3.ResponseRef) []Response

NewResponses creates a list of responses from an OA3 response ref

type ResponseModel

type ResponseModel struct {
	ContentType string
	Parent      string
	Name        string
	Properties  []Property
}

func LoadResponseObject

func LoadResponseObject(name string, r *openapi3.Response) ResponseModel

type ResponseObject

type ResponseObject struct {
	Content     map[string]SchemaType `json:"content,omitempty"`
	Description string                `json:"description,omitempty"`
}

type RouteData

type RouteData struct {
	Controllers []ControllerData `json:"controllers,omitempty"`
}

type Schema

type Schema struct {
	Ref string `json:"$ref,omitempty"`
}

type SchemaObject

type SchemaObject struct {
	Name        string
	Description string              `json:"description,omitempty"`
	Properties  map[string]Property `json:"properties,omitempty"`
	Required    []string            `json:"required,omitempty"`
	Type        string              `json:"type,omitempty"`
	Title       string              `json:"title,omitempty"`
	GoType      string
	Items       Item
}

func LoadSchemaObject

func LoadSchemaObject(name string, r *openapi3.SchemaRef) SchemaObject

todo: collect enums

type SchemaType

type SchemaType struct {
	Schema Schema `json:"schema,omitempty"`
}

type TemplateHelper

type TemplateHelper struct {
	Name     string
	Function func(string) string
}

type TestPath

type TestPath struct {
	Path string
	Name string
}

type Type

type Type struct {
	Type string
}

Directories

Path Synopsis
cmd
dev

Jump to

Keyboard shortcuts

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