github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/cmd/swagger/commands/diff/node.go (about)

     1  package diff
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/go-openapi/spec"
     7  )
     8  
     9  // Node is the position od a diff in a spec
    10  type Node struct {
    11  	Field     string `json:"name,omitempty"`
    12  	TypeName  string `json:"type,omitempty"`
    13  	IsArray   bool   `json:"is_array,omitempty"`
    14  	ChildNode *Node  `json:"child,omitempty"`
    15  }
    16  
    17  // String std string render
    18  func (n *Node) String() string {
    19  	name := n.Field
    20  	if n.IsArray {
    21  		name = fmt.Sprintf("%s<array[%s]>", name, n.TypeName)
    22  	} else if len(n.TypeName) > 0 {
    23  		name = fmt.Sprintf("%s<%s>", name, n.TypeName)
    24  	}
    25  	if n.ChildNode != nil {
    26  		return fmt.Sprintf("%s.%s", name, n.ChildNode.String())
    27  	}
    28  	return name
    29  }
    30  
    31  // AddLeafNode Adds (recursive) a Child to the first non-nil child found
    32  func (n *Node) AddLeafNode(toAdd *Node) *Node {
    33  
    34  	if n.ChildNode == nil {
    35  		n.ChildNode = toAdd
    36  	} else {
    37  		n.ChildNode.AddLeafNode(toAdd)
    38  	}
    39  
    40  	return n
    41  }
    42  
    43  // Copy deep copy of this node and children
    44  func (n Node) Copy() *Node {
    45  	newChild := n.ChildNode
    46  	if newChild != nil {
    47  		newChild = newChild.Copy()
    48  	}
    49  	newNode := Node{
    50  		Field:     n.Field,
    51  		TypeName:  n.TypeName,
    52  		IsArray:   n.IsArray,
    53  		ChildNode: newChild,
    54  	}
    55  
    56  	return &newNode
    57  }
    58  
    59  func getSchemaDiffNode(name string, schema interface{}) *Node {
    60  	node := Node{
    61  		Field: name,
    62  	}
    63  	if schema != nil {
    64  		switch s := schema.(type) {
    65  		case spec.Refable:
    66  			node.TypeName, node.IsArray = getSchemaType(s)
    67  		case *spec.Schema:
    68  			node.TypeName, node.IsArray = getSchemaType(s.SchemaProps)
    69  		case spec.SimpleSchema:
    70  			node.TypeName, node.IsArray = getSchemaType(s)
    71  		case *spec.SimpleSchema:
    72  			node.TypeName, node.IsArray = getSchemaType(s)
    73  		case *spec.SchemaProps:
    74  			node.TypeName, node.IsArray = getSchemaType(s)
    75  		case spec.SchemaProps:
    76  			node.TypeName, node.IsArray = getSchemaType(&s)
    77  		default:
    78  			node.TypeName = fmt.Sprintf("Unknown type %v", schema)
    79  		}
    80  	}
    81  	return &node
    82  }