riferrei / srclient

Golang Client for Schema Registry
Apache License 2.0
236 stars 70 forks source link

srclient.Schema fields unexported without constructor - makes SchemaRegistryClient unmockable #51

Closed ideasculptor closed 2 years ago

ideasculptor commented 3 years ago

All members of srclient.Schema are unexported, so it is impossible to populate an instance of srclient.Schema with values. When implementing a mock SchemaRegistryClient, I want to specify the schema instance that should be returned by GetLatestSchema, but I am unable to without resorting to obnoxious reflection tricks.

It's all well and good to create an immutable struct, but how about providing a constructor or a builder of some kind so that it is at least possible to create one for testing. I am reluctant to use the provided MockSchemaRegistryClient because that mock has far too much logic in it, which might include bugs of its own. A mock should be super simple and just allow for the injection of return values for a given method call. And it is easy enough to create such a mock, except that the return value I need to inject cannot be populated because it is immutable and lacks a constructor.

This kind of thing shouldn't be necessary, and wouldn't be if there was a constructor function

    suite.schema = &srclient.Schema{}
    SetUnexportedField(reflect.ValueOf(suite.schema).Elem().FieldByName("id"), 1)
    SetUnexportedField(reflect.ValueOf(suite.schema).Elem().FieldByName("schema"), "MyDomainEvent")
    SetUnexportedField(reflect.ValueOf(suite.schema).Elem().FieldByName("version"), 1)

    suite.srclient = new(MockRegistryClient)
    suite.srclient.On("GetLatestSchema", "MyDomainEvent", false).Return(suite.schema, nil)
AtakanColak commented 3 years ago

Would this function satisfy your testing requirements?

func NewSchema(id int, schema string, version int, references []Reference, codec *goavro.Codec) Schema
ideasculptor commented 3 years ago

Most definitely. That's exactly what I'm looking for

AbdulRahmanAlHamali commented 2 years ago

Hello, I notice that this hasn't made it yet to the official release. Any reason for that?

AtakanColak commented 2 years ago

Just an oversight, thank you, v0.5 is out now.