diff --git a/.gitignore b/.gitignore deleted file mode 100644 index daf913b..0000000 --- a/.gitignore +++ /dev/null @@ -1,24 +0,0 @@ -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test -*.prof diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 72825b4..0000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: go - -go: - - 1.7 - - 1.8 - - master -script: - # The GOPATH is for testing #21 - - GOPATH="$GOPATH:/tmp/jsonenums-test/go1:/tmp/jsonenums-test/go2" go test ./... diff --git a/example/shirtsize.go b/example/shirtsize.go deleted file mode 100644 index 395453c..0000000 --- a/example/shirtsize.go +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright 2017 Google Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to writing, software distributed -// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. -// -// See the License for the specific language governing permissions and -// limitations under the License. - -package main - -import ( - "encoding/json" - "fmt" - "log" - "os" - "strings" -) - -//go:generate jsonenums -type=ShirtSize - -type ShirtSize byte - -const ( - NA ShirtSize = iota - XS - S - M - L - XL -) - -//go:generate jsonenums -type=WeekDay - -type WeekDay int - -const ( - Monday WeekDay = iota - Tuesday - Wednesday - Thursday - Friday - Saturday - Sunday -) - -func (d WeekDay) String() string { - switch d { - case Monday: - return "Dilluns" - case Tuesday: - return "Dimarts" - case Wednesday: - return "Dimecres" - case Thursday: - return "Dijous" - case Friday: - return "Divendres" - case Saturday: - return "Dissabte" - case Sunday: - return "Diumenge" - default: - return "invalid WeekDay" - } -} - -func main() { - v := struct { - Size ShirtSize - Day WeekDay - }{M, Friday} - if err := json.NewEncoder(os.Stdout).Encode(v); err != nil { - log.Fatal(err) - } - - input := `{"Size":"XL", "Day":"Dimarts"}` - if err := json.NewDecoder(strings.NewReader(input)).Decode(&v); err != nil { - log.Fatal(err) - } - fmt.Printf("decoded %s as %+v\n", input, v) -} diff --git a/example/shirtsize_jsonenums.go b/example/shirtsize_jsonenums.go deleted file mode 100644 index d50c71a..0000000 --- a/example/shirtsize_jsonenums.go +++ /dev/null @@ -1,68 +0,0 @@ -// generated by jsonenums -type=ShirtSize; DO NOT EDIT - -package main - -import ( - "encoding/json" - "fmt" -) - -var ( - _ShirtSizeNameToValue = map[string]ShirtSize{ - "NA": NA, - "XS": XS, - "S": S, - "M": M, - "L": L, - "XL": XL, - } - - _ShirtSizeValueToName = map[ShirtSize]string{ - NA: "NA", - XS: "XS", - S: "S", - M: "M", - L: "L", - XL: "XL", - } -) - -func init() { - var v ShirtSize - if _, ok := interface{}(v).(fmt.Stringer); ok { - _ShirtSizeNameToValue = map[string]ShirtSize{ - interface{}(NA).(fmt.Stringer).String(): NA, - interface{}(XS).(fmt.Stringer).String(): XS, - interface{}(S).(fmt.Stringer).String(): S, - interface{}(M).(fmt.Stringer).String(): M, - interface{}(L).(fmt.Stringer).String(): L, - interface{}(XL).(fmt.Stringer).String(): XL, - } - } -} - -// MarshalJSON is generated so ShirtSize satisfies json.Marshaler. -func (r ShirtSize) MarshalJSON() ([]byte, error) { - if s, ok := interface{}(r).(fmt.Stringer); ok { - return json.Marshal(s.String()) - } - s, ok := _ShirtSizeValueToName[r] - if !ok { - return nil, fmt.Errorf("invalid ShirtSize: %d", r) - } - return json.Marshal(s) -} - -// UnmarshalJSON is generated so ShirtSize satisfies json.Unmarshaler. -func (r *ShirtSize) UnmarshalJSON(data []byte) error { - var s string - if err := json.Unmarshal(data, &s); err != nil { - return fmt.Errorf("ShirtSize should be a string, got %s", data) - } - v, ok := _ShirtSizeNameToValue[s] - if !ok { - return fmt.Errorf("invalid ShirtSize %q", s) - } - *r = v - return nil -} diff --git a/example/weekday_jsonenums.go b/example/weekday_jsonenums.go deleted file mode 100644 index a97dd11..0000000 --- a/example/weekday_jsonenums.go +++ /dev/null @@ -1,71 +0,0 @@ -// generated by jsonenums -type=WeekDay; DO NOT EDIT - -package main - -import ( - "encoding/json" - "fmt" -) - -var ( - _WeekDayNameToValue = map[string]WeekDay{ - "Monday": Monday, - "Tuesday": Tuesday, - "Wednesday": Wednesday, - "Thursday": Thursday, - "Friday": Friday, - "Saturday": Saturday, - "Sunday": Sunday, - } - - _WeekDayValueToName = map[WeekDay]string{ - Monday: "Monday", - Tuesday: "Tuesday", - Wednesday: "Wednesday", - Thursday: "Thursday", - Friday: "Friday", - Saturday: "Saturday", - Sunday: "Sunday", - } -) - -func init() { - var v WeekDay - if _, ok := interface{}(v).(fmt.Stringer); ok { - _WeekDayNameToValue = map[string]WeekDay{ - interface{}(Monday).(fmt.Stringer).String(): Monday, - interface{}(Tuesday).(fmt.Stringer).String(): Tuesday, - interface{}(Wednesday).(fmt.Stringer).String(): Wednesday, - interface{}(Thursday).(fmt.Stringer).String(): Thursday, - interface{}(Friday).(fmt.Stringer).String(): Friday, - interface{}(Saturday).(fmt.Stringer).String(): Saturday, - interface{}(Sunday).(fmt.Stringer).String(): Sunday, - } - } -} - -// MarshalJSON is generated so WeekDay satisfies json.Marshaler. -func (r WeekDay) MarshalJSON() ([]byte, error) { - if s, ok := interface{}(r).(fmt.Stringer); ok { - return json.Marshal(s.String()) - } - s, ok := _WeekDayValueToName[r] - if !ok { - return nil, fmt.Errorf("invalid WeekDay: %d", r) - } - return json.Marshal(s) -} - -// UnmarshalJSON is generated so WeekDay satisfies json.Unmarshaler. -func (r *WeekDay) UnmarshalJSON(data []byte) error { - var s string - if err := json.Unmarshal(data, &s); err != nil { - return fmt.Errorf("WeekDay should be a string, got %s", data) - } - v, ok := _WeekDayNameToValue[s] - if !ok { - return fmt.Errorf("invalid WeekDay %q", s) - } - *r = v - return nil -} diff --git a/server/app.yaml b/server/app.yaml deleted file mode 100644 index 69dda1e..0000000 --- a/server/app.yaml +++ /dev/null @@ -1,21 +0,0 @@ -# Unless required by applicable law or agreed to writing, software distributed -# under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR -# CONDITIONS OF ANY KIND, either express or implied. - -# See the License for the specific language governing permissions and -# limitations under the License. - -version: gae -runtime: go -api_version: go1 - -handlers: -- url: /generate - script: _go_app - -- url: / - static_files: static/home.html - upload: static/home.html - -- url: / - static_dir: static diff --git a/server/main.go b/server/main.go deleted file mode 100644 index 20223cc..0000000 --- a/server/main.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2017 Google Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to writing, software distributed -// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. -// -// See the License for the specific language governing permissions and -// limitations under the License. - -// +build !appengine - -package main - -import ( - "flag" - "net/http" -) - -func main() { - port := flag.String("http", "127.0.0.1:8080", "ip and port to listen to") - flag.Parse() - http.HandleFunc("/", homeHandler) - http.ListenAndServe(*port, nil) -} - -func homeHandler(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "static/home.html") -} diff --git a/server/server.go b/server/server.go deleted file mode 100644 index 58ea4a0..0000000 --- a/server/server.go +++ /dev/null @@ -1,124 +0,0 @@ -// Copyright 2017 Google Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to writing, software distributed -// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. -// -// See the License for the specific language governing permissions and -// limitations under the License. - -// Server is an http server that provides an alternative way of generating code -// based on int types and the constants defined with it. -// -// Use the http flag to change the address on which the server will listen for -// requests. The default is 127.0.0.1:8080. -package main - -import ( - "bytes" - "fmt" - "io/ioutil" - "log" - "net/http" - "os" - "path/filepath" - "text/template" - - "go/format" - - "github.com/campoy/jsonenums/parser" -) - -func init() { - http.Handle("/generate", handler(generateHandler)) -} - -func generateHandler(w http.ResponseWriter, r *http.Request) error { - if r.Method != "GET" { - return codeError{fmt.Errorf("only GET accepted"), http.StatusMethodNotAllowed} - } - code := r.FormValue("code") - if code == "" { - return codeError{fmt.Errorf("no code to be parsed"), http.StatusBadRequest} - } - typ := r.FormValue("type") - if typ == "" { - return codeError{fmt.Errorf("no type to be analyzed"), http.StatusBadRequest} - } - - dir, err := createDir(code) - if err != nil { - return err - } - defer os.RemoveAll(dir) - - pkg, err := parser.ParsePackage(dir) - if err != nil { - return fmt.Errorf("parse package: %v", err) - } - - values, err := pkg.ValuesOfType(typ) - if err != nil { - return fmt.Errorf("find values: %v", err) - } - - t, err := template.New("code").Parse(r.FormValue("template")) - if err != nil { - return codeError{fmt.Errorf("parse template: %v", err), http.StatusBadRequest} - } - var data = struct { - PackageName string - TypeName string - Values []string - }{pkg.Name, typ, values} - - var buf bytes.Buffer - if err := t.Execute(&buf, data); err != nil { - return codeError{fmt.Errorf("execute template: %v", err), http.StatusBadRequest} - } - src, err := format.Source(buf.Bytes()) - if err != nil { - return codeError{fmt.Errorf("code generated is not valid: %v\n%v", err, buf.String()), http.StatusBadRequest} - } - w.Write(src) - return nil -} - -func createDir(content string) (string, error) { - dir, err := ioutil.TempDir("", "jsonenums") - if err != nil { - return "", fmt.Errorf("create tmp dir: %v", err) - } - f, err := os.Create(filepath.Join(dir, "main.go")) - if err != nil { - os.RemoveAll(dir) - return "", fmt.Errorf("create tmp file: %v", err) - } - f.WriteString(content) - f.Close() - return dir, err -} - -type handler func(http.ResponseWriter, *http.Request) error - -func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - err := h(w, r) - if err != nil { - code := http.StatusInternalServerError - if cErr, ok := err.(codeError); ok { - code = cErr.code - } else { - log.Printf("%v: %v", r.URL, code) - } - http.Error(w, err.Error(), code) - } -} - -type codeError struct { - error - code int -} diff --git a/server/static/home.html b/server/static/home.html deleted file mode 100644 index 84ea01d..0000000 --- a/server/static/home.html +++ /dev/null @@ -1,88 +0,0 @@ - - - - - -jsonenums - - - - - - - -
- - - -
- - - - - - -