|
| 1 | +package webhook |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | +) |
| 9 | + |
| 10 | +type Payload struct { |
| 11 | + Form map[string]struct { |
| 12 | + Label string `json:"label"` |
| 13 | + Type string `json:"type"` |
| 14 | + Required bool `json:"required"` |
| 15 | + Default interface{} `json:"default"` |
| 16 | + Value interface{} `json:"value"` |
| 17 | + } `json:"form"` |
| 18 | +} |
| 19 | + |
| 20 | +func main() { |
| 21 | + data, err := io.ReadAll(os.Stdin) |
| 22 | + if err != nil { |
| 23 | + panic(err) |
| 24 | + } |
| 25 | + |
| 26 | + var p Payload |
| 27 | + if err := json.Unmarshal(data, &p); err != nil { |
| 28 | + panic(err) |
| 29 | + } |
| 30 | + |
| 31 | + username, ok := p.Form["username"] |
| 32 | + if !ok { |
| 33 | + panic("username not found in form data") |
| 34 | + } |
| 35 | + checkType(username.Type, username.Value) |
| 36 | + if username.Value.(string) != "jolheiser" { |
| 37 | + panic("username should be jolheiser") |
| 38 | + } |
| 39 | + |
| 40 | + favNum, ok := p.Form["favorite-number"] |
| 41 | + if !ok { |
| 42 | + panic("favorite-number not found in form data") |
| 43 | + } |
| 44 | + checkType(favNum.Type, favNum.Value) |
| 45 | + if username.Value.(float64) != 12 { |
| 46 | + panic("favNum should be 12") |
| 47 | + } |
| 48 | + |
| 49 | + pineapple, ok := p.Form["pineapple"] |
| 50 | + if !ok { |
| 51 | + panic("pineapple not found in form data") |
| 52 | + } |
| 53 | + checkType(pineapple.Type, pineapple.Value) |
| 54 | + if pineapple.Value.(bool) { |
| 55 | + panic("pineapple should be false") |
| 56 | + } |
| 57 | + |
| 58 | + secret, ok := p.Form["secret"] |
| 59 | + if !ok { |
| 60 | + panic("secret not found in form data") |
| 61 | + } |
| 62 | + checkType(secret.Type, secret.Value) |
| 63 | + if secret.Value.(string) != "sn34ky" { |
| 64 | + panic("secret should be sn34ky") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func checkType(typ string, val interface{}) { |
| 69 | + var ok bool |
| 70 | + switch typ { |
| 71 | + case "text", "secret": |
| 72 | + _, ok = val.(string) |
| 73 | + case "bool": |
| 74 | + _, ok = val.(bool) |
| 75 | + case "number": |
| 76 | + _, ok = val.(float64) |
| 77 | + } |
| 78 | + if !ok { |
| 79 | + panic(fmt.Sprintf("unexpected type %q for %v", typ, val)) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// override panic |
| 84 | +func panic(v interface{}) { |
| 85 | + fmt.Println(v) |
| 86 | + os.Exit(1) |
| 87 | +} |
0 commit comments