| 25 | } |
| 26 | |
| 27 | func TestSDKFieldsFormatted(t *testing.T) { |
| 28 | t.Parallel() |
| 29 | |
| 30 | fileSet := token.NewFileSet() |
| 31 | nodes, err := parser.ParseDir(fileSet, "../../codersdk", nil, parser.ParseComments) |
| 32 | require.NoError(t, err, "parser.ParseDir failed") |
| 33 | |
| 34 | for _, node := range nodes { |
| 35 | ast.Inspect(node, func(n ast.Node) bool { |
| 36 | typeSpec, ok := n.(*ast.TypeSpec) |
| 37 | if !ok { |
| 38 | return true |
| 39 | } |
| 40 | structureName := typeSpec.Name |
| 41 | |
| 42 | structType, ok := typeSpec.Type.(*ast.StructType) |
| 43 | if !ok { |
| 44 | return true // not a structure |
| 45 | } |
| 46 | |
| 47 | for _, field := range structType.Fields.List { |
| 48 | selectorExpr, ok := field.Type.(*ast.SelectorExpr) |
| 49 | if !ok { |
| 50 | continue // rather a basic, or primitive |
| 51 | } |
| 52 | |
| 53 | if field.Tag == nil || !strings.Contains(field.Tag.Value, `json:"`) { |
| 54 | continue // not a JSON property |
| 55 | } |
| 56 | |
| 57 | switch selectorExpr.Sel.Name { |
| 58 | case "UUID": |
| 59 | assert.Contains(t, field.Tag.Value, `format:"uuid"`, `Swagger formatting requires to annotate the field with - format:"uuid". Location: %s/%s`, structureName, field.Names) |
| 60 | case "Time": |
| 61 | assert.Contains(t, field.Tag.Value, `format:"date-time"`, `Swagger formatting requires to annotate the field with - format:"date-time". Location: %s/%s`, structureName, field.Names) |
| 62 | } |
| 63 | } |
| 64 | return true |
| 65 | }) |
| 66 | } |
| 67 | } |