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

feat(geo): Store subdivisions #2058

Merged
merged 11 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions relay-general/src/protocol/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub struct Geo {
#[metastructure(pii = "true", max_chars = "summary")]
pub city: Annotated<String>,

/// Human readable subdivision name.
#[metastructure(pii = "true", max_chars = "summary")]
pub subdivisions: Annotated<String>,

/// Human readable region name or code.
#[metastructure(pii = "true", max_chars = "summary")]
pub region: Annotated<String>,
Expand Down Expand Up @@ -96,6 +100,7 @@ mod tests {
let geo = Annotated::new(Geo {
country_code: Annotated::new("US".to_string()),
city: Annotated::new("San Francisco".to_string()),
subdivisions: Annotated::new("California".to_string()),
region: Annotated::new("CA".to_string()),
other: {
let mut map = Map::new();
Expand All @@ -117,6 +122,7 @@ mod tests {
let geo = Annotated::new(Geo {
country_code: Annotated::empty(),
city: Annotated::empty(),
subdivisions: Annotated::empty(),
region: Annotated::empty(),
other: Object::default(),
});
Expand Down
5 changes: 5 additions & 0 deletions relay-general/src/store/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ impl GeoIpLookup {
.as_ref()
.and_then(|city| Some(city.names.as_ref()?.get("en")?.to_string())),
),
subdivisions: Annotated::from(
city.subdivisions
.as_ref()
.and_then(|subdivisions| Some(subdivisions.names.as_ref()?.get("en")?.to_string())),
),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is failing because subdivisions is a list like this one:

"subdivisions": [
    {
      "geoname_id": 5332921,
      "iso_code": "CA",
      "names": {
        "de": "Kalifornien",
        "en": "California",
        "es": "California",
        "fr": "Californie",
        "ja": "カリフォルニア",
        "ru": "Калифорния",
        "zh-CN": "加州"
      }
    }
  ]

but I'm not too sure how to grab first element in Rust

Copy link
Member

Choose a reason for hiding this comment

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

something like

city.subdivisions.as_ref().and_then(|subdivisions| {
            subdivisions.get(0).and_then(|subdivision| {
                subdivision.names.as_ref().and_then(|subdivision_names| {
                    subdivision_names
                        .get("en")
                        .cloned()
                        .map(|subdivision_name| subdivision_name.to_string())
                })
            })
        });

i think?
i do wonder how often the multi subdivision case comes up and if its worth storing an array of them instead

Copy link
Member

Choose a reason for hiding this comment

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

it's probably fine just to take the first (top level as i understand it) subdivision.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

based on this example it looks like it's always a list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was gonna grab the first element but rust wasn't happy about how I did it. I'll try your suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also changed it subdivisions -> subdivision

region: Annotated::from(
city.country
.as_ref()
Expand Down
1 change: 1 addition & 0 deletions relay-general/src/store/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,7 @@ mod tests {
let expected = Annotated::new(Geo {
country_code: Annotated::new("GB".to_string()),
city: Annotated::new("Boxford".to_string()),
subdivisions: Annotated::new("Sudbury".to_string()),
region: Annotated::new("United Kingdom".to_string()),
..Geo::default()
});
Expand Down