MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / ParseGoImportExtension

Function ParseGoImportExtension

pkg/codegen/utils.go:1098–1146  ·  view source on GitHub ↗
(v *openapi3.SchemaRef)

Source from the content-addressed store, hash-verified

1096}
1097
1098func ParseGoImportExtension(v *openapi3.SchemaRef) (*goImport, error) {
1099 // An x-go-type-import is only meaningful in concert with an x-go-type
1100 // override. Without one, the imported package is never referenced in
1101 // the generated code, producing an "imported and not used" compile
1102 // error. Require at least one x-go-type to be in scope (either next to
1103 // the $ref or on the referenced schema) before collecting an import.
1104 hasGoType := v.Extensions[extPropGoType] != nil ||
1105 (v.Value != nil && v.Value.Extensions[extPropGoType] != nil)
1106 if !hasGoType {
1107 return nil, nil
1108 }
1109
1110 var goTypeImportExt any
1111
1112 // check extensions next to the $ref before checking the schema itself
1113 // values next to $ref will be used before those in the actual schema
1114 if v.Extensions[extPropGoImport] != nil {
1115 goTypeImportExt = v.Extensions[extPropGoImport]
1116 } else if v.Value != nil && v.Value.Extensions[extPropGoImport] != nil {
1117 goTypeImportExt = v.Value.Extensions[extPropGoImport]
1118 } else {
1119 return nil, nil
1120 }
1121
1122 importI, ok := goTypeImportExt.(map[string]any)
1123 if !ok {
1124 return nil, fmt.Errorf("failed to convert type: %T", goTypeImportExt)
1125 }
1126
1127 gi := goImport{}
1128 // replicate the case-insensitive field mapping json.Unmarshal would do
1129 for k, v := range importI {
1130 if strings.EqualFold(k, "name") {
1131 if vs, ok := v.(string); ok {
1132 gi.Name = vs
1133 } else {
1134 return nil, fmt.Errorf("failed to convert type: %T", v)
1135 }
1136 } else if strings.EqualFold(k, "path") {
1137 if vs, ok := v.(string); ok {
1138 gi.Path = vs
1139 } else {
1140 return nil, fmt.Errorf("failed to convert type: %T", v)
1141 }
1142 }
1143 }
1144
1145 return &gi, nil
1146}
1147
1148// TypeDefinitionsEquivalent checks for equality between two type definitions, but
1149// not every field is considered. We only want to know if they are fundamentally

Callers 1

GoSchemaImportsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected