I'm new to Golang and using a Go library for processing some webhook events from Github.
I've access to a Deployment's Payload
struct defined here:
https://github.com/go-playground/webhooks/blob/v3/github/payload.go#L384
The library parses the webhook JSON payload and constructs that. This is a custom field, i.e. it's a hashmap/dictionary whose fields can be custom set by the client.
So I think it's being defined as an empty struct by the library. How do I extract a specific field called "foo" from this struct?
Now there are certain limitations on what you are able to achieve, but by using the reflect package you can easily check to see if your object is empty or not:
package main
import (
"fmt"
"reflect"
"strconv"
)
type emptiness struct {}
type thing struct {
stuff string
}
func main() {
e := emptiness{}
t := thing{
stuff: "present",
}
fmt.Println(t.stuff)
v := reflect.ValueOf(t)
fmt.Println(strconv.Itoa(v.NumField()))
v = reflect.ValueOf(e)
fmt.Println(strconv.Itoa(v.NumField()))
if v.NumField() == 0 {
// handle your empty object accordingly
}
}
Edit: Forgot to add the runnable example's link. you can play with it and get more info using reflect as well but if you just want to check if it's empty, this will work.