| 1 |
// Copyright 2023-2026 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package errors |
| 5 |
|
| 6 |
import ( |
| 7 |
"go.yaml.in/yaml/v4" |
| 8 |
|
| 9 |
v3 "github.com/pb33f/libopenapi/datamodel/high/v3" |
| 10 |
|
| 11 |
"github.com/pb33f/libopenapi/datamodel/high/base" |
| 12 |
) |
| 13 |
|
| 14 |
// SafeNodeLineCol safely extracts line and column from a yaml.Node pointer. |
| 15 |
// Returns (1, 0) if the node is nil. |
| 16 |
func SafeNodeLineCol(node *yaml.Node) (int, int) { |
| 17 |
if node == nil { |
| 18 |
return 1, 0 |
| 19 |
} |
| 20 |
return node.Line, node.Column |
| 21 |
} |
| 22 |
|
| 23 |
// paramExplodeLineCol safely extracts SpecLine/SpecCol from param.GoLow().Explode.ValueNode. |
| 24 |
func paramExplodeLineCol(param *v3.Parameter) (int, int) { |
| 25 |
if low := param.GoLow(); low != nil && low.Explode.ValueNode != nil { |
| 26 |
return low.Explode.ValueNode.Line, low.Explode.ValueNode.Column |
| 27 |
} |
| 28 |
return 1, 0 |
| 29 |
} |
| 30 |
|
| 31 |
// paramStyleLineCol safely extracts SpecLine/SpecCol from param.GoLow().Style.ValueNode. |
| 32 |
func paramStyleLineCol(param *v3.Parameter) (int, int) { |
| 33 |
if low := param.GoLow(); low != nil && low.Style.ValueNode != nil { |
| 34 |
return low.Style.ValueNode.Line, low.Style.ValueNode.Column |
| 35 |
} |
| 36 |
return 1, 0 |
| 37 |
} |
| 38 |
|
| 39 |
// paramRequiredLineCol safely extracts SpecLine/SpecCol from param.GoLow().Required.KeyNode. |
| 40 |
func paramRequiredLineCol(param *v3.Parameter) (int, int) { |
| 41 |
if low := param.GoLow(); low != nil && low.Required.KeyNode != nil { |
| 42 |
return low.Required.KeyNode.Line, low.Required.KeyNode.Column |
| 43 |
} |
| 44 |
return 1, 0 |
| 45 |
} |
| 46 |
|
| 47 |
// paramSchemaKeyLineCol safely extracts SpecLine/SpecCol from param.GoLow().Schema.KeyNode. |
| 48 |
func paramSchemaKeyLineCol(param *v3.Parameter) (int, int) { |
| 49 |
if low := param.GoLow(); low != nil && low.Schema.KeyNode != nil { |
| 50 |
return low.Schema.KeyNode.Line, low.Schema.KeyNode.Column |
| 51 |
} |
| 52 |
return 1, 0 |
| 53 |
} |
| 54 |
|
| 55 |
// paramSchemaTypeLineCol safely extracts SpecLine/SpecCol from |
| 56 |
// param.GoLow().Schema.Value.Schema().Type.KeyNode. |
| 57 |
func paramSchemaTypeLineCol(param *v3.Parameter) (int, int) { |
| 58 |
if low := param.GoLow(); low != nil { |
| 59 |
if sv := low.Schema.Value; sv != nil { |
| 60 |
if s := sv.Schema(); s != nil && s.Type.KeyNode != nil { |
| 61 |
return s.Type.KeyNode.Line, s.Type.KeyNode.Column |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
return 1, 0 |
| 66 |
} |
| 67 |
|
| 68 |
// paramSchemaEnumLineCol safely extracts SpecLine/SpecCol from |
| 69 |
// param.GoLow().Schema.Value.Schema().Enum.KeyNode. |
| 70 |
func paramSchemaEnumLineCol(param *v3.Parameter) (int, int) { |
| 71 |
if low := param.GoLow(); low != nil { |
| 72 |
if sv := low.Schema.Value; sv != nil { |
| 73 |
if s := sv.Schema(); s != nil && s.Enum.KeyNode != nil { |
| 74 |
return s.Enum.KeyNode.Line, s.Enum.KeyNode.Column |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
return 1, 0 |
| 79 |
} |
| 80 |
|
| 81 |
// paramContentLineCol safely extracts SpecLine/SpecCol from |
| 82 |
// param.GoLow().FindContent(ct).ValueNode. |
| 83 |
func paramContentLineCol(param *v3.Parameter, contentType string) (int, int) { |
| 84 |
if low := param.GoLow(); low != nil { |
| 85 |
if found := low.FindContent(contentType); found != nil && found.ValueNode != nil { |
| 86 |
return found.ValueNode.Line, found.ValueNode.Column |
| 87 |
} |
| 88 |
} |
| 89 |
return 1, 0 |
| 90 |
} |
| 91 |
|
| 92 |
// schemaItemsTypeLineCol safely extracts SpecLine/SpecCol from |
| 93 |
// sch.Items.A.GoLow().Schema().Type.KeyNode. |
| 94 |
func schemaItemsTypeLineCol(sch *base.Schema) (int, int) { |
| 95 |
if sch.Items != nil && sch.Items.A != nil { |
| 96 |
if low := sch.Items.A.GoLow(); low != nil { |
| 97 |
if s := low.Schema(); s != nil && s.Type.KeyNode != nil { |
| 98 |
return s.Type.KeyNode.Line, s.Type.KeyNode.Column |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
return 1, 0 |
| 103 |
} |