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

geojson: handle extra/foreign members in featureCollection #56

Merged
merged 6 commits into from
Jan 16, 2021

Conversation

paulmach
Copy link
Owner

@paulmach paulmach commented Jan 7, 2021

Foreign Members are those that aren’t in the spec. i.e. properties in the feature collection object that aren’t the big 3, type,bbox and features.

Support for this has been requested 3 times. #1 #13 #42

This PR adds full support to those features by storing them in a ExtraMembers map in the feature collection. These members will be encoded/decoded to/from the base feature collection as expected.

Concerns

There was an example and issue response suggesting to embed a feature collection in another object like so

type MyFeatureCollection struct {
	geojson.FeatureCollection
	Title string `json:"title"`
}

This approach will no longer work and has been abandoned because marshaling/encoding was not supported. To maintain this behavior one can add a UnmarshalJSON() method on the new struct type like so:

func (fc *MyFeatureCollection) UnmarshalJSON(data []byte) error {
	err := json.Unmarshal(data, &fc.FeatureCollection)
	if err != nil {
		return err
	}

	fc.Title = fc.ExtraMembers.MustString("title", "")
	fc.ExtraMembers = nil
	return nil
}

Note: you could still do this to keep things type safe but the reverse MarshalJSON would also need to be implemented for encoding to work.

Performance

Benchmarks did no show any meaningful change in performance after this change.

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.

1 participant