func computeCompatibleRange() semver.Range { v, err := semver.Parse(Version) if err != nil { panic(err) } return semver.CompatibleRange(v) }
// CheckCompatWithGeneratedCodeAt will panic if the ThriftRW version used to // generated code (given by `genCodeVersion`) is not compatible with the // current version of ThriftRW. // This function is designed to be called during initialization of the // generated code. // // Rationale: It is possible that the old generated code is not compatible with // the new version of ThriftRW in subtle ways but still compiles successfully. // This function will ensure that the version mismatch is detected and help // avoid bugs that could be caused by this discrepancy. func CheckCompatWithGeneratedCodeAt(genCodeVersion string, fromPkg string) { v, err := semver.Parse(genCodeVersion) if err != nil { panic(err) } if !compatRange.Contains(v) { log.Panicf(`incompatible version from generated package %q, expected >=%s and <%s, got %s`, fromPkg, &compatRange.Begin, &compatRange.End, &v) } }
// NewTransportHandle builds a new Handle which speaks to the given transport. // // If the transport is an io.Closer, it will be closed when the handle is closed. func NewTransportHandle(name string, t envelope.Transport) (Handle, error) { client := api.NewPluginClient(multiplex.NewClient( "Plugin", envelope.NewClient(_proto, t), )) handshake, err := client.Handshake(&api.HandshakeRequest{}) if err != nil { return nil, errHandshakeFailed{Name: name, Reason: err} } if handshake.Name != name { return nil, errHandshakeFailed{ Name: name, Reason: errNameMismatch{Want: name, Got: handshake.Name}, } } if handshake.APIVersion != api.APIVersion { return nil, errHandshakeFailed{ Name: name, Reason: errAPIVersionMismatch{Want: api.APIVersion, Got: handshake.APIVersion}, } } // If we got here, the API version matches so the plugin must have // provided the Version if handshake.LibraryVersion == nil { return nil, errHandshakeFailed{ Name: name, Reason: errVersionIsRequired, } } version, err := semver.Parse(*handshake.LibraryVersion) if err != nil { return nil, errHandshakeFailed{Name: name, Reason: err} } if !compatRange.Contains(version) { return nil, errHandshakeFailed{ Name: name, Reason: errVersionMismatch{ Want: compatRange, Got: *handshake.LibraryVersion, }, } } features := make(map[api.Feature]struct{}, len(handshake.Features)) for _, feature := range handshake.Features { features[feature] = struct{}{} } return &transportHandle{ name: name, Transport: t, Client: client, Running: atomic.NewBool(true), Features: features, }, nil }