maxmind / mmdbwriter

Go library for writing MaxMind DB (mmdb) files
Apache License 2.0
116 stars 28 forks source link

How to create array in subdivisions element? #12

Closed hkelley closed 3 years ago

hkelley commented 3 years ago

I am very new to both Go and the inner workings of MaxMind DB. My goal is to merge my private IP ranges into an existing City MMDB. I have been able to insert most data elements but I have not figured out how to add subdivisions as a JSON array in Go.

This does not produce an array:

        recordData := mmdbtype.Map{
            "city": mmdbtype.Map{
                "names": mmdbtype.Map{
                    "en": mmdbtype.String(vlanLoc.City + ": " + vlanLoc.AdNet),
                },
            },
            "subdivisions": mmdbtype.Map{
                "iso_code": mmdbtype.String(vlanLoc.Province),
                "names": mmdbtype.Map{
                    "en": mmdbtype.String("subdivison data here"),
                },
            },

Putting array brackets "[]" in from of the "mmdbtype.Map" triggers this error in the compiler.

cannot use []mmdbtype.Map literal (type []mmdbtype.Map) as type mmdbtype.DataType in map value:
    []mmdbtype.Map does not implement mmdbtype.DataType (missing Copy method)

Example of the above:

        subdivisions := make([]mmdbtype.Map, 1)
        subdivisions[0] = mmdbtype.Map{
            "iso_code": mmdbtype.String(vlanLoc.Province),
            "names":    mmdbtype.Map{"en": mmdbtype.String("subdivison data here")},
        }

        recordData := mmdbtype.Map{
            "city": mmdbtype.Map{
                "names": mmdbtype.Map{
                    "en": mmdbtype.String(vlanLoc.City + ": " + vlanLoc.AdNet),
                },
            },
            "subdivisions": subdivisions,

How can this be constructed so that an array like the following is added to the MMDB?

                    "subdivisions": [
                        {
                            "geoname_id": 5128638,
                            "iso_code": "NY",
                            "names": {
                                "de": "New York",
                                "en": "New York",
                            }
                        }
                    ]
hkelley commented 3 years ago

Slices!

        recordData := mmdbtype.Map{
            "city": mmdbtype.Map{
                "names": mmdbtype.Map{
                    "en": mmdbtype.String(vlanLoc.City + ": " + vlanLoc.AdNet),
                },
            },
            "subdivisions": mmdbtype.Slice{
                mmdbtype.Map{
                    "iso_code": mmdbtype.String(vlanLoc.Province),
                    "names":    mmdbtype.Map{"en": mmdbtype.String("subdivison 1? data here")},
                },
                mmdbtype.Map{
                    "iso_code": mmdbtype.String(vlanLoc.Province),
                    "names":    mmdbtype.Map{"en": mmdbtype.String("subdivison 2? data here")},
                },
            },