boostport / address Goto Github PK
View Code? Open in Web Editor NEWAddress is a Go library that validates and formats addresses using data generated from Google's Address Data Service
License: Apache License 2.0
Address is a Go library that validates and formats addresses using data generated from Google's Address Data Service
License: Apache License 2.0
For example you create a User in the DB and then want to record their address, but use a lookup table for Country, City.
Everyone uses different DB and DAL, so maybe we can generate a DB CSV as part of the gen process.
I use Ent, because I am lazy :) But it does work well with atlas. So from a CSV I can write a bit of code to manage that.
change "AU" to "AUfoo", and it still passes...
// NewValid creates a new Address. If the address is invalid, an error is returned.
package main
import (
"log"
"github.com/Boostport/address"
"github.com/hashicorp/go-multierror"
)
func main() {
addr, err := address.NewValid(
address.WithCountry("AUfoo"), // Must be an ISO 3166-1 country code
address.WithName("John Citizen"),
address.WithOrganization("Some Company Pty Ltd"),
address.WithStreetAddress([]string{
"525 Collins Street",
}),
address.WithLocality("Melbourne"),
address.WithAdministrativeArea("VIC"), // If the country has a pre-defined list of admin areas (like here), you must use the key and not the name
address.WithPostCode("3000"),
)
if err != nil {
// If there was an error and you want to find out which validations failed,
// type switch it as a *multierror.Error to access the list of errors
if merr, ok := err.(*multierror.Error); ok {
for _, subErr := range merr.Errors {
if subErr == address.ErrInvalidCountryCode {
log.Fatalf(subErr.Error())
}
}
}
}
// Use addr here
log.Print("log")
log.Print("Country: " + addr.Country)
log.Print("Name: " + addr.Name)
log.Print("Organization: " + addr.Organization)
log.Print("Locality: " + addr.Locality)
log.Print("DependentLocality: " + addr.DependentLocality)
log.Print("AdministrativeArea: " + addr.AdministrativeArea)
}
regexp.MatchString
used in checkPostCode
returns true
as long as the test address contains a match, but doesn't enforce an exact match.
This is causing issues for us where users are entering in extra information into the address form. E.G. NSW 3500
is accepted as a valid post code for Australia NSW, whereas the correct post code is 3500
.
A simple fix for this might be to wrap the top-level regex with ^()$
. E.g. \d{4}
would become ^(\d{4})$
.
The variable err
is never defined, and New()
doesn't return an error.
Had an issue with running go generate.
Output:
Generating code...
Formatting generated code...
Total time taken: 1m31.7646708s
constants.go:1: running "stringer": exec: "stringer": executable file not found in %PATH%
Fixed it with:
go get golang.org/x/tools/cmd/stringer
Maybe add as dependency somewhere or in readme
Hi,
I wanted to inquire about some observations I've made in generated data:
Administrative Area Keys: The keys used to identify administrative areas in China appear to be numeric within the data. However, the ISO 3166-2 codes is not numeric. Please refer to https://en.wikipedia.org/wiki/ISO_3166-2:CN.
Locality IDs and Language: The locality IDs for China's administrative areas seem to be presented in Han characters for the "en" (English) language. Shouldn't these IDs be in English language?
The current format causes inconsistencies while attempting to interact with the library for Chinese addresses. Also, using Han characters for locality IDs in the "en" language could lead to confusion for users expecting English.
Is it possible to adopt ISO 3166-2 standard for administrative area keys in China and displaying locality IDs in English for the "en" language?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.