Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DSL Code Generation #537

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ GO_FILES=$(shell find . -type f -name '*.go')
# Standard dependencies are installed via go get
DEPEND=\
golang.org/x/tools/cmd/goimports@latest \
honnef.co/go/tools/cmd/staticcheck@latest \
github.com/golangci/golangci-lint/cmd/golangci-lint@latest \
github.com/mjibson/esc@latest

all: lint check-generated test
Expand All @@ -41,8 +41,8 @@ ifneq ($(GOOS),windows)
@if [ "`goimports -l $(GO_FILES) | tee /dev/stderr`" ]; then \
echo "^ - Repo contains improperly formatted go files" && echo && exit 1; \
fi
@if [ "`staticcheck ./... | tee /dev/stderr`" ]; then \
echo "^ - staticcheck errors!" && echo && exit 1; \
@if [ "`golangci-lint run ./... | tee /dev/stderr`" ]; then \
echo "^ - golangci-lint errors!" && echo && exit 1; \
fi
endif

Expand Down
14 changes: 10 additions & 4 deletions cmd/mdl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,19 @@ func main() {
switch cmd {
case "gen":
addGlobals(genset)
genset.Parse(os.Args[idx:])
if err := genset.Parse(os.Args[idx:]); err != nil {
fail(err.Error())
}
case "serve":
addGlobals(svrset)
svrset.Parse(os.Args[idx:])
if err := svrset.Parse(os.Args[idx:]); err != nil {
fail(err.Error())
}
default:
addGlobals(gset)
gset.Parse(os.Args[idx:])
if err := gset.Parse(os.Args[idx:]); err != nil {
fail(err.Error())
}
}

if *h || *help {
Expand Down Expand Up @@ -154,7 +160,7 @@ func printUsage(fss ...*flag.FlagSet) {
}
}

func fail(format string, args ...interface{}) {
func fail(format string, args ...any) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
}
43 changes: 37 additions & 6 deletions cmd/mdl/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"sync"

"goa.design/model/codegen"
"goa.design/model/mdl"
)

Expand All @@ -31,7 +32,7 @@ type (
}

// Layout is position info saved for one view (diagram)
Layout = map[string]interface{}
Layout = map[string]any

// Layouts is a map from view key to the view Layout
Layouts = map[string]Layout
Expand Down Expand Up @@ -81,7 +82,7 @@ func (s *Server) Serve(outDir string, devmode bool, port int) error {
http.HandleFunc("/data/save", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, "Param id is missing", http.StatusBadRequest)
handleError(w, fmt.Errorf("missing id"))
return
}

Expand All @@ -91,9 +92,7 @@ func (s *Server) Serve(outDir string, devmode bool, port int) error {
svgFile := path.Join(outDir, id+".svg")
f, err := os.Create(svgFile)
if err != nil {
msg := fmt.Sprintf("Saving failed, can't write to %s: %s!\n", svgFile, err)
fmt.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
handleError(w, err)
return
}
defer func() { _ = f.Close() }()
Expand All @@ -102,6 +101,32 @@ func (s *Server) Serve(outDir string, devmode bool, port int) error {
w.WriteHeader(http.StatusAccepted)
})

http.HandleFunc("/data/gen", func(w http.ResponseWriter, r *http.Request) {
var design mdl.Design

s.lock.Lock()
defer s.lock.Unlock()

if err := json.NewDecoder(r.Body).Decode(&design); err != nil {
handleError(w, err)
return
}

s.SetDesign(&design)

dsl, err := codegen.Model(&design, outDir)
if err != nil {
handleError(w, err)
return
}
if err := os.WriteFile(path.Join(outDir, "model.go"), dsl, 0644); err != nil {
handleError(w, err)
return
}

w.WriteHeader(http.StatusNoContent)
})

// start the server
fmt.Printf("Editor started. Open http://localhost:%d in your browser.\n", port)
return http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), nil)
Expand All @@ -123,6 +148,12 @@ func (s *Server) SetDesign(d *mdl.Design) {
s.design = b
}

// handleError writes the given error to stderr and http.Error.
func handleError(w http.ResponseWriter, err error) {
fmt.Fprintln(os.Stderr, err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}

// loadLayouts lists out directory and reads layout info from SVG files
// for backwards compatibility, fallback to layout.json
func loadLayouts(dir string) ([]byte, error) {
Expand Down Expand Up @@ -164,7 +195,7 @@ func loadLayouts(dir string) ([]byte, error) {
end := bytes.Index(b, endMark)
b = b[begin:end]

var l Layout = make(map[string]interface{})
var l Layout = make(map[string]any)
err = json.Unmarshal(b, &l)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion cmd/mdl/webapp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions cmd/stz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func main() {
}
}
done:
fs.Parse(os.Args[idx:])
if err := fs.Parse(os.Args[idx:]); err != nil {
fail(err.Error())
}

pathOrDefault := func(p string) string {
if p == "" {
Expand Down Expand Up @@ -208,7 +210,7 @@ func put(path, wid, key, secret string, debug bool) error {
return c.Put(wid, local)
}

func fail(format string, args ...interface{}) {
func fail(format string, args ...any) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
}
Expand Down
Loading
Loading