From 6fec8cfc5422a08f2f95fee784ebfa7966d284d9 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Mon, 29 Jan 2018 15:53:38 -0500 Subject: [PATCH] moved the meta.Name type to the github.com/markbates/inflect package instead --- Dockerfile | 3 +- buffalo/cmd/generate/mailer.go | 3 +- buffalo/cmd/generate/resource.go | 4 +- buffalo/cmd/new.go | 5 +- generators/action/action.go | 16 ++-- generators/action/generator.go | 19 ++-- generators/grift/generator.go | 19 ++-- generators/mail/mail.go | 7 +- generators/newapp/generator.go | 5 +- generators/resource/generator.go | 30 +++--- generators/resource/props.go | 5 +- meta/app.go | 31 +++--- meta/name.go | 128 ++----------------------- meta/name_test.go | 159 ------------------------------- 14 files changed, 85 insertions(+), 349 deletions(-) delete mode 100644 meta/name_test.go diff --git a/Dockerfile b/Dockerfile index e69ac882e..dfc5315ec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ RUN go get -v -u github.com/golang/lint/golint RUN go get -v -u github.com/markbates/filetest RUN go get -v -u github.com/gobuffalo/makr RUN go get -v -u github.com/markbates/grift +RUN go get -v -u github.com/markbates/inflect RUN go get -v -u github.com/markbates/refresh RUN go get -v -u github.com/gobuffalo/tags @@ -17,7 +18,7 @@ RUN mkdir -p $BP WORKDIR $BP ADD . . -RUN go get -v -t $(go list ./... | grep -v /vendor/) +RUN go get -v -t ./... RUN go install -v ./buffalo diff --git a/buffalo/cmd/generate/mailer.go b/buffalo/cmd/generate/mailer.go index 889c6e813..f1b5046a4 100644 --- a/buffalo/cmd/generate/mailer.go +++ b/buffalo/cmd/generate/mailer.go @@ -4,6 +4,7 @@ import ( "github.com/gobuffalo/buffalo/generators/mail" "github.com/gobuffalo/buffalo/meta" "github.com/gobuffalo/makr" + "github.com/markbates/inflect" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -19,7 +20,7 @@ var MailCmd = &cobra.Command{ return errors.New("you must supply a name for your mailer") } mailer.App = meta.New(".") - mailer.Name = meta.Name(args[0]) + mailer.Name = inflect.Name(args[0]) data := makr.Data{} return mailer.Run(".", data) diff --git a/buffalo/cmd/generate/resource.go b/buffalo/cmd/generate/resource.go index 1e44a722b..0929ec0c0 100644 --- a/buffalo/cmd/generate/resource.go +++ b/buffalo/cmd/generate/resource.go @@ -3,10 +3,10 @@ package generate import ( "strings" + "github.com/markbates/inflect" "github.com/pkg/errors" "github.com/gobuffalo/buffalo/generators/resource" - "github.com/gobuffalo/buffalo/meta" "github.com/gobuffalo/makr" "github.com/spf13/cobra" ) @@ -35,7 +35,7 @@ var ResourceCmd = &cobra.Command{ o.SkipMigration = resourceOptions.SkipMigration if resourceOptions.ModelName != "" { o.UseModel = true - o.Model = meta.Name(resourceOptions.ModelName) + o.Model = inflect.Name(resourceOptions.ModelName) } if err := o.Validate(); err != nil { diff --git a/buffalo/cmd/new.go b/buffalo/cmd/new.go index 0a4f32d6e..22dd9e53a 100644 --- a/buffalo/cmd/new.go +++ b/buffalo/cmd/new.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strings" + "github.com/markbates/inflect" "github.com/sirupsen/logrus" "github.com/pkg/errors" @@ -39,11 +40,11 @@ var newCmd = &cobra.Command{ return errors.New("you must enter a name for your new application") } - app.Name = meta.Name(args[0]) + app.Name = inflect.Name(args[0]) app.Version = Version if app.Name == "." { - app.Name = meta.Name(filepath.Base(app.Root)) + app.Name = inflect.Name(filepath.Base(app.Root)) } else { app.Root = filepath.Join(app.Root, app.Name.File()) } diff --git a/generators/action/action.go b/generators/action/action.go index 1ad12bd83..72637234b 100644 --- a/generators/action/action.go +++ b/generators/action/action.go @@ -6,11 +6,11 @@ import ( "path/filepath" "strings" + "github.com/markbates/inflect" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/gobuffalo/buffalo/generators" - "github.com/gobuffalo/buffalo/meta" "github.com/gobuffalo/makr" ) @@ -74,7 +74,7 @@ func (act Generator) buildTestsTemplate(filePath string) string { return testsTemplate } -func (act Generator) addTemplateFiles(actionsToAdd []meta.Name, data makr.Data) error { +func (act Generator) addTemplateFiles(actionsToAdd []inflect.Name, data makr.Data) error { for _, action := range actionsToAdd { vg := makr.New() viewPath := filepath.Join("templates", fmt.Sprintf("%s", act.Name.File()), fmt.Sprintf("%s.html", action.File())) @@ -90,13 +90,13 @@ func (act Generator) addTemplateFiles(actionsToAdd []meta.Name, data makr.Data) return nil } -func (act Generator) findActionsToAdd(path string) []meta.Name { +func (act Generator) findActionsToAdd(path string) []inflect.Name { fileContents, err := ioutil.ReadFile(path) if err != nil { fileContents = []byte("") } - actionsToAdd := []meta.Name{} + actionsToAdd := []inflect.Name{} for _, action := range act.Actions { funcSignature := fmt.Sprintf("func %s%s(c buffalo.Context) error", act.Name.Camel(), action.Camel()) @@ -111,13 +111,13 @@ func (act Generator) findActionsToAdd(path string) []meta.Name { return actionsToAdd } -func (act Generator) findHandlersToAdd(path string) []meta.Name { +func (act Generator) findHandlersToAdd(path string) []inflect.Name { fileContents, err := ioutil.ReadFile(path) if err != nil { fileContents = []byte("") } - handlersToAdd := []meta.Name{} + handlersToAdd := []inflect.Name{} for _, action := range act.Actions { funcSignature := fmt.Sprintf("app.GET(\"/%s/%s\", %s%s)", act.Name.URL(), action.URL(), act.Name.Camel(), action.Camel()) @@ -132,13 +132,13 @@ func (act Generator) findHandlersToAdd(path string) []meta.Name { return handlersToAdd } -func (act Generator) findTestsToAdd(path string) []meta.Name { +func (act Generator) findTestsToAdd(path string) []inflect.Name { fileContents, err := ioutil.ReadFile(path) if err != nil { fileContents = []byte("") } - actionsToAdd := []meta.Name{} + actionsToAdd := []inflect.Name{} for _, action := range act.Actions { funcSignature := fmt.Sprintf("func (as *ActionSuite) Test_%v_%v() {", act.Name.Camel(), action.Camel()) diff --git a/generators/action/generator.go b/generators/action/generator.go index 63755bcd8..aa7f2717d 100644 --- a/generators/action/generator.go +++ b/generators/action/generator.go @@ -4,16 +4,17 @@ import ( "errors" "github.com/gobuffalo/buffalo/meta" + "github.com/markbates/inflect" ) // Generator for generating new actions type Generator struct { - App meta.App `json:"app"` - Name meta.Name `json:"name"` - Method string `json:"method"` - SkipTemplate bool `json:"skip_template"` - Actions []meta.Name `json:"actions"` - Args []string `json:"args"` + App meta.App `json:"app"` + Name inflect.Name `json:"name"` + Method string `json:"method"` + SkipTemplate bool `json:"skip_template"` + Actions []inflect.Name `json:"actions"` + Args []string `json:"args"` } // New returns a well formed set of Options @@ -21,16 +22,16 @@ type Generator struct { func New(args ...string) (Generator, error) { o := Generator{ App: meta.New("."), - Actions: []meta.Name{}, + Actions: []inflect.Name{}, Args: args, Method: "GET", } if len(args) < 2 { return o, errors.New("you need to provide at least an action name and handler name") } - o.Name = meta.Name(args[0]) + o.Name = inflect.Name(args[0]) for _, a := range args[1:] { - o.Actions = append(o.Actions, meta.Name(a)) + o.Actions = append(o.Actions, inflect.Name(a)) } return o, nil diff --git a/generators/grift/generator.go b/generators/grift/generator.go index aa3ee78ae..af1525dcd 100644 --- a/generators/grift/generator.go +++ b/generators/grift/generator.go @@ -4,20 +4,21 @@ import ( "strings" "github.com/gobuffalo/buffalo/meta" + "github.com/markbates/inflect" "github.com/pkg/errors" ) // Generator for creating a new grift task type Generator struct { - App meta.App `json:"app"` - Name meta.Name `json:"name"` - Parts []meta.Name `json:"parts"` - Args []string `json:"args"` - Namespaced bool `json:"namespaced"` + App meta.App `json:"app"` + Name inflect.Name `json:"name"` + Parts []inflect.Name `json:"parts"` + Args []string `json:"args"` + Namespaced bool `json:"namespaced"` } // Last checks if the name is the last of the parts -func (g Generator) Last(n meta.Name) bool { +func (g Generator) Last(n inflect.Name) bool { return g.Parts[len(g.Parts)-1] == n } @@ -26,15 +27,15 @@ func New(args ...string) (Generator, error) { g := Generator{ App: meta.New("."), Args: args, - Parts: []meta.Name{}, + Parts: []inflect.Name{}, } if len(args) > 0 { g.Namespaced = strings.Contains(args[0], ":") for _, n := range strings.Split(args[0], ":") { - g.Parts = append(g.Parts, meta.Name(n)) + g.Parts = append(g.Parts, inflect.Name(n)) } - g.Name = meta.Name(g.Parts[len(g.Parts)-1]) + g.Name = inflect.Name(g.Parts[len(g.Parts)-1]) } return g, g.Validate() diff --git a/generators/mail/mail.go b/generators/mail/mail.go index 061a503c9..8aff45ad0 100644 --- a/generators/mail/mail.go +++ b/generators/mail/mail.go @@ -8,14 +8,15 @@ import ( "github.com/gobuffalo/buffalo/meta" "github.com/gobuffalo/makr" "github.com/gobuffalo/packr" + "github.com/markbates/inflect" "github.com/pkg/errors" ) // Generator for creating new mailers type Generator struct { - App meta.App `json:"app"` - Name meta.Name `json:"name"` - SkipInit bool `json:"skip_init"` + App meta.App `json:"app"` + Name inflect.Name `json:"name"` + SkipInit bool `json:"skip_init"` } // Run the new mailer generator. It will init the mailers directory diff --git a/generators/newapp/generator.go b/generators/newapp/generator.go index 45eff1beb..bd2c56123 100644 --- a/generators/newapp/generator.go +++ b/generators/newapp/generator.go @@ -9,6 +9,7 @@ import ( "github.com/gobuffalo/buffalo/meta" "github.com/gobuffalo/envy" + "github.com/markbates/inflect" "github.com/pkg/errors" ) @@ -42,10 +43,10 @@ func New(name string) (Generator, error) { AsWeb: true, Docker: "multi", } - g.Name = meta.Name(name) + g.Name = inflect.Name(name) if g.Name == "." { - g.Name = meta.Name(filepath.Base(g.Root)) + g.Name = inflect.Name(filepath.Base(g.Root)) } else { g.Root = filepath.Join(g.Root, g.Name.File()) } diff --git a/generators/resource/generator.go b/generators/resource/generator.go index 4d038cfd5..7f496e141 100644 --- a/generators/resource/generator.go +++ b/generators/resource/generator.go @@ -11,17 +11,17 @@ import ( // Generator for generating a new resource type Generator struct { - App meta.App `json:"app"` - Name meta.Name `json:"name"` - Model meta.Name `json:"model"` - SkipMigration bool `json:"skip_migration"` - SkipModel bool `json:"skip_model"` - UseModel bool `json:"use_model"` - MimeType string `json:"mime_type"` - FilesPath string `json:"files_path"` - ActionsPath string `json:"actions_path"` - Props []Prop `json:"props"` - Args []string `json:"args"` + App meta.App `json:"app"` + Name inflect.Name `json:"name"` + Model inflect.Name `json:"model"` + SkipMigration bool `json:"skip_migration"` + SkipModel bool `json:"skip_model"` + UseModel bool `json:"use_model"` + MimeType string `json:"mime_type"` + FilesPath string `json:"files_path"` + ActionsPath string `json:"actions_path"` + Props []Prop `json:"props"` + Args []string `json:"args"` } // New constructs new options for generating a resource @@ -34,8 +34,8 @@ func New(modelName string, args ...string) (Generator, error) { o.App = meta.New(pwd) if len(o.Args) > 0 { - o.Name = meta.Name(o.Args[0]) - o.Model = meta.Name(o.Args[0]) + o.Name = inflect.Name(o.Args[0]) + o.Model = inflect.Name(o.Args[0]) } o.Props = modelPropertiesFromArgs(o.Args) @@ -43,11 +43,11 @@ func New(modelName string, args ...string) (Generator, error) { o.ActionsPath = o.FilesPath if strings.Contains(string(o.Name), "/") { parts := strings.Split(string(o.Name), "/") - o.Model = meta.Name(parts[len(parts)-1]) + o.Model = inflect.Name(parts[len(parts)-1]) o.ActionsPath = inflect.Underscore(o.Name.Resource()) } if modelName != "" { - o.Model = meta.Name(modelName) + o.Model = inflect.Name(modelName) } return o, o.Validate() } diff --git a/generators/resource/props.go b/generators/resource/props.go index a2c95d65f..6aa861f4e 100644 --- a/generators/resource/props.go +++ b/generators/resource/props.go @@ -3,13 +3,12 @@ package resource import ( "strings" - "github.com/gobuffalo/buffalo/meta" "github.com/markbates/inflect" ) // Prop of a model. Starts as name:type on the command line. type Prop struct { - Name meta.Name + Name inflect.Name Type string } @@ -26,7 +25,7 @@ func modelPropertiesFromArgs(args []string) []Prop { for _, a := range args[1:] { ax := strings.Split(a, ":") p := Prop{ - Name: meta.Name(inflect.ForeignKeyToAttribute(ax[0])), + Name: inflect.Name(inflect.ForeignKeyToAttribute(ax[0])), Type: "string", } if len(ax) > 1 { diff --git a/meta/app.go b/meta/app.go index 291bd5282..967f423a6 100644 --- a/meta/app.go +++ b/meta/app.go @@ -8,25 +8,26 @@ import ( "runtime" "github.com/gobuffalo/envy" + "github.com/markbates/inflect" ) // App represents meta data for a Buffalo application on disk type App struct { - Pwd string `json:"pwd"` - Root string `json:"root"` - GoPath string `json:"go_path"` - Name Name `json:"name"` - Bin string `json:"bin"` - PackagePkg string `json:"package_path"` - ActionsPkg string `json:"actions_path"` - ModelsPkg string `json:"models_path"` - GriftsPkg string `json:"grifts_path"` - WithPop bool `json:"with_pop"` - WithDep bool `json:"with_dep"` - WithWebpack bool `json:"with_webpack"` - WithYarn bool `json:"with_yarn"` - WithDocker bool `json:"with_docker"` - WithGrifts bool `json:"with_grifts"` + Pwd string `json:"pwd"` + Root string `json:"root"` + GoPath string `json:"go_path"` + Name inflect.Name `json:"name"` + Bin string `json:"bin"` + PackagePkg string `json:"package_path"` + ActionsPkg string `json:"actions_path"` + ModelsPkg string `json:"models_path"` + GriftsPkg string `json:"grifts_path"` + WithPop bool `json:"with_pop"` + WithDep bool `json:"with_dep"` + WithWebpack bool `json:"with_webpack"` + WithYarn bool `json:"with_yarn"` + WithDocker bool `json:"with_docker"` + WithGrifts bool `json:"with_grifts"` } // New App based on the details found at the provided root path diff --git a/meta/name.go b/meta/name.go index c8f3f624f..fc678b085 100644 --- a/meta/name.go +++ b/meta/name.go @@ -2,129 +2,17 @@ package meta import ( "fmt" - "strings" + "runtime" "github.com/markbates/inflect" ) -// Name is a string that represents the "name" of a thing, like an app, model, etc... -type Name string - -// Title version of a name. ie. "foo_bar" => "Foo Bar" -func (n Name) Title() string { - x := strings.Split(string(n), "/") - for i, s := range x { - x[i] = inflect.Titleize(s) - } - - return strings.Join(x, " ") -} - -// Underscore version of a name. ie. "FooBar" => "foo_bar" -func (n Name) Underscore() string { - return inflect.Underscore(string(n)) -} - -// Plural version of a name -func (n Name) Plural() string { - return inflect.Pluralize(string(n)) -} - -// Singular version of a name -func (n Name) Singular() string { - return inflect.Singularize(string(n)) -} - -// Camel version of a name -func (n Name) Camel() string { - return inflect.Camelize(string(n)) -} - -// Model version of a name. ie. "user" => "User" -func (n Name) Model() string { - x := strings.Split(string(n), "/") - for i, s := range x { - x[i] = inflect.Camelize(inflect.Singularize(s)) +// Name is deprecated, please use github.com/markbates/inflect.Name instead. +func Name(s string) inflect.Name { + warningMsg := "Name is deprecated, and will be removed in v0.12.0. Use github.com/markbates/inflect.Name instead." + _, file, no, ok := runtime.Caller(1) + if ok { + warningMsg = fmt.Sprintf("%s Called from %s:%d", warningMsg, file, no) } - - return strings.Join(x, "") -} - -// Resource version of a name -func (n Name) Resource() string { - name := inflect.Underscore(string(n)) - x := strings.FieldsFunc(name, func(r rune) bool { - return r == '_' || r == '/' - }) - - for i, w := range x { - if i == len(x)-1 { - x[i] = inflect.Camelize(inflect.Pluralize(strings.ToLower(w))) - continue - } - - x[i] = inflect.Camelize(w) - } - - return strings.Join(x, "") -} - -// ModelPlural version of a name. ie. "user" => "Users" -func (n Name) ModelPlural() string { - return inflect.Pluralize(n.Model()) -} - -// File version of a name -func (n Name) File() string { - return inflect.Underscore(inflect.Camelize(string(n))) -} - -// Table version of a name -func (n Name) Table() string { - return inflect.Underscore(inflect.Pluralize(string(n))) -} - -// UnderSingular version of a name -func (n Name) UnderSingular() string { - return inflect.Underscore(inflect.Singularize(string(n))) -} - -// PluralCamel version of a name -func (n Name) PluralCamel() string { - return inflect.Pluralize(inflect.Camelize(string(n))) -} - -// PluralUnder version of a name -func (n Name) PluralUnder() string { - return inflect.Pluralize(inflect.Underscore(string(n))) -} - -// URL version of a name -func (n Name) URL() string { - return n.PluralUnder() -} - -// CamelSingular version of a name -func (n Name) CamelSingular() string { - return inflect.Camelize(inflect.Singularize(string(n))) -} - -// VarCaseSingular version of a name. ie. "FooBar" => "fooBar" -func (n Name) VarCaseSingular() string { - return inflect.CamelizeDownFirst(inflect.Singularize(inflect.Underscore(n.Resource()))) -} - -// VarCasePlural version of a name. ie. "FooBar" => "fooBar" -func (n Name) VarCasePlural() string { - return inflect.CamelizeDownFirst(n.Resource()) -} - -// Lower case version of a string -func (n Name) Lower() string { - return strings.ToLower(string(n)) -} - -// ParamID returns foo_bar_id -func (n Name) ParamID() string { - return fmt.Sprintf("%s_id", strings.Replace(n.UnderSingular(), "/", "_", -1)) + return inflect.Name(s) } diff --git a/meta/name_test.go b/meta/name_test.go deleted file mode 100644 index 0873f1685..000000000 --- a/meta/name_test.go +++ /dev/null @@ -1,159 +0,0 @@ -package meta - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func Test_Name_ParamID(t *testing.T) { - r := require.New(t) - table := []struct { - V string - E string - }{ - {V: "foo_bar", E: "foo_bar_id"}, - {V: "admin/widget", E: "admin_widget_id"}, - {V: "widget", E: "widget_id"}, - {V: "User", E: "user_id"}, - } - for _, tt := range table { - r.Equal(tt.E, Name(tt.V).ParamID()) - } -} - -func Test_Name_Title(t *testing.T) { - r := require.New(t) - table := []struct { - V string - E string - }{ - {V: "foo_bar", E: "Foo Bar"}, - {V: "admin/widget", E: "Admin Widget"}, - {V: "widget", E: "Widget"}, - } - for _, tt := range table { - r.Equal(tt.E, Name(tt.V).Title()) - } -} - -func Test_Name_Model(t *testing.T) { - r := require.New(t) - table := []struct { - V string - E string - }{ - {V: "foo_bar", E: "FooBar"}, - {V: "admin/widget", E: "AdminWidget"}, - {V: "widget", E: "Widget"}, - {V: "widgets", E: "Widget"}, - {V: "status", E: "Status"}, - {V: "Statuses", E: "Status"}, - {V: "statuses", E: "Status"}, - } - for _, tt := range table { - r.Equal(tt.E, Name(tt.V).Model()) - } -} - -func Test_Name_Resource(t *testing.T) { - r := require.New(t) - table := []struct { - V string - E string - }{ - {V: "Person", E: "People"}, - {V: "foo_bar", E: "FooBars"}, - {V: "admin/widget", E: "AdminWidgets"}, - {V: "widget", E: "Widgets"}, - {V: "widgets", E: "Widgets"}, - {V: "greatPerson", E: "GreatPeople"}, - {V: "great/person", E: "GreatPeople"}, - {V: "status", E: "Statuses"}, - {V: "Status", E: "Statuses"}, - {V: "Statuses", E: "Statuses"}, - {V: "statuses", E: "Statuses"}, - } - for _, tt := range table { - r.Equal(tt.E, Name(tt.V).Resource()) - } -} - -func Test_Name_ModelPlural(t *testing.T) { - r := require.New(t) - table := []struct { - V string - E string - }{ - {V: "foo_bar", E: "FooBars"}, - {V: "admin/widget", E: "AdminWidgets"}, - {V: "widget", E: "Widgets"}, - {V: "widgets", E: "Widgets"}, - {V: "status", E: "Statuses"}, - {V: "statuses", E: "Statuses"}, - } - for _, tt := range table { - r.Equal(tt.E, Name(tt.V).ModelPlural()) - } -} - -func Test_Name_File(t *testing.T) { - r := require.New(t) - table := []struct { - V string - E string - }{ - {V: "foo_bar", E: "foo_bar"}, - {V: "admin/widget", E: "admin/widget"}, - {V: "widget", E: "widget"}, - {V: "widgets", E: "widgets"}, - {V: "User", E: "user"}, - } - for _, tt := range table { - r.Equal(tt.E, Name(tt.V).File()) - } -} - -func Test_Name_VarCaseSingular(t *testing.T) { - r := require.New(t) - table := []struct { - V string - E string - }{ - {V: "foo_bar", E: "fooBar"}, - {V: "admin/widget", E: "adminWidget"}, - {V: "widget", E: "widget"}, - {V: "widgets", E: "widget"}, - {V: "User", E: "user"}, - {V: "FooBar", E: "fooBar"}, - {V: "status", E: "status"}, - {V: "statuses", E: "status"}, - {V: "Status", E: "status"}, - {V: "Statuses", E: "status"}, - } - for _, tt := range table { - r.Equal(tt.E, Name(tt.V).VarCaseSingular()) - } -} - -func Test_Name_VarCasePlural(t *testing.T) { - r := require.New(t) - table := []struct { - V string - E string - }{ - {V: "foo_bar", E: "fooBars"}, - {V: "admin/widget", E: "adminWidgets"}, - {V: "widget", E: "widgets"}, - {V: "widgets", E: "widgets"}, - {V: "User", E: "users"}, - {V: "FooBar", E: "fooBars"}, - {V: "status", E: "statuses"}, - {V: "statuses", E: "statuses"}, - {V: "Status", E: "statuses"}, - {V: "Statuses", E: "statuses"}, - } - for _, tt := range table { - r.Equal(tt.E, Name(tt.V).VarCasePlural()) - } -}