@@ -110,6 +110,7 @@ const (
110110 paramOmitEmpty = "omitempty"
111111 paramReadOnly = "readonly"
112112 paramOmitDoc = "omitdoc"
113+ paramEnum = "enum"
113114)
114115
115116func setTags (name , fName string , p * Schema , tags []string ) error {
@@ -129,6 +130,9 @@ func setTags(name, fName string, p *Schema, tags []string) error {
129130 case paramReadOnly :
130131 t := true
131132 p .Readonly = & t
133+ case paramEnum :
134+ // For this type of enum, we figure out the variations based on the type.
135+ p .Type = "enum"
132136
133137 // Various string formats.
134138 // https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7.3
@@ -274,6 +278,15 @@ start:
274278
275279 // Simple identifiers such as "string", "int", "MyType", etc.
276280 case * ast.Ident :
281+ if p .Type == "enum" && len (p .Enum ) == 0 {
282+ if variations , err := getEnumVariations (ref .File , pkg , typ .Name ); len (variations ) > 0 {
283+ p .Enum = variations
284+ return & p , nil
285+ } else if err != nil {
286+ return nil , err
287+ }
288+ }
289+
277290 mappedType , mappedFormat := MapType (prog , pkg + "." + typ .Name )
278291 if mappedType == "" {
279292 // Only check for canonicalType if this isn't mapped.
@@ -443,6 +456,33 @@ start:
443456 return & p , nil
444457}
445458
459+ // Helper function to extract enum variations from a file.
460+ func getEnumVariations (currentFile , pkgPath , typeName string ) ([]string , error ) {
461+ resolvedPath , pkg , err := resolvePackage (currentFile , pkgPath )
462+ if err != nil {
463+ return nil , fmt .Errorf ("could not resolve package: %v" , err )
464+ }
465+ decls , err := getDecls (pkg , resolvedPath )
466+ if err != nil {
467+ return nil , err
468+ }
469+ var variations []string
470+ for _ , decl := range decls {
471+ if decl .vs == nil {
472+ continue
473+ }
474+ if exprToString (decl .vs .Type ) != typeName {
475+ continue
476+ }
477+ if len (decl .vs .Values ) == 0 {
478+ continue
479+ }
480+ variations = append (variations , exprToString (decl .vs .Values [0 ]))
481+ }
482+
483+ return variations , nil
484+ }
485+
446486func dropTypePointers (typ ast.Expr ) ast.Expr {
447487 var t * ast.StarExpr
448488 var ok bool
0 commit comments