當前位置: 首頁>>代碼示例>>Golang>>正文


Golang inflect.Singularize函數代碼示例

本文整理匯總了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
		}
	}
}
開發者ID:derekperkins,項目名稱:gorma,代碼行數:32,代碼來源:relationalmodel.go

示例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))
}
開發者ID:DuoSoftware,項目名稱:v6engine-deps,代碼行數:15,代碼來源:inflect.go

示例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
		}
	}
}
開發者ID:derekperkins,項目名稱:gorma,代碼行數:57,代碼來源:relationalmodel.go

示例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,
	}
}
開發者ID:ScruffyProdigy,項目名稱:Middleware,代碼行數:31,代碼來源:mapper.go

示例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
}
開發者ID:DimShadoWWW,項目名稱:hugo,代碼行數:8,代碼來源:template_funcs.go

示例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)
		}
	}

}
開發者ID:lopaka,項目名稱:rsc,代碼行數:35,代碼來源:api_analyzer.go

示例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
		}
	}
}
開發者ID:derekperkins,項目名稱:gorma,代碼行數:53,代碼來源:relationalmodel.go

示例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)
}
開發者ID:lopaka,項目名稱:rsc,代碼行數:12,代碼來源:api_analyzer.go

示例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
}
開發者ID:rightscale,項目名稱:self-service-plugins,代碼行數:9,代碼來源:handlers.go

示例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
}
開發者ID:cdwilhelm,項目名稱:self-service-plugins,代碼行數:10,代碼來源:handlers.go

示例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
}
開發者ID:emilsjolander,項目名稱:goroutes,代碼行數:13,代碼來源:goroutes.go

示例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
}
開發者ID:dylanmei,項目名稱:rsc,代碼行數:52,代碼來源:resource_analysis.go

示例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
		},
	}
}
開發者ID:sarbjit399,項目名稱:hugo,代碼行數:73,代碼來源:template_funcs.go

示例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",
			)
		}
	}
}
開發者ID:scotthelm,項目名稱:sqlgen,代碼行數:84,代碼來源:gen_schema.go

示例15: singularize

// singularize returns singular version of a word
func singularize(s string) string {
	return inflect.Singularize(s)
}
開發者ID:Jumpscale,項目名稱:go-raml,代碼行數:4,代碼來源:inflection.go


注:本文中的bitbucket/org/pkg/inflect.Singularize函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。