-
Notifications
You must be signed in to change notification settings - Fork 4
automatically convert between common protobuf time/durations #6
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
base: main
Are you sure you want to change the base?
Conversation
| flags.StringVar(&opts.source, "source", ".", "package path for source structs") | ||
|
|
||
| // TODO: make this a positional arg, set a Usage func to document it | ||
| flags.StringVar(&opts.moduleWorkDir, "modworkdir", "", "module working directory to run from") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed this so the E2E test could use it after converting internal/e2e into a submodule.
| // S4 Workload // for testing struct-to-ptr for slices | ||
| // T1 timestamp.Timestamp // for testing struct-to-struct for time | ||
| // T2 *timestamp.Timestamp // for testing ptr-to-ptr for time | ||
| T3 *timestamp.Timestamp // for testing ptr-to-struct for time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only handling pointer-protobufs to non-pointer-stdlib conversions for now, which is the most common.
| // S4 *Workload // for testing struct-to-ptr for slices | ||
| // T1 time.Time // for testing struct-to-struct for time | ||
| // T2 *time.Time // for testing ptr-to-ptr for time | ||
| T3 time.Time // for testing ptr-to-struct for time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the target side of the conversion.
| } | ||
| } | ||
|
|
||
| func astAssignMulti(left, right []ast.Expr) ast.Stmt { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variation of astAssign that works for generating foo, bar := gir, zim instead of just foo := gir
| case special.ProtobufDuration && special.ProtobufDirection == DirFrom: | ||
| // pointer-to-struct | ||
| return newAssignMultiStmtUserFunc( | ||
| []ast.Expr{left, &ast.Ident{Name: "_"}}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We specifically ignore the error here by assigning to the blank identifier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also in 2 more functions below.
| rawKind, ok := computeAssignment(field.Type(), sourceField.SourceType) | ||
| if !ok { | ||
| assignErrFn(nil) | ||
| rawKind, err := computeAssignment(field.Type(), sourceField.SourceType, imports) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really should've had this return an error instead of a boolean the first time, so i fixed that since i found it useful to return a real error in the case where you are using protobufs but not in the exact way this PR handles.
| sourceField.ConvertFuncName(DirFrom), | ||
| kind.Direct, | ||
| kind.Convert, | ||
| kind.Special.ReverseDirection(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We always generate both the TO and FROM functions at the same time, so when we apply the protobuf conversion in reverse we have to flip the direction so the function we use switches as well.
| packages.NeedFiles | | ||
| packages.NeedCompiledGoFiles | | ||
| packages.NeedImports | | ||
| packages.NeedModule | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: I was going to figure out how to use this attribute to determine protobuf versions so the generated code could freely switch to different conversion functions if needed.
| return false | ||
| } | ||
|
|
||
| func isAnyProtobufTimeOrDuration(types ...types.Type) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are the key functions here. They infer if we should be trying the special casing of protobuf time/duration conversions.
| t.M8[k] = y | ||
| } | ||
| } | ||
| t.T3, _ = ptypes.Timestamp(s.T3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
examples of the generated code in action
dhiaayachi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will be merged soon, please re-request a review if the status of this change.
Fixes #1