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

Add the possibility to change the addressRange maxLen with options. #142

Merged
merged 1 commit into from
Mar 21, 2024

Conversation

Hurtsich
Copy link
Contributor

@Hurtsich Hurtsich commented Mar 4, 2024

Hello, we have a national service provider that use a custom protocol based on SMPP.
We need to have the possibility to set the max length for the address range.

I made the change using the option pattern (like the change on the connector to inject the address range)
This way we open the lib but do not break the public interface.

@Hurtsich Hurtsich force-pushed the master branch 2 times, most recently from 4c5acde to 17e4700 Compare March 4, 2024 16:04
@laduchesneau
Copy link
Collaborator

laduchesneau commented Mar 4, 2024

Hi, I haven't looked over the code yet, but if I may propose another solution to this problem....lets make all Address fields exportalable. We don't need mutators and default TON/NPI will both be 0 anyway.

I would delete it all and just add a validator function

// Address smpp address of src and dst.
type Address struct {
	Ton     byte
	Npi     byte
	Addr string
}

// isValid verifies the Address, return a error if not valid
func (c *Address) IsValid() error{
	if len(c.Addr) > data.SM_ADDR_LEN {
		return fmt.Errorf("address len exceed limit. (%d > %d)", len(c.Addr), data.SM_ADDR_LEN)
	}
	return nil
}

Unless there is a specific reason we need getter and setter, that I am not seeing.

We would still keeps the marshal, unmarshal and string functions

This would be a breaking change, but a good one. If this solution is accepted, I would also recommend the same change for AddressRange.

@linxGnu
Copy link
Owner

linxGnu commented Mar 5, 2024

@Hurtsich
Simplest solutions:

  • Solution 1: Exposing AddressRange field
  • Solution 2: implement function like this:
func (c * AddressRange) ForceSetAddressRange(addr string){
     c.addressRange = addr
}

Just it if you want to have custom addrRange without validation.

@laduchesneau
Copy link
Collaborator

I know we are talking about AddressRange, but both AddressRange and Address have the same issue. If we "fix" one, the other should also be "fixed".

  • Solution 1: Exposing AddressRange field

  • Solution 2: implement function like this:

I do love the simplicity of solution 2, but it doesn't really resolve the root cause of the problem.

With exported fields, all these functions are not needed.

func NewAddress() Address {}
func NewAddressWithAddr(addr string) (a Address, err error) {}
func NewAddressWithTonNpi(ton, npi byte) Address {}
func NewAddressWithTonNpiAddr(ton, npi byte, addr string) (a Address, err error) {}
func (c *Address) SetAddress(addr string) (err error) {}
func (c *Address) SetTon(ton byte) {}
func (c *Address) SetNpi(npi byte) {}
func (c *Address) Address() string {}
func (c *Address) Ton() byte {}
func (c *Address) Npi() byte {}

func NewAddressRange() AddressRange {}
func NewAddressRangeWithAddr(addr string) (a AddressRange, err error) {}
func NewAddressRangeWithTonNpi(ton, npi byte) AddressRange {}
func NewAddressRangeWithTonNpiAddr(ton, npi byte, addr string) (a AddressRange, err error) {}
func (c *AddressRange) SetAddressRange(addr string) (err error) {}
func (c *AddressRange) SetTon(ton byte) {}
func (c *AddressRange) SetNpi(npi byte) {}
func (c *AddressRange) AddressRange() string {}
func (c *AddressRange) Ton() byte {}
func (c *AddressRange) Npi() byte {}

Dont take this the wrong way....but I think of Java when I look at this code. :)

@linxGnu
Copy link
Owner

linxGnu commented Mar 5, 2024

@laduchesneau
yeah, I understand your concern. I think best practice here is: only "open" if needed/on user request.
In this way, we balanced between maintenance effort and user need.

@Hurtsich
Copy link
Contributor Author

Hurtsich commented Mar 5, 2024

@linxGnu If you're OK with breaking the public interface, i think just exposing the fields on AddressRange is fine.

@Hurtsich Hurtsich force-pushed the master branch 2 times, most recently from 2bd852d to e1b42c8 Compare March 11, 2024 15:10
@Hurtsich
Copy link
Contributor Author

Ping, I updated the PR with the changes that we discussed. I'll let you merge if it's okay :)

Copy link
Owner

@linxGnu linxGnu left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you very much.

@linxGnu linxGnu merged commit 1d156d8 into linxGnu:master Mar 21, 2024
2 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.

3 participants