优化示例值类型 #18
| @ -125,7 +125,7 @@ type Schema struct { | ||||
| 	WriteOnly        bool                 `json:"writeOnly,omitempty"`          // 仅与 Schema "properties" 定义有关。声明此 property 为 "write only"。所以它可以作为 request 的一部分而不应该作为 response 的一部分被发送。如果一个 property 的 writeOnly 被标记为 true 且在 required 列表中,required 将只作用于 request。一个 property 的 readOnly 和 writeOnly 不能同时被标记为 true。默认值是 false。 | ||||
| 	Xml              *XML                 `json:"xml,omitempty"`                // 这只能用于 properties schemas,在root schemas 中没有效果。 | ||||
| 	ExternalDocs     *ExternalDocs        `json:"externalDocs,omitempty"`       // 此 schema 附加的外部文档。 | ||||
| 	Example          string               `json:"example,omitempty"`            // 一个用于示范此 schema实例的示例,可以是任意格式。为了表达无法用 JSON 或 YAML 格式呈现的示例,可以使用 string 类型的值,且在必要的地方需要使用字符转义。 | ||||
| 	Example          any                  `json:"example,omitempty"`            // 一个用于示范此 schema实例的示例,可以是任意格式。为了表达无法用 JSON 或 YAML 格式呈现的示例,可以使用 string 类型的值,且在必要的地方需要使用字符转义。 | ||||
| 	Description      string               `json:"description,omitempty"`        // 一个用于示范此 schema实例的示例,可以是任意格式。为了表达无法用 JSON 或 YAML 格式呈现的示例,可以使用 string 类型的值,且在必要的地方需要使用字符转义。 | ||||
| 	Deprecated       bool                 `json:"deprecated,omitempty"`         // 表示一个 schema 是废弃的,应该逐渐被放弃使用。默认值是 false. | ||||
| 	Properties       map[string]*Property `json:"properties,omitempty"`         // 数据字段 => 数据规则 | ||||
| @ -156,7 +156,7 @@ type Property struct { | ||||
| 	XEnumDescription     map[string]string    `json:"x-enumDescriptions,omitempty"`   // 枚举值描述的扩展, redoc-free支持 | ||||
| 	Default              any                  `json:"default,omitempty"`              // 默认值 : 不同于 JSON Schema,这个值必须符合定义与相同级别的 Schema 对象 中定义的类型,比如 type 是 string,那么 default 可以是 "foo" 但不能是 1。 | ||||
| 	Description          string               `json:"description,omitempty"`          // 数据描述, CommonMark syntax可以被用来呈现富文本格式. | ||||
| 	Example              string               `json:"example,omitempty"`              // 媒体类型的示例。示例对象应该符合此媒体类型的格式, 这里指定的example对象 object is mutually exclusive of the examples object. 而且如果引用的schema也包含示例,在这里指定的example值将会覆盖schema提供的示例。 | ||||
| 	Example              any                  `json:"example,omitempty"`              // 媒体类型的示例。示例对象应该符合此媒体类型的格式, 这里指定的example对象 object is mutually exclusive of the examples object. 而且如果引用的schema也包含示例,在这里指定的example值将会覆盖schema提供的示例。 | ||||
| 	Maximum              *int64               `json:"maximum,omitempty"`              // 最大值 | ||||
| 	Minimum              *int64               `json:"minimum,omitempty"`              // 最小值 | ||||
| 	MinLength            *int64               `json:"minLength,omitempty"`            // 字符串最小长度 | ||||
|  | ||||
| @ -184,13 +184,37 @@ func (psf parseStructFieldTag) EnumDescription(structField reflect.StructField) | ||||
| // Author : go_developer@163.com<白茶清欢> | ||||
| // | ||||
| // Date : 14:42 2025/2/20 | ||||
| func (psf parseStructFieldTag) GetExampleValue(structField reflect.StructField) string { | ||||
| func (psf parseStructFieldTag) GetExampleValue(structField reflect.StructField) any { | ||||
| 	descTagList := []string{define.TagEg, define.TagExample} | ||||
| 	fieldType := structField.Type.Kind().String() | ||||
| 	for _, tag := range descTagList { | ||||
| 		tagVal := structField.Tag.Get(tag) | ||||
| 		if tagVal != "" { | ||||
| 			return strings.ReplaceAll(tagVal, "###", "`") | ||||
| 		val := strings.TrimSpace(structField.Tag.Get(tag)) | ||||
| 		if val == "" { | ||||
| 			continue | ||||
| 		} | ||||
| 		if strings.HasPrefix(fieldType, "int") { | ||||
| 			i, _ := strconv.Atoi(val) | ||||
| 			return i | ||||
| 		} | ||||
| 		if strings.HasPrefix(fieldType, "uint") { | ||||
| 			uintVal, _ := strconv.ParseUint(val, 10, 64) | ||||
| 			return uintVal | ||||
| 		} | ||||
| 		if strings.HasPrefix(fieldType, "float") { | ||||
| 			floatVal, _ := strconv.ParseFloat(val, 64) | ||||
| 			return floatVal | ||||
| 		} | ||||
| 		if strings.HasPrefix(fieldType, "string") { | ||||
| 			return val | ||||
| 		} | ||||
| 		if strings.HasPrefix(fieldType, "bool") { | ||||
| 			if val == "true" { | ||||
| 				return true | ||||
| 			} else { | ||||
| 				return false | ||||
| 			} | ||||
| 		} | ||||
| 		return val | ||||
| 	} | ||||
| 	return "" | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user