jackc / pgx

PostgreSQL driver and toolkit for Go
MIT License
10.4k stars 825 forks source link

Provide method to retrieve parameterStatuses keys from #1814

Open devhawk opened 10 months ago

devhawk commented 10 months ago

PgConn struct has a private parameterStatuses map field. The PgConn.ParameterStatus method returns the value of a specified key from that map. As far as I can tell, there is no mechanism to retrieve the set of keys in the map.

PgConn should expose either a clone of the parameterStatuses field (generated via maps.Clone maybe?) or an array of the parameterStatuses keys that the caller can then use to call the existing ParameterStatus with.

jackc commented 9 months ago

I agree this would be useful. This might be part of #1803. Otherwise, a new method could be added.

I see 3 options for that method.

  1. Return the parameter statuses map directly. This means we can't prevent the caller from mutating it. But it is fairly common in Go to document that a return value must not be changed.
  2. Clone the map.
  3. Return an array of keys.

I kind of lean toward the first option as it avoids any allocations.

devhawk commented 9 months ago

Call back based iterator would also work (https://blog.kowalczyk.info/article/1Bkr/3-ways-to-iterate-in-go.html)

jackc commented 9 months ago

Yeah, an iterator would be possible. Though maybe overkill. Hmm... actually if that was done it would be best to use the new https://github.com/golang/go/issues/61405 Go iteration construct (though that would mean delaying the feature for at least a year).

Though I think I still lean toward exposing the map directly.