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

Use Rust FFI #14: Xml example #335

Closed
wants to merge 5 commits into from
Closed

Conversation

tienvx
Copy link
Contributor

@tienvx tienvx commented Aug 29, 2023

Add xml example.

This PR depends on #332. Please review it first.

"path": "/movies"
},
"response": {
"body": "<?xml version='1.0'?><movies><movie><title>PHP: Behind the Parser</title><characters><character><name>Ms. Coder</name><actor>Onlivia Actora</actor></character><character><name/><actor/></character></characters><plot>So, this language. It's like, a programming language. Or is it a\nscripting language? All is revealed in this thrilling horror spoof\nof a documentary.</plot><great-lines><line>PHP solves all my web problems</line></great-lines><rating type='thumbs'>7</rating><rating type='stars'>5</rating></movie></movies>",
Copy link
Member

Choose a reason for hiding this comment

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

😄

namespace XmlConsumer\Tests\Service;

use Tienvx\PactPhpXml\Model\Options;
use Tienvx\PactPhpXml\XmlBuilder;
Copy link
Member

Choose a reason for hiding this comment

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

We will probably want to document that this comes from a seperate-repo that would need to be added, into a readme of the changes.

https://github.com/tienvx/pact-php-xml

ps, what is the advantage of publishing these are separate packages?

Is it useful for

  • users of the library?
  • testing of the library?

or are there other advantages, outside of the possibly maintainence sprawl of having lots of different projects.

It might be that it works for PHP and is what users expect, but equally leads to more places to maintain, and more things to convey to the user, that they need, conditionally, depending on their particular use case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason I put some code outside of this PR, into separate project pact-php-xml because:

  • I just want to make this PR small so it easier to review
  • The code of that library is highly opinionated
  • That library is optional, can easily replace by raw json

What do you think if I move the code back to this PR?

Copy link
Member

Choose a reason for hiding this comment

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

Not sure on a definitive answer, but good question and counter points. I've tagged a few other parties who may be offer to offer their thoughts.

On the fact its optional and highly opinionated, would you see this kind of library either coming under the pact-foundation, or us having a list of them in the repo, so that others can create their own and point to them (many ways to skin a cat and all that!)

Hey team, just wondering if you can cast your eyes on the above thread. 🙏🏾

@Greg0 @WaylandAce @Lewiscowles1986 @mmoll @cfmack

Copy link
Collaborator

Choose a reason for hiding this comment

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

reading now, but feel it would be better for Pact to have this repo under their umbrella with a contributor to review (there might be a lot I'd miss as I dip in and out)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Regarding the specifics, it's a value-object using builder; it's a strange mix. Why this vs templating? Unless I'm mistaken (in which case I apologise); but it looks like a special string builder. XML is not strictly speaking a string.

Copy link
Member

Choose a reason for hiding this comment

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

I believe it to be a special string builder, as we need to store the XML in the pact json file, so its stored in a string (along with any matchers that have been encoded as part of the test)

This is unpacked at the provider side, and reads the content type of application/xml and sends the content appropriately to the provider

Copy link
Member

Choose a reason for hiding this comment

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

thanks for the input dude 👍🏾

Copy link
Collaborator

Choose a reason for hiding this comment

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

json_encode($iContainXML) should handle escaping etc though right?

I guess we need to know if pact needs UTF-8 escaped, but XML does not and JSON spec does not, so I'm guessing not. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the builder to use the Functional Options pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • About UTF-8 encoding:
    • According to this safety note, I think yes, pact (in this case pactffi_with_body) need UTF-8 encoding.
    • According to this doc I think json_encode also need the value to be UTF-8 encoded.
  • About UTF-8 encoding vs escaping: I'm not good at this. I don't think I handle these things at all in the code. I think we need to add special test cases and then update the code to handle these cases.

Copy link
Member

@YOU54F YOU54F left a comment

Choose a reason for hiding this comment

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

Looks good, comment on some docs re pulling in pact-php-xml project but bar that sweet

@Lewiscowles1986
Copy link
Collaborator

Is there a concrete example of the XML this produces, or the "string with XML" this produces? I'd very much like to compare it to json_encode on the same string.

@tienvx
Copy link
Contributor Author

tienvx commented Sep 21, 2023

Is there a concrete example of the XML this produces, or the "string with XML" this produces?

Pact's Rust core (FFI) required the body in JSON format (not XML). Here is the json string (json encoded from builder's array):

        {
            "version": "1.0",
            "charset": "UTF-8",
            "root": {
                "name": "movies",
                "children": [
                    {
                        "pact:matcher:type": "type",
                        "value": {
                            "name": "movie",
                            "children": [
                                {
                                    "name": "title",
                                    "children": [
                                        {
                                            "content": "PHP: Behind the Parser",
                                            "matcher": {
                                                "pact:matcher:type": "type"
                                            }
                                        }
                                    ],
                                    "attributes": {}
                                },
                                {
                                    "name": "characters",
                                    "children": [
                                        {
                                            "pact:matcher:type": "type",
                                            "value": {
                                                "name": "character",
                                                "children": [
                                                    {
                                                        "name": "name",
                                                        "children": [
                                                            {
                                                                "content": "Ms. Coder",
                                                                "matcher": {
                                                                    "pact:matcher:type": "type"
                                                                }
                                                            }
                                                        ],
                                                        "attributes": {}
                                                    },
                                                    {
                                                        "name": "actor",
                                                        "children": [
                                                            {
                                                                "content": "Onlivia Actora",
                                                                "matcher": {
                                                                    "pact:matcher:type": "type"
                                                                }
                                                            }
                                                        ],
                                                        "attributes": {}
                                                    }
                                                ],
                                                "attributes": {}
                                            },
                                            "examples": 2
                                        }
                                    ],
                                    "attributes": {}
                                },
                                {
                                    "name": "plot",
                                    "children": [
                                        {
                                            "content": "So, this language. It's like, a programming language. Or is it a\\nscripting language? All is revealed in this thrilling horror spoof\\nof a documentary.",
                                            "matcher": {
                                                "pact:matcher:type": "type"
                                            }
                                        }
                                    ],
                                    "attributes": {}
                                },
                                {
                                    "name": "great-lines",
                                    "children": [
                                        {
                                            "pact:matcher:type": "type",
                                            "value": {
                                                "name": "line",
                                                "children": [
                                                    {
                                                        "content": "PHP solves all my web problems",
                                                        "matcher": {
                                                            "pact:matcher:type": "type"
                                                        }
                                                    }
                                                ],
                                                "attributes": {}
                                            },
                                            "examples": 1
                                        }
                                    ],
                                    "attributes": {}
                                },
                                {
                                    "name": "rating",
                                    "children": [
                                        {
                                            "content": 7,
                                            "matcher": {
                                                "pact:matcher:type": "type"
                                            }
                                        }
                                    ],
                                    "attributes": {
                                        "type": "thumbs"
                                    }
                                },
                                {
                                    "name": "rating",
                                    "children": [
                                        {
                                            "content": 5,
                                            "matcher": {
                                                "pact:matcher:type": "type"
                                            }
                                        }
                                    ],
                                    "attributes": {
                                        "type": "stars"
                                    }
                                }
                            ],
                            "attributes": {}
                        },
                        "examples": 1
                    }
                ],
                "attributes": {}
            }
        }

I'd very much like to compare it to json_encode on the same string.

I'm not sure I understand this one.

@Lewiscowles1986
Copy link
Collaborator

@tienvx the thing you are building the XML example for. I'd like to see what PHP's native json_encode does with the XML generated.

Copy link
Collaborator

@Lewiscowles1986 Lewiscowles1986 left a comment

Choose a reason for hiding this comment

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

Now it doesn't need coordination of ->start() and ->end() calls I think this is a lot easier to work with. The extra package pinned to dev main will need to have something happen before it can be released as that is an attack vector; but I see no problem having this go into main

@tienvx
Copy link
Contributor Author

tienvx commented Sep 29, 2023

Please review and merge #340 to make tests pass

@Lewiscowles1986
Copy link
Collaborator

@tienvx it's one line you made into another PR; the rest is just refactoring examples to be DRY (they don't have to be, they are separate examples). Does that one line have to be in another MR, or can it just be made here?

@Lewiscowles1986
Copy link
Collaborator

I've approved the other MR, please rebase this from that branch once #340 is merged, providing this meets the PACT foundation approval as well.

@YOU54F
Copy link
Member

YOU54F commented Oct 12, 2023

hmm, I rebased and merged this but this didn't close, maybe because I didn't push the rebase back to the this/original branch?

Following gh instructions in the merge command section above, albeit with the additional rebase

git checkout -b tienvx-xml ffi
git pull git@github.com:tienvx/pact-php.git xml
git rebase -i ffi
<rebase things>
git checkout ffi
git merge --no-ff tienvx-xml
git push origin ffi

@Lewiscowles1986
Copy link
Collaborator

So... Although this passes tests, the pinning to dev-main looks like it could lead to issues down the line
"tienvx/pact-php-xml": "dev-main",
@tienvx @YOU54F any chance that repo can be transferred into PACT control, under foundation heading, OR use a released tagged version that it is pinned to?

@tienvx
Copy link
Contributor Author

tienvx commented Oct 13, 2023

hmm, I rebased and merged this but this didn't close, maybe because I didn't push the rebase back to the this/original branch?

I think so. But if we push back to the branch of this PR, this PR will become empty. I think we just need to close this PR.

any chance that repo can be transferred into PACT control, under foundation heading

I'm happy to transfer, but I think this require more work to do, and I didn't do it before:

  • Agree about license? That library is currently MIT
  • Update package name, from tienvx/pact-php-xml to pact-foundation/pact-php-xml?
  • I don't think rename package in packagist is possible, so we need to publish another package named pact-foundation/pact-php-xml
  • Tag first version 1.0.0, but please review the code first if are there anything to change.

OR use a released tagged version that it is pinned to?

Please review that library, are there anything I need to update? then I will tag a version, probably 1.0.0

@YOU54F
Copy link
Member

YOU54F commented Oct 13, 2023

the pinning to dev-main looks like it could lead to issues down the line
"tienvx/pact-php-xml": "dev-main",
@tienvx @YOU54F any chance that repo can be transferred into PACT control, under foundation heading, OR use a released tagged version that it is pinned to?

agreed, we just hadn't agreed on a path forward atm but thanks for raising again I using a released tagged version that can be pinned to is sensible. It is sufficient to be from a git repo tag, rather than being published to packagist, especially whilst in our alpha phase?

Agree about license? That library is currently MIT

Licenses across the foundation are a mix of either MIT, or Apache 2.0, and I think everyone is generally comfortable with that.

related comment in maintainers guide draft pact-foundation/docs.pact.io#270 (comment)

Update package name, from tienvx/pact-php-xml to pact-foundation/pact-php-xml?
I don't think rename package in packagist is possible, so we need to publish another package named pact-foundation/pact-php-xml

Yes I believe you are correct, that you cannot rename. That is why I believe the original pact-php packages are under mattermacks handle. I am okay with this being outside of the pact foundation and transferred in when you are ready Tien.

It's not a core piece piece of the project (not all users want/need the xml func) and it is added as a dev dep.

Up to you! If it gets transferred in, you will have admin rights anyway, as its your own repository. We've not taken admin rights away from users who have transferred their repos in, as they are ultimately the original owners and we are but caretakers!

Tag first version 1.0.0, but please review the code first if are there anything to change.

I would probably start with a <1.x version, until you get some real-world feedback, as users can take a v1.x library as production ready, but that is a matter of personal choice.

Given the below thoughts, it is probably worth tracking this is a separate issue, so we can close this PR down and avoid ancillary noise

hmm, I rebased and merged this but this didn't close, maybe because I didn't push the rebase back to the this/original branch?

I think so. But if we push back to the branch of this PR, this PR will become empty. I think we just need to close this PR.

@tienvx
Copy link
Contributor Author

tienvx commented Oct 13, 2023

I release version 0.1.0 of pact-php-xml and update it via this PR #347

Closed as commits are rebased to ffi branch.

@tienvx tienvx closed this Oct 13, 2023
@tienvx tienvx deleted the xml branch October 13, 2023 14:59
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.

None yet

3 participants