-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
feat: add support to MatchV2 structs generated by proto-gen-go #262
base: 2.x.x
Are you sure you want to change the base?
Conversation
e1c139d
to
0eb0a27
Compare
fyi - this is more of a wip/prototype at this point as i'm trying to get this to work with protojson structs. |
Thanks for this Haz. Let's have a think about if it makes sense to bake any protobuf specific items into the main package, of it is best left as an extension in a separate package. It feels like a slippery slope for Pact Go to "know" about all kinds of other protocols, which I think is not idealy. |
0eb0a27
to
e895411
Compare
yep - i agree. i tried to rework this in a different way. i'm curious as how to do the extension. feel free to tell me what you'd like and i can get it done if you're interested in supporting this extension point the use case on my end is we have client generated code from proto files. while writing the pact definitions i cannot reuse any of the pb.go structs and duplicate a lot of unnecessary structure. so i'd like to add an extension to support my case and only need to tap into the the main parts of a protojson struct to consider:
|
matchers/matcher.go
Outdated
} | ||
} | ||
|
||
type MatchStruct struct { |
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.
added as a type to support injecting my own func
} | ||
type FieldStrategyFunc func(field reflect.StructField) FieldMatchArgs | ||
|
||
var DefaultFieldStrategyFunc = func(field reflect.StructField) FieldMatchArgs { |
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.
extracted your original code as a default strategy and exported so i can fall back on this when needed
matchers/matcher.go
Outdated
} | ||
return FieldMatchArgs{fieldName, field.Type, pluckParams(field.Type, field.Tag.Get("pact"))} | ||
} | ||
|
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.
my example custom code to inject
matchers/matcher.go
Outdated
} | ||
|
||
func MatchV2WithProtoJsonStrategy(src interface{}) Matcher { | ||
m := &MatchStruct{fieldStrategyFunc: ProtoJsonFieldStrategyFunc} |
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 could extract this as parameter to inject from my code
result[strings.Split(field.Tag.Get("json"), ",")[0]] = match(field.Type, pluckParams(field.Type, field.Tag.Get("pact"))) | ||
args := m.fieldStrategyFunc(field) | ||
if args.name == "" { | ||
continue |
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 kind of a bug currently and now it will get skipped explicitly
…ructs field tags this allows customized support for struct field parsing. the example custom implementation could parse struct fields used to support protojson structs
e895411
to
62d394d
Compare
@hborham do you want to possibly mark this PR as a draft? |
Allows for reuse of structs generated by the client code for reuse with protobuf or json. The protobuf portion of the struct definition was not compatible with the MatchV2 due to some field types not supported, eg func. And in some cases the json information is only an attribute of protobuf tag.
This enhancement has a strategy of
The skip may be a breaking change, but as far as I can tell, any compliant type w/o a json tag would create an empty key in the struct, so technically it makes this more clear.
🤞