Skip to content
This repository has been archived by the owner on Jun 14, 2022. It is now read-only.

Add enum support via interface #24

Closed
wants to merge 1 commit into from

Conversation

wader
Copy link

@wader wader commented Jul 27, 2018

Hello, would this a valid way of adding more flexible enum support? my use case is various enum types that already have string<->int translation maps so JSONSchemaEnums() would just be a range loop to collect strings and return them.

Some questions and concerns:

Weird or confusing to create a values from the type and call code when "reflecting"? maybe only support value receiver method?

Support more then one level of pointers?

Support enum type? spec seem to say the type is optional for enum.

TODO:
Document

Related to #23

If a type implements JSONSchemaEnums() it will be assumed to be a
enum and be called to get enum values.
@wader
Copy link
Author

wader commented Aug 3, 2018

@alecthomas hi, any opinion about this?

@@ -68,6 +80,11 @@ type TestUser struct {
Feeling ProtoEnum `json:"feeling,omitempty"`
Age int `json:"age" jsonschema:"minimum=18,maximum=120,exclusiveMaximum=true,exclusiveMinimum=true"`
Email string `json:"email" jsonschema:"format=email"`

TestEnumVal ABCEnumVal `json:"test_enum_val,omitempty"`
Copy link
Owner

Choose a reason for hiding this comment

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

I don't mind this approach, but I'm still not sure why not just support a jsonschema:"enum=a,b,c" tag parameter?

Copy link
Author

Choose a reason for hiding this comment

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

In my case code duplication. I already have lists of enums to handle JSON marshall/unmarshalling for various types so a approach like the removes the need to keep the struct tags in sync.

@wolfeidau
Copy link
Sponsor

Any movement on this PR?

Would really like to avoid using a fork ;)

Given this is a pretty minimal library I would prefer the tag parameter option.

@wader
Copy link
Author

wader commented Jan 28, 2019

@wolfeidau Hi, im not actively working on this. Maybe doing it using tag parameter option should be separate PR?

@alecthomas alecthomas closed this Apr 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants