UnmarshalJSON allows TemplatableInt32 to accept both JSON numbers (integer literals) and JSON strings that are GitHub Actions expressions. Free-form string literals that are not expressions are rejected with an error.
(data []byte)
| 62 | // literals) and JSON strings that are GitHub Actions expressions. |
| 63 | // Free-form string literals that are not expressions are rejected with an error. |
| 64 | func (t *TemplatableInt32) UnmarshalJSON(data []byte) error { |
| 65 | // Try a JSON number first (e.g. 30) |
| 66 | var n int32 |
| 67 | if err := json.Unmarshal(data, &n); err == nil { |
| 68 | *t = TemplatableInt32(strconv.FormatInt(int64(n), 10)) |
| 69 | return nil |
| 70 | } |
| 71 | // Try a JSON string (e.g. "${{ inputs.timeout }}") |
| 72 | var s string |
| 73 | if err := json.Unmarshal(data, &s); err != nil { |
| 74 | templatablesLog.Printf("TemplatableInt32 rejected: not number or string: %s", data) |
| 75 | return fmt.Errorf("timeout-minutes must be an integer or a GitHub Actions expression (e.g. '${{ inputs.timeout }}'), got %s", data) |
| 76 | } |
| 77 | if !isExpression(s) { |
| 78 | templatablesLog.Printf("TemplatableInt32 rejected non-expression string: %q", s) |
| 79 | return fmt.Errorf("timeout-minutes must be an integer or a GitHub Actions expression (e.g. '${{ inputs.timeout }}'), got string %q", s) |
| 80 | } |
| 81 | *t = TemplatableInt32(s) |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | // MarshalJSON emits a JSON number for numeric literals and a JSON string for |
| 86 | // GitHub Actions expressions. |
nothing calls this directly
no test coverage detected