You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following is a terrible workaround that I'm posting in case someone else needs it.
This implements source.NewClient via reflect, and then passes it to the FileURL method via reflect also.
It's particularly terrible because it's using reflect.Value.Set on an unexported field which should really not be done.
The better approach would be for the package maintainers to provide a legal way to construct a source.Client.
package pleasedontusethis
import (
"context""net/http""reflect""time""github.com/google/go-licenses/v2/licenses""go.opencensus.io/plugin/ochttp"
)
varlibraryFunc=reflect.ValueOf((*licenses.Library).FileURL)
varsourceClientTyp=libraryFunc.Type().In(2).Elem() // reflect.TypeFor[source.Client]// like (*licenses.Library).FileURL, but not taking an internal parameterfuncgoLicensesLibraryFileURL(lib*licenses.Library, context context.Context, timeout time.Duration, filestring) (string, error) {
result:=libraryFunc.Call([]reflect.Value{
reflect.ValueOf(lib),
reflect.ValueOf(context),
newSourceClient(timeout),
reflect.ValueOf(file),
})
str:=result[0].String()
err, _:=result[1].Interface().(error)
returnstr, err
}
// like source.NewClient, but not internalfuncnewSourceClient(timeout time.Duration) reflect.Value {
client:=&http.Client{
Transport: &ochttp.Transport{},
Timeout: timeout,
}
sourceClient:=reflect.New(sourceClientTyp)
// get the httpClient field// and forget that it was retrieved by means of an unexported fieldhttpClientField:=sourceClient.Elem().FieldByName("httpClient")
httpClientField=reflect.NewAt(httpClientField.Type(), httpClientField.Addr().UnsafePointer()).Elem()
httpClientField.Set(reflect.ValueOf(client))
returnsourceClient
}
The public method license.FileURL requires a source.Client.
There is currently no way to construct a source.Client when consuming go-licenses as a library consumer.
The text was updated successfully, but these errors were encountered: