From dc0b415d0f8e1ec363fdd998153c9a709cde194a Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Wed, 20 Sep 2023 11:22:21 +0000 Subject: [PATCH] Simplify schema loading --- bill/invoice_correct.go | 85 +++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/bill/invoice_correct.go b/bill/invoice_correct.go index f43e258b..adb0ffc7 100644 --- a/bill/invoice_correct.go +++ b/bill/invoice_correct.go @@ -126,7 +126,6 @@ func (inv *Invoice) CorrectionOptionsSchema() (interface{}, error) { if r == nil { return nil, nil } - cd := r.CorrectionDefinitionFor(ShortSchemaInvoice) schema := new(jsonschema.Schema) @@ -143,53 +142,57 @@ func (inv *Invoice) CorrectionOptionsSchema() (interface{}, error) { // Improve the quality of the schema schema.Required = append(schema.Required, "credit") - if cd != nil { - if cd.ReasonRequired { - schema.Required = append(schema.Required, "reason") - } - // These methods are quite ugly as the jsonschema was not designed - // for being able to load documents. - if len(cd.Methods) > 0 { - schema.Required = append(schema.Required, "method") - if prop, ok := schema.Properties.Get("method"); ok { - ps := prop.(orderedmap.OrderedMap) - oneOf := make([]*jsonschema.Schema, len(cd.Methods)) - for i, v := range cd.Methods { - oneOf[i] = &jsonschema.Schema{ - Const: v.Key.String(), - Title: v.Name.String(), - } - if !v.Desc.IsEmpty() { - oneOf[i].Description = v.Desc.String() - } + cd := r.CorrectionDefinitionFor(ShortSchemaInvoice) + if cd == nil { + return schema, nil + } + + if cd.ReasonRequired { + schema.Required = append(schema.Required, "reason") + } + + // These methods are quite ugly as the jsonschema was not designed + // for being able to load documents. + if len(cd.Methods) > 0 { + schema.Required = append(schema.Required, "method") + if prop, ok := schema.Properties.Get("method"); ok { + ps := prop.(orderedmap.OrderedMap) + oneOf := make([]*jsonschema.Schema, len(cd.Methods)) + for i, v := range cd.Methods { + oneOf[i] = &jsonschema.Schema{ + Const: v.Key.String(), + Title: v.Name.String(), + } + if !v.Desc.IsEmpty() { + oneOf[i].Description = v.Desc.String() } - ps.Set("oneOf", oneOf) - schema.Properties.Set("method", ps) } + ps.Set("oneOf", oneOf) + schema.Properties.Set("method", ps) } + } - if len(cd.Keys) > 0 { - schema.Required = append(schema.Required, "changes") - if prop, ok := schema.Properties.Get("changes"); ok { - ps := prop.(orderedmap.OrderedMap) - items, _ := ps.Get("items") - pi := items.(orderedmap.OrderedMap) - - oneOf := make([]*jsonschema.Schema, len(cd.Keys)) - for i, v := range cd.Keys { - oneOf[i] = &jsonschema.Schema{ - Const: v.Key.String(), - Title: v.Name.String(), - } - if !v.Desc.IsEmpty() { - oneOf[i].Description = v.Desc.String() - } + if len(cd.Keys) > 0 { + schema.Required = append(schema.Required, "changes") + if prop, ok := schema.Properties.Get("changes"); ok { + ps := prop.(orderedmap.OrderedMap) + items, _ := ps.Get("items") + pi := items.(orderedmap.OrderedMap) + + oneOf := make([]*jsonschema.Schema, len(cd.Keys)) + for i, v := range cd.Keys { + oneOf[i] = &jsonschema.Schema{ + Const: v.Key.String(), + Title: v.Name.String(), + } + if !v.Desc.IsEmpty() { + oneOf[i].Description = v.Desc.String() } - pi.Set("oneOf", oneOf) - ps.Set("items", pi) - schema.Properties.Set("changes", ps) } + pi.Set("oneOf", oneOf) + ps.Set("items", pi) + schema.Properties.Set("changes", ps) } }