[GODRIVER-2812] Make "WriteConcern" struct parameterized to accept either int or string for W Created: 14/Apr/23  Updated: 30/Aug/23  Resolved: 01/May/23

Status: Closed
Project: Go Driver
Component/s: Read and Write Concern
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Unknown
Reporter: Matt Dale Assignee: Unassigned
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-2685 Simplify "writeconcern" API Closed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

When GODRIVER-2685 is complete, the W field in the writeconcern.WriteConcern struct will be exported and visible/settable by anyone. While that simplifies the writeconcern package API, the field can be either a string or an int, so it has to be type interface{}, which can be confusing to users because it doesn't specify what type is required (any value can be set, but some values will result in BSON marshal errors).

For Go Driver 2.0, we can use generics, so make WriteConcern parameterized so we can limit the types usable for W to int or string.

E.g.

type WriteConcern[T string | int] struct {
	W T
	// ...

Definition of done:

  • The W field on a WriteConcern can only be a string or an int.

Note that users will have to update any WriteConcern literal declarations to include the type parameter. E.g.

&writeconcern.WriteConcern[int]{W: 2}



 Comments   
Comment by Matt Dale [ 01/May/23 ]

This is actually not practically with the current options API. Go methods cannot have type constraints, only functions and structs can. As a result, an options setter method like SetWriteConcern can't accept a generic struct unless the receiver (i.e. the ClientOptions) also has type parameters. For example, to set a write concern w:1 on a Client, you'd need to specify the parameter type on the ClientOptions struct as well as the WriteConcern struct:

wc := writeconcern.WriteConcern[int]{W: 1}
options.Client[int]().SetWriteConcern(wc)

That API is extremely non-intuitive, so we shouldn't do that.

Instead, we should either just leave it empty interface or define a type that can only be a string or an int.

Generated at Thu Feb 08 08:39:25 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.