Skip to content

Geo Package First Take#2

Merged
zhongshixi merged 51 commits intomasterfrom
maximind-geo
Oct 10, 2023
Merged

Geo Package First Take#2
zhongshixi merged 51 commits intomasterfrom
maximind-geo

Conversation

@zhongshixi
Copy link
Copy Markdown
Collaborator

@zhongshixi zhongshixi commented Oct 2, 2023

Change Set

  1. first take on geo package, including all functionalities and checks

@zhongshixi
Copy link
Copy Markdown
Collaborator Author

Change Set

  1. first take on geo package, including all functionalities , unit test and CI using dagger

Comment thread README.md Outdated
t.Fatal(fmt.Errorf("embedFS.Open: %w", err))
}

maximind, err := NewMaxMindReader(file)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

should it be something like geo.NewMaxMindReader?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

it should, i will add it

Comment thread maximind.go
Comment on lines +64 to +71
func (m *Reader) IsTestDB() bool {
desc, ok := m.Reader.Metadata().Description["en"]
if !ok {
return false
}

return strings.Contains(desc, "Test Database")
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

why do we need to test if db is test DB?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

oh, got it. We use it for testing, to parse specific IPs.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

it is a sanity check for consumer , so they know if they are using test DB for production or not

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When/How a test db could be initialized versus a regular db?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: imho, whoever is calling the reader should know if it's a test environment or not. There's no benefit for the reader to know that information itself.

Comment thread maxmind_test.go
var embedFS embed.FS

const (
maxmindCountryTestDB string = "test/MaxMind-DB/test-data/GeoIP2-Country-Test.mmdb" // test database, only covers NA, EU, AS continents
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

oh, this is just a link. We don't keep actual binary. Cool!

Copy link
Copy Markdown
Collaborator Author

@zhongshixi zhongshixi Oct 6, 2023

Choose a reason for hiding this comment

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

this is a submodule link, the repo still keeps the test db binary, however when you install the package, you are not going to get db binary as part of the golang library.

Comment thread country.go Outdated

// CountryAlpha3Code - return 3-letter ISO code for country
func (c Country) CountryAlpha3Code() string {
if c.isEmpty() || len(c.Country.IsoCode) != 2 {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

we can create a new func
e.g.

func (c Country) isCountryAlpha2Code() bool {
    return c.isEmpty() || len(c.Country.IsoCode) != 2
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

nice! but i think the better name i think is isCountryAlpha2CodeValid()

Comment thread .circleci/config.yml
version: 2.1

orbs:
codecov: codecov/codecov@3.2.5
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we need the codecov and git orbs?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

we do

  1. codecov is a nice-to-have, i want the data to be reflected as badge
  2. git is nice-to-have since it uses submodule, without git orb, i have to write my own submodule sync, just a simple helper

Comment thread country.go Outdated
"github.com/oschwald/geoip2-golang"
)

// Country - a type definition on geoip2.Country data struct while providing heloer functions to retrieve certain data in convinent way and additional country data maxmind db does not provide
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: providing heloer -> providing helper

Comment thread .circleci/config.yml
jobs:
unit-test:
docker:
- image: cimg/go:1.21.1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Where is the cimg/go coming from?
Can we switch to chainguard if we need to use a public image? https://edu.chainguard.dev/chainguard/chainguard-images/reference/go/

Copy link
Copy Markdown
Collaborator Author

@zhongshixi zhongshixi Oct 6, 2023

Choose a reason for hiding this comment

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

  1. cimage is the circle ci's golang image

  2. any specific reason you want to use chainguard image? any major benefit?

Copy link
Copy Markdown
Collaborator Author

@zhongshixi zhongshixi Oct 6, 2023

Choose a reason for hiding this comment

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

@diogogmt ok, this is interesting
https://edu.chainguard.dev/chainguard/chainguard-images/vuln-comparison/go/

i like this - however, there is a few things to consider

  1. this image does not have built-in curl , i have to install it
  2. i do not see how security is going to impact this package, we are not building an app container, it is just there to run dagger
  3. cimage so far is the most convenient image so i do not have add extra steps to install tool chains to make things work

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fair enough. If it's the official image by CircleCI with some helper already installed sounds good to me 👍

Comment thread .circleci/config.yml
resource_class: small
steps:
- checkout
- git/checkout-with-submodules
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why with submodules?

Copy link
Copy Markdown
Collaborator Author

@zhongshixi zhongshixi Oct 6, 2023

Choose a reason for hiding this comment

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

as specified in presentation, without submodules, you can not use the test db file provided by MaxMind which is required in unit testing

https://github.com/maxmind/MaxMind-DB

I try to avoid copy-and-paste and if more feature is added to the repo, i can easily grab different test db for unit testing since i have a submodule for all of its test db

Comment thread .circleci/config.yml
command: cd /usr/local && { curl -L https://dl.dagger.io/dagger/install.sh | sudo sh; cd -; }
- run:
name: unit test
command: dagger run --progress plain go run ci/main.go --cov_file cov.out
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🏅

Comment thread .gitmodules
@@ -0,0 +1,3 @@
[submodule "test/MaxMind-DB"]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why do we need a submodule?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Comment thread README.md
@@ -1,2 +1,83 @@
# geo
provide a simple interface to provide geo-related data, support maxmind db only at the moment
# Geo Information Library For Go
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well done with the README! 👍

Comment thread country.go Outdated
"github.com/oschwald/geoip2-golang"
)

// Country - a type definition on geoip2.Country data struct while providing heloer functions to retrieve certain data in convinent way and additional country data maxmind db does not provide
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: can you run the comments through a spell checker? eg; heloer -> helper

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@diogogmt do you have a tool for this? my spell checker does not work on ReadMe

Copy link
Copy Markdown
Collaborator Author

@zhongshixi zhongshixi Oct 6, 2023

Choose a reason for hiding this comment

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

nvm, i found a vs code plugin, have run through a check, fixed the wording issue

Comment thread country.go
)

// Country - a type definition on geoip2.Country data struct while providing heloer functions to retrieve certain data in convinent way and additional country data maxmind db does not provide
// user is expected not to directly modify it.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If the user shouldn't modify it, why is it public instead of private?

Copy link
Copy Markdown
Collaborator Author

@zhongshixi zhongshixi Oct 6, 2023

Choose a reason for hiding this comment

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

because the design is - users still can access its internal data if they have more use case, and since the underlying structure is referred to a 3rd party package, it is not quite impossible to do so.

even you make it type country , you can still modify its internal, it is just user can not create a default country for their own

i will change it type country, since i do not expect user to create an empty Country struct

Comment thread maximind.go
}

func NewMaxMind(b []byte) (*Reader, error) {
reader, err := geoip2.FromBytes(b)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This block of code overlaps with https://github.com/blockthrough/geo/pull/2/files#diff-d607e19697d2bc59fe4217b2de67b083970a90c4ac563ca72891bd13c4c052baR24

suggestion: reference a private function to read the bytes from geoip2.

Comment thread maximind.go Outdated
return time.Unix(int64(m.Metadata().BuildEpoch), 0)
}

// Version - the opinionated semanic version for the underlying MaxMind DB in the format of v<major_version>.<minor_version>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

semanic -> semantic

@zhongshixi zhongshixi merged commit f910162 into master Oct 10, 2023
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.

5 participants