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

Function GenerateEnums

pkg/codegen/codegen.go:994–1071  ·  view source on GitHub ↗
(t *template.Template, types []TypeDefinition)

Source from the content-addressed store, hash-verified

992}
993
994func GenerateEnums(t *template.Template, types []TypeDefinition) (string, error) {
995 enums := []EnumDefinition{}
996
997 // Keep track of which enums we've generated
998 m := map[string]bool{}
999
1000 // These are all types defined globally
1001 for _, tp := range types {
1002 if found := m[tp.TypeName]; found {
1003 continue
1004 }
1005
1006 m[tp.TypeName] = true
1007
1008 if len(tp.Schema.EnumValues) > 0 {
1009 wrapper := ""
1010 if tp.Schema.GoType == "string" {
1011 wrapper = `"`
1012 }
1013 enums = append(enums, EnumDefinition{
1014 Schema: tp.Schema,
1015 TypeName: tp.TypeName,
1016 ValueWrapper: wrapper,
1017 PrefixTypeName: globalState.options.Compatibility.AlwaysPrefixEnumValues || tp.ForceEnumPrefix,
1018 })
1019 }
1020 }
1021
1022 // Now, go through all the enums, and figure out if we have conflicts with
1023 // any others.
1024 for i := range enums {
1025 // Look through all other enums not compared so far. Make sure we don't
1026 // compare against self.
1027 e1 := enums[i]
1028 for j := i + 1; j < len(enums); j++ {
1029 e2 := enums[j]
1030
1031 for e1key := range e1.GetValues() {
1032 _, found := e2.GetValues()[e1key]
1033 if found {
1034 e1.PrefixTypeName = true
1035 e2.PrefixTypeName = true
1036 enums[i] = e1
1037 enums[j] = e2
1038 break
1039 }
1040 }
1041 }
1042
1043 // now see if this enum conflicts with any global type names.
1044 for _, tp := range types {
1045 // Skip over enums, since we've handled those above.
1046 if len(tp.Schema.EnumValues) > 0 {
1047 continue
1048 }
1049 _, found := e1.Schema.EnumValues[tp.TypeName]
1050 if found {
1051 e1.PrefixTypeName = true

Callers 2

GenerateFunction · 0.85
GenerateTypeDefinitionsFunction · 0.85

Calls 2

GenerateTemplatesFunction · 0.85
GetValuesMethod · 0.80

Tested by

no test coverage detected