congen

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2021 License: MIT Imports: 12 Imported by: 0

README

go-congen

Go HTTP controller generator. (see example)

Can be used as CLI or library.

Utility:

  • parses HTML file
  • detects all forms
  • generates endpoints and stubs

By-default, if no response sent, 303 See Other will be sent to client.

Installation

MacOS

brew install reddec/go-congen

From binary - check releases section

As CLI from source

go get github.com/reddec/congen/cmd/...

Features

Multiple forms with same action

It supports multiple forms with same action and different fields set. Fields will be merged to one structure:

index.html

<form action="something" method="post">
    <input type="text" name="field1"/>
</form>
<form action="something" method="post">
    <input type="text" name="field2"/>
</form>

gen.go

// ...

type Controller interface {
    // ...
	DoSomething(writer http.ResponseWriter, request *http.Request, params SomethingParams) error
    // ...
}
// ...
type SomethingParams struct {
	Field1 string
	Field2 string
}
// ...
Multiple fields in the same form

In case of (un)intentional definition of fields with same names within one form only first field will be used.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Process

func Process(htmlSource io.Reader, htmlFile string, packageName string) ([]byte, error)

Process HTML source, find all defined forms and generate controller stub. Returns rendered template. templateFile or htmlSource has Name() function will be used for embedding.

func ProcessFile

func ProcessFile(htmlFile string, outputFile string, packageName string) error

ProcessFile invokes process, feeds content of htmlFile and writes result to outputFile.

Types

type Field

type Field struct {
	Type  Type
	Label string
}

func (*Field) IsBool

func (f *Field) IsBool() bool

func (*Field) IsFloat

func (f *Field) IsFloat() bool

func (*Field) IsInteger

func (f *Field) IsInteger() bool

func (*Field) IsString

func (f *Field) IsString() bool

func (*Field) Name

func (f *Field) Name() string

type Form

type Form struct {
	Action string
	Method string
	Fields []Field
}

func Forms

func Forms(reader io.Reader) ([]Form, error)

Forms from parsed HTML stream.

func (*Form) HasField

func (f *Form) HasField(label string) bool

func (*Form) MergeFields

func (f *Form) MergeFields(fields []Field)

func (*Form) Name

func (f *Form) Name() string

type Path

type Path struct {
	Name string
	Get  *Form
	Post *Form
}

type RenderEnv

type RenderEnv struct {
	Input   string
	Forms   []Form
	Package string
}

func (*RenderEnv) Paths

func (r *RenderEnv) Paths() []Path

type Type

type Type int
const (
	String Type = iota
	Double
	Integer
	Boolean
)

func (Type) String

func (t Type) String() string

Directories

Path Synopsis
cmd
Code generated by go-congen DO NOT EDIT.
Code generated by go-congen DO NOT EDIT.

Jump to

Keyboard shortcuts

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