Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null types #1

Closed
dafanasev1986 opened this issue May 24, 2024 · 6 comments · Fixed by #2
Closed

Null types #1

dafanasev1986 opened this issue May 24, 2024 · 6 comments · Fixed by #2

Comments

@dafanasev1986
Copy link

dafanasev1986 commented May 24, 2024

Currently codec does not support scans of possible null geometries (returns the EOF error).

As a workaround I added this piece of code, but in this case we loose the ability to have non null geometries
(although it should be restricted on the DB side anyway).

func (p binaryScanPlan) Scan(src []byte, target any) error {
        ...
	if src == nil {
		return nil
	}

If you are ok with this sollution please add it, or maybe there is a better one (e.g. separate type like NullGeometry)?

twpayne added a commit that referenced this issue May 24, 2024
@twpayne
Copy link
Owner

twpayne commented May 24, 2024

Currently codec does not support scans of possible null geometries (returns the EOF error).

I tried to duplicate this problem in #2, but as far as I can tell the current code supports converting NULL geometries to Go's nil correctly.

Could you give an example or test case that demonstrates the problem? I would be very happy to fix this.

@dafanasev1986
Copy link
Author

dafanasev1986 commented May 26, 2024

Of course, here is an example

func TestCodecDecodeNullGeometry(t *testing.T) {
	defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, tb testing.TB, conn *pgx.Conn) {
		type s struct {
			Geom geom.T `db:"geom"`
		}

		tb.Helper()
		rows, err := conn.Query(ctx, "select NULL::geometry AS geom", pgx.QueryResultFormats{pgx.BinaryFormatCode})
		assert.NoError(tb, err)

		value, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[s])
		assert.NoError(t, err)
		assert.NotZero(t, value)
		assert.Zero(t, value.Geom)
	})
}

@twpayne
Copy link
Owner

twpayne commented May 26, 2024

Thank you for the example. I don't think the test is correct. Specifically, this line:

assert.NotZero(t, value)

value is an interface type. I believe that pgx-geom is correctly setting the underlying value to nil and that this test is incorrectly testing for a nil interface. For more information, see https://groups.google.com/g/golang-nuts/c/wnH302gBa4I.

@dafanasev1986
Copy link
Author

I'm sorry, my fault, you can all remove all asserts except the one that tests for the error (assert.NoError(t, err)). The test will still fail

@twpayne
Copy link
Owner

twpayne commented May 28, 2024

Thank you for the extra information. I've added support for NULL geometries in #2 and tagged version v0.0.1 which includes the fix.

@dafanasev1986
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants