Ai
11 Star 11 Fork 0

Gitee 极速下载/goa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/goadesign/goa
克隆/下载
merge.go 2.31 KB
一键复制 编辑 原始数据 按行查看 历史
Raphael Simon 提交于 2020-07-11 14:58 +08:00 . OpenAPI v3 (v2) (#2608)
package openapi
import "reflect"
// mergeItems is an internal datatype used to merge two schemas.
type mergeItems []struct {
a, b interface{}
needed bool
}
// Merge does a two level deep merge of other into s.
func (s *Schema) Merge(other *Schema) {
items := s.createMergeItems(other)
for _, v := range items {
if v.needed && v.b != nil {
reflect.Indirect(reflect.ValueOf(v.a)).Set(reflect.ValueOf(v.b))
}
}
for n, p := range other.Properties {
if _, ok := s.Properties[n]; !ok {
if s.Properties == nil {
s.Properties = make(map[string]*Schema)
}
s.Properties[n] = p
}
}
for n, d := range other.Definitions {
if _, ok := s.Definitions[n]; !ok {
s.Definitions[n] = d
}
}
s.Links = append(s.Links, other.Links...)
s.Required = append(s.Required, other.Required...)
}
func (s *Schema) createMergeItems(other *Schema) mergeItems {
minInt := func(a, b *int) bool { return (a == nil && b != nil) || (a != nil && b != nil && *a > *b) }
maxInt := func(a, b *int) bool { return (a == nil && b != nil) || (a != nil && b != nil && *a < *b) }
minFloat64 := func(a, b *float64) bool { return (a == nil && b != nil) || (a != nil && b != nil && *a > *b) }
maxFloat64 := func(a, b *float64) bool { return (a == nil && b != nil) || (a != nil && b != nil && *a < *b) }
return mergeItems{
{&s.ID, other.ID, s.ID == ""},
{&s.Type, other.Type, s.Type == ""},
{&s.Ref, other.Ref, s.Ref == ""},
{&s.Items, other.Items, s.Items == nil},
{&s.DefaultValue, other.DefaultValue, s.DefaultValue == nil},
{&s.Title, other.Title, s.Title == ""},
{&s.Media, other.Media, s.Media == nil},
{&s.ReadOnly, other.ReadOnly, !s.ReadOnly},
{&s.PathStart, other.PathStart, s.PathStart == ""},
{&s.Enum, other.Enum, s.Enum == nil},
{&s.Format, other.Format, s.Format == ""},
{&s.Pattern, other.Pattern, s.Pattern == ""},
{&s.AdditionalProperties, other.AdditionalProperties, !s.AdditionalProperties},
{&s.Minimum, other.Minimum, minFloat64(s.Minimum, other.Minimum)},
{&s.Maximum, other.Maximum, maxFloat64(s.Maximum, other.Maximum)},
{&s.MinLength, other.MinLength, minInt(s.MinLength, other.MinLength)},
{&s.MaxLength, other.MaxLength, maxInt(s.MaxLength, other.MaxLength)},
{&s.MinItems, other.MinItems, minInt(s.MinItems, other.MinItems)},
{&s.MaxItems, other.MaxItems, maxInt(s.MaxItems, other.MaxItems)},
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/goa.git
git@gitee.com:mirrors/goa.git
mirrors
goa
goa
v2.2.5

搜索帮助