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 a "duration" string format #257

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

ptodev
Copy link

@ptodev ptodev commented Aug 5, 2024

I've been trying to schematise the config of an application which uses time.Duration. Ideally, I'd like the generated Go code to use time.Duration.

This PR is still at a very early stage. Please feel free to let me know if you approve the overall direction in which the work is going. Any help would be much appreciated! 😊

@omissis
Copy link
Owner

omissis commented Sep 16, 2024

hey @ptodev the idea looks promising, are you still interested in pushing this one further?

@ptodev ptodev force-pushed the duration branch 2 times, most recently from 5f4c018 to b385088 Compare September 19, 2024 13:07
@ptodev
Copy link
Author

ptodev commented Sep 19, 2024

Hi, @omissis! I'd be very happy to address any feedback you have.
Thank you so much for taking a look!

I believe the code mostly generates what it needs to, but it's still WIP due to the TODOs.

@omissis
Copy link
Owner

omissis commented Nov 16, 2024

nothing against this one, so feel free to finish it and set it to "Ready for review" once all the todos and required fixes are done! thank you! 💪

_, ok := v.defaultValueType.(codegen.DurationType)
if v.defaultValueType != nil && ok {
defaultDurationISO8601, ok := v.defaultValue.(string)
if !ok {
Copy link
Author

Choose a reason for hiding this comment

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

@omissis Do you think it is ok to leave these panic statements here for now? It looks like the current code doesn't really have a way to guard aganst such invalid defaults, and so I'm not sure how to propagate the errors. For example, this schema:

    "myNumber": {
      "type": "number",
      "default": "asdf"
    },

... produces code like this:

type Primitives struct {
  ...
  MyNumber float64 `json:"myNumber,omitempty" yaml:"myNumber,omitempty" mapstructure:"myNumber,omitempty"`
  ...
}

func (j *Primitives) UnmarshalJSON(b []byte) error {
  ...
  if v, ok := raw["myNumber"]; !ok || v == nil {
    plain.MyNumber = "asdf"
  }
}

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.

2 participants