Skip to content

Conversation

@rboyer
Copy link
Member

@rboyer rboyer commented Mar 24, 2022

Fixes #1

@rboyer rboyer self-assigned this Mar 24, 2022
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")
Copy link
Member Author

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
Copy link
Member Author

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
Copy link
Member Author

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 {
Copy link
Member Author

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: "_"}},
Copy link
Member Author

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.

Copy link
Member Author

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)
Copy link
Member Author

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(),
Copy link
Member Author

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 |
Copy link
Member Author

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 {
Copy link
Member Author

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)
Copy link
Member Author

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

Copy link

@dhiaayachi dhiaayachi left a 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.

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.

special case time and duration conversions

2 participants