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

Ajouter fonctionnalité de modifier les attributs #5

Merged
merged 10 commits into from
Nov 3, 2023
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ jobs:
run: docker-compose exec -T -u root -w /workspace ci make init

- uses: FranzDiebold/github-env-vars-action@v2
- name: Run CI # up to test-int (info → refresh → lint → test → release → test-int)
- name: Run CI # up to release (info → refresh → lint → test → release)
run: |
docker-compose exec \
-T \
-u root \
-w /workspace \
-e PATH=/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/home/vscode/go/bin:/workspace/bin \
ci \
neon -props "{tag: ${CI_ACTION_REF_NAME}, MODULE: github.com/${CI_REPOSITORY,,}, BY: ${CI_ACTOR}}" test-int
neon -props "{tag: ${CI_ACTION_REF_NAME}, MODULE: github.com/${CI_REPOSITORY,,}, BY: ${CI_ACTOR}}" release
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Details are available on [the official Visual Studio documentation](https://code
- [ ] The code you added/modified **compile** (run the `neon compile` command)
- [ ] The code you added/modified **is linted** (run the `neon lint` command)
- [ ] The code you added/modified **is covered by unit tests** and **all tests are passing** (run the `neon test` command)
- [ ] The features you added/modified or the bugs you fixed **are covered by integration tests** and **all integration tests are passing** (run the `neon test-int` command)

4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ release-%: warning
release: warning
@neon release

.PHONY: test-int
test-int: warning
@neon test-int

.PHONY: publish-%
publish-%: warning
@neon -props "{buildpaths: ["cmd/$*"]}" publish
Expand Down
83 changes: 27 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,86 +1,57 @@

# XIXO - XML Input XML Output

**XIXO** is a versatile command-line tool designed for seamless XML input and XML output operations. It empowers you to manipulate XML content effortlessly, offering a range of features for both basic and advanced operations. This README provides a comprehensive guide to **XIXO**, including installation instructions, usage examples, and acknowledgments to the open-source community.
**XIXO** is a Go library that allows you to parse XML files and extract custom attributes. It empowers you to manipulate XML content effortlessly, offering a range of features for both basic and advanced operations. This README provides a comprehensive guide to **XIXO**, including installation instructions, usage examples, and acknowledgments to the open-source community.

## Installation

To install xixo, follow these steps:
To install xixo, you can use go get:

- Download the right release for your operating system and architecture from the xixo releases page.

- Extract the xixo binary from the downloaded archive.

- Move the xixo binary to a directory that is included in your system's PATH. This step is essential to make xixo accessible from any location in your terminal.

Note: If you are not sure which directory to use, you can typically place it in /usr/local/bin or ~/bin (for Linux/macOS) or C:\Windows\System32 (for Windows). You may need administrator privileges to move the binary to some directories.

- Verify the installation by running the following command in your terminal:

```bash
$ xixo --version
```

If installed correctly, it should display the version of xixo.
go get github.com/CGI-FR/XIXO
```

Now, you can start using xixo to edit XML files with ease.

## Example

### Input XML
To use **XIXO**, you need to create a Parser object with the path of the XML file to parse and the name of element, here is a emexple of XML file: (same exemple in Unit testing **TestMapCallbackWithAttributsParentAndChilds()** in callback_test.go )

```xml
<root>
<foo>
<bar>a</bar>
<baz>z</baz>
</foo>
<foo>
<bar>b</bar>
</foo>
<baz>
<bar>c</bar>
</baz>
</root>
```

### Command

```shell
$ xixo --subscribers foo="tee debug.jsonl | jq --unbuffered -c '.bar |= ascii_upcase' " < test/data/foo_bar_baz.xml
<root>
<foo>
<bar>A</bar>
<baz>z</baz>
</foo>
<foo>
<bar>B</bar>
</foo>
<baz>
<bar>c</bar>
</baz>
<root type="foo">
<element1 age="22" sex="male">Hello world !</element1>
<element2>Contenu2 </element2>
</root>
```

### Process Description

1. **Initialization**: **xixo** begins by parsing the input XML file in a streaming manner. It identifies the structure of the XML and locates elements that match the subscriber criteria (`foo` elements in this case).
1. **Initialization**: **xixo** begins by parsing the input XML file in a streaming manner. It identifies the structure of the XML and locates elements that match the begin element name. (`root` elements in this case).

2. **Subscriber Script Execution**: The subscriber script (`tee debug.jsonl | jq --unbuffered -c '.bar |= ascii_upcase'`) is executed once at the beginning of the parsing process, as indicated. It's important to note that the script is not called separately for each `foo` element but rather only once as the input is piped from **xixo**. The script performs the following steps:
2. **Transform XML to Go map** As the XML parsing progresses, **xixo** reads the XML file and creat a element tree of root element. In exemple will be:

- It starts by using `tee` to write matched elements (e.g., `<foo><bar>a</bar><baz>z</baz></foo>`) as JSON lines, such as `{"bar":"a","baz":"z"}`, to the `debug.jsonl` file.
- It then uses `jq` to apply the transformation to the `bar` element within the JSON content. The `ascii_upcase` function is used to convert the text within the `bar` element to uppercase. The `--unbuffered` flag ensures that **jq** processes the input line by line.
- The script generates modified JSON lines, such as `{"bar":"A","baz":"z"}` and `{"bar":"B"}`, and writes them to the standard output.
```go
{"@type":"foo","element1":"Hello world !","element1@age":"22","element1":"male","element2":"Contenu2 "}.
```

3. **Merging JSON Output**: As the XML parsing progresses, **xixo** reads the JSON lines from the script's standard output. Each line of JSON data corresponds to a `foo` element in the XML. **xixo** combines this JSON data with the current matching XML element to produce the updated XML structure with the transformations applied. The modified XML is then emitted as output in a streaming manner.
3. **Edite element value and attribute value with callback**: For modify the data, we need give callback function a map with the element name as key, new data as value. In exemple we give a map like this:

4. **Final Output**: The final XML output, with the transformations applied to the `bar` elements within the `foo` elements.
```go
{"@type":"bar","element1@age":"50","element1":"newChildContent","element2@age":"25"}.
```

### Key Points
4. **Final Output**: The final XML output will be:

- **Subscriber Script Execution**: The subscriber script is executed only once at the beginning of the parsing process and then stopped at the end. It processes the XML elements as they match the criteria and serializes them into JSON lines, which are then merged with the corresponding XML elements to produce the modified XML output.
```xml
<root type="bar">
<element1 age="50" sex="male">newChildContent</element1>
<element2 age="25">Contenu2 </element2>
</root>
```

### Key Points

- **Performance Optimization**: **xixo** optimizes performance by not calling the subscriber script for each `foo` element separately but rather processing the input in a stream and merging the results efficiently.
- **Performance Optimization**: **xixo** optimizes performance by not calling the subscriber script for each `root` element separately but rather processing the input in a stream and merging the results efficiently.

This detailed process demonstrates how **xixo** processes XML files in a streaming and efficient manner, applying custom transformations to specific elements using subscribers.

Expand Down
22 changes: 1 addition & 21 deletions build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ expose:
"lint",
"test",
"release",
"test-int",
"publish",
"docker",
"docker-tag",
Expand Down Expand Up @@ -93,8 +92,6 @@ targets:
print -P "%B%F{blue}lint%f%b Examine source code and report suspicious constructs [info->refresh]"
print -P "%B%F{blue}test%f%b Run all tests with coverage [info->refresh->lint]"
print -P "%B%F{blue}release%f%b Compile binary files for production [info->refresh->lint->test]"
print -P "%B%F{blue}test-int%f%b Run all integration tests [info->refresh->lint->test->release]"
print -P "%B%F{blue}publish%f%b Publish tagged binary to Github [info->refresh->lint->test->release->test-int]"
print -P "%B%F{blue}docker%f%b Build docker images [info]"
print -P "%B%F{blue}docker-tag%f%b Tag docker images [info->docker]"
print -P "%B%F{blue}docker-push%f%b Publish docker images to Dockerhub [info->docker->docker-tag]"
Expand All @@ -108,7 +105,6 @@ targets:
print -P "→ promote"
print -P "→ info ┰ docker → docker-tag → docker-push"
print -P " ┖ refresh ┰ compile → license"
print -P " ┖ lint → test → release → test-int → publish"

info:
doc: "Print build informations"
Expand Down Expand Up @@ -259,27 +255,11 @@ targets:
- ldflags = ldflags + " -s -w" # Omit the DWARF symbol table. Omit the symbol table and debug information.
- call: compile

# to debug a single test use :
# neon -props '{testsuites: "<test suite filename>"}' test-int-debug
test-int-debug:
doc: "Run all integration tests"
depends: ["info", "compile"]
steps:
- delete: venom*.log
- $: venom run test/suites/={testsuites}

test-int:
doc: "Run all integration tests"
depends: ["info", "refresh", "lint", "test", "bench", "release"]
steps:
- delete: venom*.log
- $: venom run test/suites/={testsuites}

# run "neon -props '{buildpaths: ["path/to/main/package1","path/to/main/package2"]}' publish" to publish specific targets
# example : neon -props '{buildpaths: ["cmd/cli"]}' publish
publish:
doc: "Publish tagged binaries to Github"
depends: ["info", "refresh", "lint", "test", "release", "test-int"]
depends: ["info", "refresh", "lint", "test", "release"]
steps:
- $: git clean -f -d
- if: len(buildpaths) == 0
Expand Down
137 changes: 0 additions & 137 deletions cmd/xixo/main.go

This file was deleted.

5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ module github.com/youen/xixo
go 1.21

require (
github.com/mattn/go-isatty v0.0.14
github.com/rs/zerolog v1.28.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
github.com/tamerh/xpath v1.0.0
)

require (
github.com/antchfx/xpath v1.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
github.com/antchfx/xpath v1.2.4 h1:dW1HB/JxKvGtJ9WyVGJ0sIoEcqftV3SqIstujI+B9XY=
github.com/antchfx/xpath v1.2.4/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
Expand All @@ -17,11 +14,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tamerh/xpath v1.0.0 h1:NccMES/Ej8slPCFDff73Kf6V1xu9hdbuKf2RyDsxf5Q=
Expand Down
Loading
Loading