本文整理匯總了Golang中bitbucket/org/pkg/inflect.Singularize函數的典型用法代碼示例。如果您正苦於以下問題:Golang Singularize函數的具體用法?Golang Singularize怎麽用?Golang Singularize使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Singularize函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: BelongsTo
// BelongsTo signifies a relationship between this model and a
// Parent. The Parent has the child, and the Child belongs
// to the Parent.
// Usage: BelongsTo("User")
func BelongsTo(parent string) {
if r, ok := relationalModelDefinition(false); ok {
idfield := &gorma.RelationalFieldDefinition{
Name: codegen.Goify(inflect.Singularize(parent), true) + "ID",
Description: "Belongs To " + codegen.Goify(inflect.Singularize(parent), true),
Parent: r,
Datatype: gorma.BelongsTo,
DatabaseFieldName: SanitizeDBFieldName(codegen.Goify(inflect.Singularize(parent), true) + "ID"),
}
r.RelationalFields[idfield.Name] = idfield
bt, ok := r.Parent.RelationalModels[codegen.Goify(inflect.Singularize(parent), true)]
if ok {
r.BelongsTo[bt.Name] = bt
} else {
models := &gorma.RelationalModelDefinition{
Name: codegen.Goify(inflect.Singularize(parent), true),
Parent: r.Parent,
RelationalFields: make(map[string]*gorma.RelationalFieldDefinition),
BelongsTo: make(map[string]*gorma.RelationalModelDefinition),
HasMany: make(map[string]*gorma.RelationalModelDefinition),
HasOne: make(map[string]*gorma.RelationalModelDefinition),
ManyToMany: make(map[string]*gorma.ManyToManyDefinition),
}
r.BelongsTo[models.Name] = models
}
}
}
示例2: pluralize
func pluralize(c int, s string, format bool) string {
if c == 1 {
if format {
return fmt.Sprintf("1 %s", inflect.Singularize(s))
}
return fmt.Sprintf("%s", inflect.Singularize(s))
}
if format {
return fmt.Sprintf("%d %s", c, inflect.Pluralize(s))
}
return fmt.Sprintf("%s", inflect.Pluralize(s))
}
示例3: HasMany
// HasMany signifies a relationship between this model and a
// set of Children. The Parent has the children, and the Children belong
// to the Parent. The first parameter becomes the name of the
// field in the model struct, the second parameter is the name
// of the child model. The Child model will have a ParentID field
// appended to the field list. The Parent model definition will use
// the first parameter as the field name in the struct definition.
// Usage: HasMany("Orders", "Order")
// Struct field definition: Children []Child
func HasMany(name, child string) {
if r, ok := relationalModelDefinition(false); ok {
field := &gorma.RelationalFieldDefinition{
Name: codegen.Goify(name, true),
HasMany: child,
Description: "has many " + inflect.Pluralize(child),
Datatype: gorma.HasMany,
Parent: r,
}
r.RelationalFields[field.Name] = field
var model *gorma.RelationalModelDefinition
model, ok := r.Parent.RelationalModels[child]
if ok {
r.HasMany[child] = model
// create the fk field
f := &gorma.RelationalFieldDefinition{
Name: codegen.Goify(inflect.Singularize(r.Name), true) + "ID",
HasMany: child,
Description: "has many " + child,
Datatype: gorma.HasManyKey,
Parent: model,
DatabaseFieldName: SanitizeDBFieldName(codegen.Goify(inflect.Singularize(r.Name), true) + "ID"),
}
model.RelationalFields[f.Name] = f
} else {
model = &gorma.RelationalModelDefinition{
Name: child,
Parent: r.Parent,
RelationalFields: make(map[string]*gorma.RelationalFieldDefinition),
BelongsTo: make(map[string]*gorma.RelationalModelDefinition),
HasMany: make(map[string]*gorma.RelationalModelDefinition),
HasOne: make(map[string]*gorma.RelationalModelDefinition),
ManyToMany: make(map[string]*gorma.ManyToManyDefinition),
}
r.HasMany[child] = model
// create the fk field
f := &gorma.RelationalFieldDefinition{
Name: codegen.Goify(inflect.Singularize(r.Name), true) + "ID",
HasMany: child,
Description: "has many " + child,
Datatype: gorma.HasManyKey,
Parent: model,
DatabaseFieldName: SanitizeDBFieldName(codegen.Goify(inflect.Singularize(r.Name), true) + "ID"),
}
model.RelationalFields[f.Name] = f
}
}
}
示例4: createDescriptor
func createDescriptor(iface interface{}, router *ResourceRouter) descriptor {
pv := reflect.ValueOf(iface)
v := reflect.Indirect(pv)
t := v.Type()
var varName string
if nameGetter, hasVarName := iface.(interface {
VarName() string
}); hasVarName {
varName = nameGetter.VarName()
} else {
varName = inflect.Singularize(t.Name())
}
var routeName string
if nameGetter, hasRouteName := iface.(interface {
RouteName() string
}); hasRouteName {
routeName = nameGetter.RouteName()
} else {
routeName = strings.ToLower(t.Name())
}
return descriptor{
obj: iface,
router: router,
t: t,
varName: varName,
routeName: routeName,
}
}
示例5: singularize
// singularize returns the singular form of a single word.
func singularize(in interface{}) (string, error) {
word, err := cast.ToStringE(in)
if err != nil {
return "", err
}
return inflect.Singularize(word), nil
}
示例6: parseReturn
func parseReturn(kind, resName, contentType string) string {
switch kind {
case "show":
return refType(resName)
case "index":
return fmt.Sprintf("[]%s", refType(resName))
case "create":
if _, ok := noMediaTypeResources[resName]; ok {
return "map[string]interface{}"
}
return "*" + inflect.Singularize(resName) + "Locator"
case "update", "destroy":
return ""
case "current_instances":
return "[]*Instance"
default:
switch {
case len(contentType) == 0:
return ""
case strings.Index(contentType, "application/vnd.rightscale.") == 0:
if contentType == "application/vnd.rightscale.text" {
return "string"
}
elems := strings.SplitN(contentType[27:], ";", 2)
name := refType(inflect.Camelize(elems[0]))
if len(elems) > 1 && elems[1] == "type=collection" {
name = "[]" + refType(inflect.Camelize(elems[0]))
}
return name
default: // Shouldn't be here
panic("api15gen: Unknown content type " + contentType)
}
}
}
示例7: HasOne
// HasOne signifies a relationship between this model and another model.
// If this model HasOne(OtherModel), then OtherModel is expected
// to have a ThisModelID field as a Foreign Key to this model's
// Primary Key. ThisModel will have a field named OtherModel of type
// OtherModel.
// Usage: HasOne("Proposal")
func HasOne(child string) {
if r, ok := relationalModelDefinition(false); ok {
field := &gorma.RelationalFieldDefinition{
Name: codegen.Goify(inflect.Singularize(child), true),
HasOne: child,
Description: "has one " + child,
Datatype: gorma.HasOne,
Parent: r,
}
r.RelationalFields[field.Name] = field
bt, ok := r.Parent.RelationalModels[child]
if ok {
r.HasOne[child] = bt
// create the fk field
f := &gorma.RelationalFieldDefinition{
Name: codegen.Goify(inflect.Singularize(r.Name), true) + "ID",
HasOne: child,
Description: "has one " + child,
Datatype: gorma.HasOneKey,
Parent: bt,
DatabaseFieldName: SanitizeDBFieldName(codegen.Goify(inflect.Singularize(r.Name), true) + "ID"),
}
bt.RelationalFields[f.Name] = f
} else {
models := &gorma.RelationalModelDefinition{
Name: child,
Parent: r.Parent,
RelationalFields: make(map[string]*gorma.RelationalFieldDefinition),
BelongsTo: make(map[string]*gorma.RelationalModelDefinition),
HasMany: make(map[string]*gorma.RelationalModelDefinition),
HasOne: make(map[string]*gorma.RelationalModelDefinition),
ManyToMany: make(map[string]*gorma.ManyToManyDefinition),
}
r.HasOne[child] = models
// create the fk field
f := &gorma.RelationalFieldDefinition{
Name: codegen.Goify(inflect.Singularize(r.Name), true) + "ID",
HasOne: child,
Description: "has one " + child,
Datatype: gorma.HasOneKey,
Parent: bt,
DatabaseFieldName: SanitizeDBFieldName(codegen.Goify(inflect.Singularize(r.Name), true) + "ID"),
}
models.RelationalFields[f.Name] = f
}
}
}
示例8: resourceType
// Name of go type for resource with given name
// It should always be the same (camelized) but there are some resources that don't have a media
// type so for these we use a map.
func resourceType(resName string) string {
if resName == "ChildAccounts" {
return "Account"
}
if _, ok := noMediaTypeResources[resName]; ok {
return "map[string]interface{}"
}
return inflect.Singularize(resName)
}
示例9: serviceResource
func serviceResource(svc *Service, resource string, w http.ResponseWriter) (*Resource, error) {
res, ok := svc.Resources[inflect.Singularize(resource)]
if !ok {
return nil, httpError(w, 404,
"Sorry, %s does not have resource %s, available resources: %+v",
svc.Name, resource, svc.ResourceNames())
}
return res, nil
}
示例10: serviceResource
func serviceResource(svc *Service, resource string, w http.ResponseWriter) (*Resource, error) {
res, ok := svc.Resources[inflect.Singularize(resource)]
if !ok {
w.WriteHeader(404)
fmt.Fprintf(w, "Sorry, %s does not have resource %s, available resources: %+v",
svc.Name, resource, svc.ResourceNames())
return nil, fmt.Errorf("%s does not have resource %s", svc.Name, resource)
}
return res, nil
}
示例11: buildParentPath
// build the path that precedes the controller name
func buildParentPath(parentControllers []string) (string, error) {
path := "/"
for i := len(parentControllers) - 1; i >= 0; i-- {
c := parentControllers[i]
name, err := getResourceName(c)
if err != nil {
return "", err
}
path += strings.ToLower(name) + "/:" + inflect.Singularize(name) + "Id/"
}
return path, nil
}
示例12: AnalyzeResource
// Create API descriptor from raw resources and types
func (a *ApiAnalyzer) AnalyzeResource(name string, res map[string]interface{}, desc *gen.ApiDescriptor) error {
name = inflect.Singularize(name)
resource := gen.Resource{Name: name, ClientName: a.ClientName}
// Description
if d, ok := res["description"]; ok {
resource.Description = removeBlankLines(d.(string))
}
// Attributes
hasHref := false
attributes := []*gen.Attribute{}
m, ok := res["media_type"].(string)
if ok {
t, ok := a.RawTypes[m]
if ok {
attrs, ok := t["attributes"].(map[string]interface{})
if ok {
attributes = make([]*gen.Attribute, len(attrs))
for idx, n := range sortedKeys(attrs) {
if n == "href" {
hasHref = true
}
param, err := a.AnalyzeAttribute(n, n, attrs[n].(map[string]interface{}))
if err != nil {
return err
}
attributes[idx] = &gen.Attribute{n, inflect.Camelize(n), param.Signature()}
}
}
}
}
resource.Attributes = attributes
if hasHref {
resource.LocatorFunc = locatorFunc(name)
}
// Actions
actions, err := a.AnalyzeActions(name, res)
if err != nil {
return err
}
resource.Actions = actions
// Name and done
resName := toGoTypeName(name, false)
desc.Resources[resName] = &resource
desc.ResourceNames = append(desc.ResourceNames, resName)
return nil
}
示例13: init
func init() {
funcMap = template.FuncMap{
"urlize": helpers.URLize,
"sanitizeURL": helpers.SanitizeURL,
"sanitizeurl": helpers.SanitizeURL,
"eq": Eq,
"ne": Ne,
"gt": Gt,
"ge": Ge,
"lt": Lt,
"le": Le,
"dict": Dictionary,
"in": In,
"slicestr": Slicestr,
"substr": Substr,
"split": Split,
"intersect": Intersect,
"isSet": IsSet,
"isset": IsSet,
"echoParam": ReturnWhenSet,
"safeHTML": SafeHTML,
"safeCSS": SafeCSS,
"safeURL": SafeURL,
"absURL": func(a string) template.HTML { return template.HTML(helpers.AbsURL(a)) },
"relURL": func(a string) template.HTML { return template.HTML(helpers.RelURL(a)) },
"markdownify": Markdownify,
"first": First,
"last": Last,
"after": After,
"where": Where,
"delimit": Delimit,
"sort": Sort,
"highlight": Highlight,
"add": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '+') },
"sub": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '-') },
"div": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '/') },
"mod": Mod,
"mul": func(a, b interface{}) (interface{}, error) { return doArithmetic(a, b, '*') },
"modBool": ModBool,
"lower": func(a string) string { return strings.ToLower(a) },
"upper": func(a string) string { return strings.ToUpper(a) },
"title": func(a string) string { return strings.Title(a) },
"partial": Partial,
"ref": Ref,
"relref": RelRef,
"apply": Apply,
"chomp": Chomp,
"replace": Replace,
"trim": Trim,
"dateFormat": DateFormat,
"getJSON": GetJSON,
"getCSV": GetCSV,
"readDir": ReadDir,
"seq": helpers.Seq,
"getenv": func(varName string) string { return os.Getenv(varName) },
"base64Decode": Base64Decode,
"base64Encode": Base64Encode,
"pluralize": func(in interface{}) (string, error) {
word, err := cast.ToStringE(in)
if err != nil {
return "", err
}
return inflect.Pluralize(word), nil
},
"singularize": func(in interface{}) (string, error) {
word, err := cast.ToStringE(in)
if err != nil {
return "", err
}
return inflect.Singularize(word), nil
},
}
}
示例14: writeSchema
// writeSchema writes SQL statements to CREATE, INSERT,
// UPDATE and DELETE values from Table t.
func writeSchema(w io.Writer, d schema.Dialect, t *schema.Table) {
writeConst(w,
d.Table(t),
"create", inflect.Singularize(t.Name), "stmt",
)
writeConst(w,
d.Insert(t),
"insert", inflect.Singularize(t.Name), "stmt",
)
writeConst(w,
d.Select(t, nil),
"select", inflect.Singularize(t.Name), "stmt",
)
writeConst(w,
d.SelectRange(t, nil),
"select", inflect.Singularize(t.Name), "range", "stmt",
)
writeConst(w,
d.SelectCount(t, nil),
"select", inflect.Singularize(t.Name), "count", "stmt",
)
if len(t.Primary) != 0 {
writeConst(w,
d.Select(t, t.Primary),
"select", inflect.Singularize(t.Name), "pkey", "stmt",
)
writeConst(w,
d.Update(t, t.Primary),
"update", inflect.Singularize(t.Name), "pkey", "stmt",
)
writeConst(w,
d.Delete(t, t.Primary),
"delete", inflect.Singularize(t.Name), "pkey", "stmt",
)
}
for _, ix := range t.Index {
writeConst(w,
d.Index(t, ix),
"create", ix.Name, "stmt",
)
writeConst(w,
d.Select(t, ix.Fields),
"select", ix.Name, "stmt",
)
if !ix.Unique {
writeConst(w,
d.SelectRange(t, ix.Fields),
"select", ix.Name, "range", "stmt",
)
writeConst(w,
d.SelectCount(t, ix.Fields),
"select", ix.Name, "count", "stmt",
)
} else {
writeConst(w,
d.Update(t, ix.Fields),
"update", ix.Name, "stmt",
)
writeConst(w,
d.Delete(t, ix.Fields),
"delete", ix.Name, "stmt",
)
}
}
}
示例15: singularize
// singularize returns singular version of a word
func singularize(s string) string {
return inflect.Singularize(s)
}