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

Add SchemaData, SimpleArrayData, SimpleData #35

Merged
merged 5 commits into from
Nov 5, 2022

Conversation

k-mack
Copy link
Contributor

@k-mack k-mack commented Oct 28, 2022

Support kml:SchemaData, which depends on kml:SimpleArrayData and kml:SimpleData.

Closes #33, #34.

Copy link
Member

@pjsier pjsier left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! This is looking great, I had one small comment. Other than that, I had a question around typing.

Currently we're a bit inconsistent on how to handle required attributes. A lot of the style elements like LabelStyle and ListStyle require id and load it outside of the attrs HashMap, but it looks like Link has the same requirement and uses attrs.

I'm not sure I see a huge up or downside either way, but seeing the spec for this one I might lean towards setting SimpleData.name directly. What do you think?

src/reader.rs Outdated
Comment on lines 815 to 818
Ok(SimpleData {
value: self.read_str()?,
attrs,
})
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 a no-op for here now, but for consistency can we add ..Default::default() here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding this triggers clippy's needless_update warning. I can put it in anyways if you'd like.

@@ -2,7 +2,7 @@ name: CI

on:
pull_request:
types: [opened, synchronized]
types: [opened, synchronize]
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for catching that!

@k-mack
Copy link
Contributor Author

k-mack commented Oct 29, 2022

Thanks for the review! Regarding adding a struct field for required attributes makes sense. I actually started down that path initially. I then started to weigh whether there should be fields for the optional attributes like how the micromata Java KML library does.

I backed out the changes and settled on storing all attributes in a map with the mindset that if I am a user reading the spec and I see that this library exposes a map of attributes, then I know I should be able to do SimpleData.attrs.get("name").unwrap(). Although, this library doesn't do validation yet, so the unwrap isn't safe.

I agree that we should be consistent, so I will add fields for required attributes.

@k-mack
Copy link
Contributor Author

k-mack commented Nov 4, 2022

When reading required attributes, it makes sense to me that we'd want to remove them from the HashMap. If they aren't removed, then they'll be written twice. This happens only with models that can have a mixture of required and optional attributes like SimpleData. See comment in the below snippet for what I am referring to.

    fn write_simple_data(&mut self, simple_data: &SimpleData) -> Result<(), Error> {
        self.writer.write_event(Event::Start(
            BytesStart::owned_name(b"SimpleData".to_vec())
                .with_attributes(vec![("name", &*simple_data.name)])
                // the below line will write `name` again if it is not removed
                .with_attributes(self.hash_map_as_attrs(&simple_data.attrs)),
        ))?;

        self.writer.write(simple_data.value.as_bytes())?;

        Ok(self
            .writer
            .write_event(Event::End(BytesEnd::borrowed(b"SimpleData")))?)
    }

@pjsier
Copy link
Member

pjsier commented Nov 4, 2022

@k-mack good catch! I think in other examples we’ve been ignoring the other attributes, but I think it makes sense to use remove instead of get when pulling the id attribute to avoid writing duplicates

Extract required `name` field from SimpleData's attributes into a
designated field upon reading. Remove the field if present when writing
to avoid duplicate `name` attributes in the output.
Extract required `name` field from SimpleArrayData's attributes into a
designated field upon reading. Remove the field if present when writing
to avoid duplicate `name` attributes in the output.
@k-mack
Copy link
Contributor Author

k-mack commented Nov 5, 2022

I reviewed the spec for LabelStyle, ListStyle, and Link, but I found them all to treat their id attribute as optional. Am I missing something?

@k-mack k-mack requested a review from pjsier November 5, 2022 05:43
Fix new clippy warnings.
Copy link
Member

@pjsier pjsier left a comment

Choose a reason for hiding this comment

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

Thanks for these updates! And looking again at the spec you're right about the style attributes, I'll probably update that in a separate PR

@pjsier pjsier merged commit 4c048b9 into georust:main Nov 5, 2022
@pjsier pjsier mentioned this pull request Nov 5, 2022
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.

Add kml:SimpleData
2 participants