GithubHelp home page GithubHelp logo

jia's Introduction

Jia

Parse the go file by libraries like go/ast, then generate the other file that related to this go file by your custom template.

中文文档

Warning

It can be used now, but still in progress. All API may be changed.

Example

Write you web api as a function. Pass all param directly.

package models

type Order struct {
	Id    int
	Title string
	Price float64
}

func FindOrderById(userid, orderid int) (*Order, error) {
	// ...
	return &Order{}, nil
}

func CreateOrder(userid int, title string, price float64) (*Order, error) {
	// ...
	return &Order{}, nil
}

Add you custom route template.

// generate by jia
package routes

import (
  "net/http"

  "github.com/relaxgo/tangram/param"
  "somepkg/models"
)

{{ range $_, $f := .ValidFuncs }}
{{if not $f.ParsedDoc.api_ignore }}
func {{ $f.Name }}(w http.ResponseWriter, r *http.Request) {
  p := NewRequestValue(r)

  {{ range $_, $p := $f.Params -}}
  {{- if $p.IsBasic -}}
    {{ $p.Name }} := param.{{firstToUpper $p.TypeName }}(p, "{{ $p.Name}}")
  {{- else -}}
    {{ $p.Name }} := &{{ $.Package }}.{{base $p.TypeName}}{}
    param.Object(p, {{ $p.Name }})
  {{- end }}
  {{ end }}

  v, err:= {{$.Package}}.{{$f.Name}}({{pluckStrings $f.Params "Name" | join ","}})
  Respond(w, r, v, err)
}
{{ end }}
{{ end }}

run go generate order.go, then http handler is ok.

// generate by jia
package routes

import (
	"net/http"

	"somepkg/models"

	"github.com/relaxgo/tangram/param"
)

func FindOrderById(w http.ResponseWriter, r *http.Request) {
	p := NewRequestValue(r)

	userid := param.Int(p, "userid")
	orderid := param.Int(p, "orderid")

	v, err := models.FindOrderById(userid, orderid)
	Respond(w, r, v, err)
}

func CreateOrder(w http.ResponseWriter, r *http.Request) {
	p := NewRequestValue(r)

	userid := param.Int(p, "userid")
	title := param.String(p, "title")
	price := param.Float64(p, "price")

	v, err := models.CreateOrder(userid, title, price)
	Respond(w, r, v, err)
}

Of course, you can write some other template and generate the file you need.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.