github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/cmd/swagger/commands/diff/schema.go (about) 1 package diff 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/go-openapi/spec" 8 ) 9 10 func getTypeFromSchema(schema *spec.Schema) (typeName string, isArray bool) { 11 refStr := definitionFromRef(schema.Ref) 12 if len(refStr) > 0 { 13 return refStr, false 14 } 15 typeName = schema.Type[0] 16 if typeName == ArrayType { 17 typeName, _ = getSchemaType(&schema.Items.Schema.SchemaProps) 18 return typeName, true 19 } 20 return typeName, false 21 22 } 23 24 func getTypeFromSimpleSchema(schema *spec.SimpleSchema) (typeName string, isArray bool) { 25 typeName = schema.Type 26 format := schema.Format 27 if len(format) > 0 { 28 typeName = fmt.Sprintf("%s.%s", typeName, format) 29 } 30 if typeName == ArrayType { 31 typeName, _ = getSchemaType(&schema.Items.SimpleSchema) 32 return typeName, true 33 } 34 return typeName, false 35 36 } 37 38 func getTypeFromSchemaProps(schema *spec.SchemaProps) (typeName string, isArray bool) { 39 refStr := definitionFromRef(schema.Ref) 40 if len(refStr) > 0 { 41 return refStr, false 42 } 43 if len(schema.Type) > 0 { 44 typeName = schema.Type[0] 45 format := schema.Format 46 if len(format) > 0 { 47 typeName = fmt.Sprintf("%s.%s", typeName, format) 48 } 49 if typeName == ArrayType { 50 typeName, _ = getSchemaType(&schema.Items.Schema.SchemaProps) 51 return typeName, true 52 } 53 } 54 return typeName, false 55 56 } 57 58 func getSchemaTypeStr(item interface{}) string { 59 typeStr, isArray := getSchemaType(item) 60 return formatTypeString(typeStr, isArray) 61 } 62 63 func getSchemaType(item interface{}) (typeName string, isArray bool) { 64 65 switch s := item.(type) { 66 case *spec.Schema: 67 typeName, isArray = getTypeFromSchema(s) 68 case *spec.SchemaProps: 69 typeName, isArray = getTypeFromSchemaProps(s) 70 case spec.SchemaProps: 71 typeName, isArray = getTypeFromSchemaProps(&s) 72 case spec.SimpleSchema: 73 typeName, isArray = getTypeFromSimpleSchema(&s) 74 case *spec.SimpleSchema: 75 typeName, isArray = getTypeFromSimpleSchema(s) 76 default: 77 typeName = "unknown" 78 } 79 80 return 81 82 } 83 84 func formatTypeString(typ string, isarray bool) string { 85 if isarray { 86 return fmt.Sprintf("<array[%s]>", typ) 87 } 88 return fmt.Sprintf("<%s>", typ) 89 } 90 91 func definitionFromRef(ref spec.Ref) string { 92 url := ref.GetURL() 93 if url == nil { 94 return "" 95 } 96 fragmentParts := strings.Split(url.Fragment, "/") 97 numParts := len(fragmentParts) 98 99 return fragmentParts[numParts-1] 100 } 101 102 func isArray(item interface{}) bool { 103 switch s := item.(type) { 104 case *spec.Schema: 105 return isArrayType(s.Type) 106 case *spec.SchemaProps: 107 return isArrayType(s.Type) 108 case *spec.SimpleSchema: 109 return isArrayType(spec.StringOrArray{s.Type}) 110 default: 111 return false 112 } 113 } 114 115 func isPrimitive(item interface{}) bool { 116 switch s := item.(type) { 117 case *spec.Schema: 118 return isPrimitiveType(s.Type) 119 case *spec.SchemaProps: 120 return isPrimitiveType(s.Type) 121 case spec.StringOrArray: 122 return isPrimitiveType(s) 123 default: 124 return false 125 } 126 }