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

Expose opened database type #31

Open
ykhrustalev opened this issue Mar 16, 2018 · 2 comments · May be fixed by #32
Open

Expose opened database type #31

ykhrustalev opened this issue Mar 16, 2018 · 2 comments · May be fixed by #32

Comments

@ykhrustalev
Copy link

It would be great to expose opened database type available via GeoIP.databaseType https://github.com/maxmind/geoip-api-c/blob/master/libGeoIP/GeoIP.h#L65.

Actual need lies in the fact that underline C library produces warning message to stdout in case API method is not supported on a given database type.

In my particular case I have a code which is intended to work with either country or city databases (yes, still legacy, there are reasons for that) and it tries to derive country code via all possible APIs. It is not quite straight forward to get the database type using the provided APIs.

Here is a snippet for country code

package main

import (
	"github.com/abh/geoip"
	"log"
	"path/filepath"
)

func main() {
	TestDb("GeoIPCity.dat")
	TestDb("GeoIP.dat")
}

func TestDb(path string) {
	ip := "109.106.142.207" // RU

	log.Printf("====")
	db, err := geoip.Open(path)
	if err != nil {
		log.Fatal(err.Error())
	}

	if db == nil {
		log.Fatal("nil pointer geo database")
	}

	// GeoIPCity.dat -> GetRecord
	// GeoIP.dat -> GetCountry

	log.Println("GetRecord before")
	record := db.GetRecord(ip)
	if record != nil {
		log.Printf("code = %s", record.CountryCode)
	}
	log.Println("GetRecord after")

	log.Println("GetCountry before")
	code, _ := db.GetCountry(ip)
	log.Printf("code = %s", code)
	log.Println("GetCountry after")
}

Possible workaround could be using reflection.

import (
  "reflect"
  "log"
)

func FindDatabaseType(db *geoip.GeoIP) {
	t := reflect.ValueOf(*db)
	val := t.FieldByName("db")
	dbType := val.Elem().FieldByName("databaseType")
	log.Printf("databaseType = %d", dbType.Int())
}

But making it explicit will be a more simple approach.

Pretty similar problem was addressed here syslog-ng/syslog-ng#1446

I am happy to submit PR for that.

@ykhrustalev ykhrustalev changed the title Expose version of opened database type Expose opened database type Mar 16, 2018
@ykhrustalev
Copy link
Author

looks like there is a PR with relevant change already horgh@a300901

@abh
Copy link
Owner

abh commented Mar 16, 2018 via email

@ykhrustalev ykhrustalev linked a pull request Mar 16, 2018 that will close this issue
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