fix(load): idempotency (#23)

This commit is contained in:
Jeroen Op 't Eynde 2021-06-17 13:16:55 +02:00 committed by GitHub
parent 6be08106d0
commit ee3cb6711f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,37 +22,34 @@ func fastLoad(d ds) Package {
continue continue
} }
n := strings.TrimPrefix(k, "#")
f := v.(map[string]interface{}) f := v.(map[string]interface{})
// field // is it a docstring?
name := strings.TrimPrefix(k, "#")
if strings.HasPrefix(k, "#") { if strings.HasPrefix(k, "#") {
pkg.API[name] = loadField(name, f, d) pkg.API[n] = loadField(n, f, d)
continue continue
} }
// non-docsonnet // is it a package?
// subpackage?
if _, ok := f["#"]; ok { if _, ok := f["#"]; ok {
p := fastLoad(ds(f)) p := fastLoad(ds(f))
pkg.Sub[p.Name] = p pkg.Sub[p.Name] = p
continue continue
} }
// non-annotated nested? // is it a regular field? check nested...
// try to load, but skip when already loaded as annotated above if nested, ok := loadNested(n, f); ok && !hasDocstring(n, d) {
if nested, ok := loadNested(name, f); ok && !fieldsHas(pkg.API, name) { pkg.API[n] = *nested
pkg.API[name] = *nested
continue
} }
} }
return pkg return pkg
} }
func fieldsHas(f Fields, key string) bool { func hasDocstring(key string, msi map[string]interface{}) bool {
_, b := f[key] _, ok := msi["#"+key]
return b return ok
} }
func loadNested(name string, msi map[string]interface{}) (*Field, bool) { func loadNested(name string, msi map[string]interface{}) (*Field, bool) {
@ -61,25 +58,20 @@ func loadNested(name string, msi map[string]interface{}) (*Field, bool) {
Fields: make(Fields), Fields: make(Fields),
} }
ok := false
for k, v := range msi { for k, v := range msi {
f := v.(map[string]interface{})
n := strings.TrimPrefix(k, "#") n := strings.TrimPrefix(k, "#")
f := v.(map[string]interface{})
if !strings.HasPrefix(k, "#") { // is it a docstring?
if l, ok := loadNested(k, f); ok { if strings.HasPrefix(k, "#") {
out.Fields[n] = *l out.Fields[n] = loadField(n, f, msi)
}
continue continue
} }
ok = true // is it a regular field? check nested...
l := loadField(n, f, msi) if nested, ok := loadNested(n, f); ok && !hasDocstring(n, msi) {
out.Fields[n] = l out.Fields[n] = *nested
} }
if !ok {
return nil, false
} }
return &Field{Object: &out}, true return &Field{Object: &out}, true
@ -165,7 +157,7 @@ func loadObj(name string, msi map[string]interface{}, parent map[string]interfac
Fields: make(Fields), Fields: make(Fields),
} }
// look for children in same key without # // look for children in same key
var iChilds interface{} var iChilds interface{}
var ok bool var ok bool
if iChilds, ok = parent[name]; !ok { if iChilds, ok = parent[name]; !ok {
@ -174,17 +166,8 @@ func loadObj(name string, msi map[string]interface{}, parent map[string]interfac
} }
childs := iChilds.(map[string]interface{}) childs := iChilds.(map[string]interface{})
for k, v := range childs { if nested, ok := loadNested(name, childs); ok {
name := strings.TrimPrefix(k, "#") obj.Fields = nested.Object.Fields
f := v.(map[string]interface{})
if !strings.HasPrefix(k, "#") {
if l, ok := loadNested(k, f); ok {
obj.Fields[name] = *l
}
continue
}
obj.Fields[name] = loadField(name, f, childs)
} }
return Field{Object: &obj} return Field{Object: &obj}