thedevsaddam / gojsonq

A simple Go package to Query over JSON/YAML/XML/CSV Data
https://github.com/thedevsaddam/gojsonq/wiki
MIT License
2.17k stars 140 forks source link

Why interfaces? #79

Closed nkev closed 4 years ago

nkev commented 4 years ago

Thanks for sharing this project. One question. Why do you do this:

// strStrictContains checks if x contains y
// This is case sensitive search
func strStrictContains(x, y interface{}) (bool, error) {
    xv, okX := x.(string)
    if !okX {
        return false, fmt.Errorf("%v must be string", x)
    }
    yv, okY := y.(string)
    if !okY {
        return false, fmt.Errorf("%v must be string", y)
    }
    return strings.Contains(xv, yv), nil
}

...and not this:

// strStrictContains checks if x contains y
// This is case sensitive search
func strStrictContains(x, y string) (bool) {
    return strings.Contains(xv, yv)
}

...or in this case, simply strings.Contains(xv, yv) without a wrapper.

In this example, you are only allowing string inputs anyway, so why not use strongly typed strings as input params instead of interface{}?

nkev commented 4 years ago

Ah, I think I worked it out. It's because all the operations need to implement the same interface, so they can be chained:

// QueryFunc describes a conditional function which perform comparison
type QueryFunc func(x, y interface{}) (bool, error)
thedevsaddam commented 4 years ago

I hope you get the answer yourself.