getExpiresIntegerToDayStringCodemod creates a codemod for converting integer expires values to day strings. Converts e.g. "expires: 7" to "expires: 7d" in all safe-outputs types.
()
| 16 | // getExpiresIntegerToDayStringCodemod creates a codemod for converting integer expires values to day strings. |
| 17 | // Converts e.g. "expires: 7" to "expires: 7d" in all safe-outputs types. |
| 18 | func getExpiresIntegerToDayStringCodemod() Codemod { |
| 19 | return Codemod{ |
| 20 | ID: "expires-integer-to-string", |
| 21 | Name: "Convert expires integer to day string", |
| 22 | Description: "Converts integer 'expires' values (e.g., 'expires: 7') to day string format (e.g., 'expires: 7d') in safe-outputs types", |
| 23 | IntroducedIn: "0.13.0", |
| 24 | Apply: func(content string, frontmatter map[string]any) (string, bool, error) { |
| 25 | // Check if safe-outputs exists |
| 26 | safeOutputsValue, hasSafeOutputs := frontmatter["safe-outputs"] |
| 27 | if !hasSafeOutputs { |
| 28 | return content, false, nil |
| 29 | } |
| 30 | |
| 31 | safeOutputsMap, ok := safeOutputsValue.(map[string]any) |
| 32 | if !ok { |
| 33 | return content, false, nil |
| 34 | } |
| 35 | |
| 36 | // Check if any safe-outputs type has an integer expires value |
| 37 | hasIntegerExpires := false |
| 38 | for _, outputTypeValue := range safeOutputsMap { |
| 39 | outputTypeMap, ok := outputTypeValue.(map[string]any) |
| 40 | if !ok { |
| 41 | continue |
| 42 | } |
| 43 | if expiresValue, hasExpires := outputTypeMap["expires"]; hasExpires { |
| 44 | switch expiresValue.(type) { |
| 45 | case int, int64, uint64: |
| 46 | hasIntegerExpires = true |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if !hasIntegerExpires { |
| 52 | return content, false, nil |
| 53 | } |
| 54 | |
| 55 | newContent, applied, err := applyFrontmatterLineTransform(content, convertExpiresIntegersToDayStrings) |
| 56 | if applied { |
| 57 | expiresIntegerCodemodLog.Print("Applied expires integer-to-string migration") |
| 58 | } |
| 59 | return newContent, applied, err |
| 60 | }, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // convertExpiresIntegersToDayStrings converts integer expires values to day strings within safe-outputs blocks. |
| 65 | // Only affects expires lines nested inside a safe-outputs block. |