Skip to content

feat: add support for encoding into URL query parameters#40

Merged
quagmt merged 2 commits intoquagmt:masterfrom
mdawar:querystring
Jun 19, 2025
Merged

feat: add support for encoding into URL query parameters#40
quagmt merged 2 commits intoquagmt:masterfrom
mdawar:querystring

Conversation

@mdawar
Copy link
Contributor

@mdawar mdawar commented Apr 22, 2025

This pull request adds support for encoding the Decimal type into a URL query parameter using the go-querystring package.

go-querystring is designed to assist in scenarios where you want to construct a URL using a struct that represents the URL query parameters.

It adds an implementation for the Encoder interface:

type Encoder interface {
	EncodeValues(key string, v *url.Values) error
}

Example usage:

package main

import (
	"fmt"
	"log"

	"github.com/google/go-querystring/query"
	"github.com/quagmt/udecimal"
)

type Params struct {
	Price    udecimal.Decimal `url:"price"`
	Quantity udecimal.Decimal `url:"quantity"`
	Zero     udecimal.Decimal `url:"zero,omitempty"`
}

func main() {
	params := Params{
		Price:    udecimal.MustParse("100"),
		Quantity: udecimal.MustParse("10"),
	}

	v, err := query.Values(params)
	if err != nil {
		log.Fatal(err)
	}

	// Output without this implementation: ""
	// Output after this implementation  : "price=100&quantity=10"
	// Output without omitempty          : "price=100&quantity=10&zero=0"
	fmt.Printf("%q", v.Encode())
}

Maybe this is a niche use case, but it doesn't add any overhead to the package.

@codecov
Copy link

codecov bot commented Apr 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.41%. Comparing base (fe453e1) to head (c1fd1a5).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #40   +/-   ##
=======================================
  Coverage   96.40%   96.41%           
=======================================
  Files           5        5           
  Lines        1921     1924    +3     
=======================================
+ Hits         1852     1855    +3     
  Misses         46       46           
  Partials       23       23           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@quagmt
Copy link
Owner

quagmt commented Apr 24, 2025

@mdawar Thanks for PR. This seems okay to me as long as it doesn't add external dependency. However, I'm thinking it might bloat the library if we implement non-std interfaces because there's a lot of them

@quagmt quagmt requested a review from Copilot April 24, 2025 12:49
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for encoding the Decimal type into URL query parameters by implementing the query.Encoder interface from the go-querystring package.

  • Added the EncodeValues method for Decimal in codec.go.
  • Added a new set of tests in codec_test.go to verify encoding consistency.
  • Updated the .golangci.yaml configuration for updated linting requirements.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
codec.go Implements the EncodeValues method for Decimal to support URL query parameter encoding.
codec_test.go Introduces TestEncodeValues to validate that Decimal values are correctly encoded.
.golangci.yaml Updates configuration settings to align with new linting standards.
Comments suppressed due to low confidence (1)

codec_test.go:416

  • Consider adding test cases to verify the omitempty behavior by checking that a Decimal field with a zero value is omitted from the URL query parameters when tagged with omitempty.
func TestEncodeValues(t *testing.T) {

@mdawar
Copy link
Contributor Author

mdawar commented Apr 24, 2025

@quagmt Thank you for reviewing the PR.

Yes this is just an interface implementation and there are no external dependencies.

You're totally right about the third party interfaces, there are a lot of them for sure.

What motivated this addition is that decimal values are commonly used in financial apps (especially in crypto) where signing URL encoded query parameters is typical. For example, here’s a reference from Binance:
https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/request-security#signed-request-example-hmac

This isn’t specific to Binance, many exchange APIs use similar schemes. So while not essential, this feature is useful in practice.

That said, I’m already using a fork with this change, so feel free to close the PR if it doesn't fit your vision for the library.

Thanks again.

@quagmt
Copy link
Owner

quagmt commented Apr 25, 2025

@mdawar I'll keep this PR open for a while to see if someone have the same issue, then I think we can consider further

@michaelwilner
Copy link

@quagmt I have a use case that would benefit from this addition

@quagmt
Copy link
Owner

quagmt commented Jun 19, 2025

@mdawar It seems there’s demand for this feature. Can you add test cases for both omitempty and the new omitzero introduced in Go 1.24 to verify their behaviors are consistent?

@mdawar
Copy link
Contributor Author

mdawar commented Jun 19, 2025

@quagmt are you sure about the tests for omitempty and omitzero?
This is the responsibility of the go-querystring package where this behavior is defined and tested. Also we need to add it as a dependency if we want to add the tests.

The current implementation does not need more than the current test where we only check that the url.Values map is populated when the EncodeValues method is called.

@quagmt
Copy link
Owner

quagmt commented Jun 19, 2025

@quagmt are you sure about the tests for omitempty and omitzero? This is the responsibility of the go-querystring package where this behavior is defined and tested. Also we need to add it as a dependency if we want to add the tests.

The current implementation does not need more than the current test where we only check that the url.Values map is populated when the EncodeValues method is called.
hmm, yea I agree with you. This shouldn't be the responsibility of this library. LGTM. I'll merge the change

@quagmt quagmt merged commit 3259bfe into quagmt:master Jun 19, 2025
9 checks passed
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 this pull request may close these issues.

4 participants