Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
add iexplciit conversion to avoid warn
Browse files Browse the repository at this point in the history
Create go.yml

Update go.yml

Update go.yml

Update go.yml

Update go.yml

Update go.yml

Update go.yml

Update go.yml

Update go.yml

upgrade version and some lint

fix in go mod

Change Action
  • Loading branch information
zapisanchez committed Nov 24, 2023
1 parent 93e5e3d commit 44d59d7
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 86 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/Arch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: ["master"]
pull_request:
branches: ["master"]

jobs:
build:
runs-on: ubuntu-latest

container:
image: archlinux:latest

steps:
- uses: actions/checkout@v3

- name: Set Up Arch Linux
run: |
pacman -Syy --noconfirm
pacman -Syu --noconfirm
pacman -S --noconfirm base-devel
pacman -S --noconfirm libxml2
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.21"

- name: Test
run: go test ./...
4 changes: 2 additions & 2 deletions clib/clib.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ static inline void MY_xmlFree(void *p) {
// Macro wrapper function. cgo cannot detect function-like macros,
// so this is how we avoid it
static inline xmlError* MY_xmlLastError() {
return xmlGetLastError();
return (xmlError*) xmlGetLastError();
}
// Macro wrapper function. cgo cannot detect function-like macros,
// so this is how we avoid it
static inline xmlError* MY_xmlCtxtLastError(void *ctx) {
return xmlCtxtGetLastError(ctx);
return (xmlError*) xmlCtxtGetLastError(ctx);
}
// Change xmlIndentTreeOutput global, return old value, so caller can
Expand Down
16 changes: 8 additions & 8 deletions dom/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,40 +265,40 @@ func TestDocumentCreateAttributeNS(t *testing.T) {
t.Errorf("Failed to create Element node: %s", err)
return
}
d.SetDocumentElement(elem)
_ = d.SetDocumentElement(elem)

attr, err := d.CreateAttribute("attr", "e & f")
if err != nil {
t.Errorf("Failed to create Attribute node: %s", err)
return
}
elem.AddChild(attr)
_ = elem.AddChild(attr)

if elem.String() != `<foo attr="e &amp; f"/>` {
t.Errorf(`Expected String '<foo attr="e &amp; f"/>', got '%s'`, elem.String())
return
}
elem.RemoveAttribute("attr")
_ = elem.RemoveAttribute("attr")

attr, err = d.CreateAttributeNS("", "attr2", "a & b")
if err != nil {
t.Errorf("Failed to create Attribute node: %s", err)
return
}
elem.AddChild(attr)
_ = elem.AddChild(attr)

if elem.String() != `<foo attr2="a &amp; b"/>` {
t.Errorf(`Expected String '<foo attr2="a &amp; b"/>', got '%s'`, elem.String())
return
}
elem.RemoveAttribute("attr2")
_ = elem.RemoveAttribute("attr2")

attr, err = d.CreateAttributeNS("http://kungfoo", "foo:attr3", "g & h")
if err != nil {
t.Errorf("Failed to create Attribute node: %s", err)
return
}
elem.AddChild(attr)
_ = elem.AddChild(attr)

if elem.String() != `<foo xmlns:foo="http://kungfoo" foo:attr3="g &amp; h"/>` {
t.Errorf(`Expected String '<foo xmlns:foo="http://kungfoo" foo:attr3="g &amp; h"/>', got '%s'`, elem.String())
Expand All @@ -318,7 +318,7 @@ func TestDocumentCreateAttributeNS(t *testing.T) {
t.Errorf("Failed to create Element node: %s", err)
return
}
d.SetDocumentElement(elem)
_ = d.SetDocumentElement(elem)

attr, err := d.CreateAttributeNS("http://kungfoo", "kung:foo", "bar")
if err != nil {
Expand Down Expand Up @@ -355,7 +355,7 @@ func TestDocumentCreateAttributeNS(t *testing.T) {
t.Errorf("Failed to create Element node: %s", err)
return
}
d.SetDocumentElement(elem)
_ = d.SetDocumentElement(elem)

badnames := []string{";", "&", "<><", "/", "1A"}
for _, name := range badnames {
Expand Down
15 changes: 8 additions & 7 deletions dom/node_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package dom
package dom_test

import (
"fmt"
"testing"

"github.com/lestrrat-go/libxml2/clib"
. "github.com/lestrrat-go/libxml2/dom"
"github.com/lestrrat-go/libxml2/types"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -123,15 +124,15 @@ func TestDOM(t *testing.T) {
return
}

doc.SetDocumentElement(root)
_ = doc.SetDocumentElement(root)
var toRemove types.Node
for i := 1; i <= 3; i++ {
child, err := doc.CreateElement(fmt.Sprintf("child%d", i))
if !assert.NoError(t, err, "dom.CreateElement(child%d) should succeed", i) {
return
}
child.AppendText(fmt.Sprintf("text%d", i))
root.AddChild(child)
_ = child.AppendText(fmt.Sprintf("text%d", i))
_ = root.AddChild(child)

if i == 2 {
toRemove = child
Expand Down Expand Up @@ -218,19 +219,19 @@ func TestCreateElementNS(t *testing.T) {
if !assert.NoError(t, err, "CreateElementNS should succeed") {
return
}
doc.SetDocumentElement(root)
_ = doc.SetDocumentElement(root)

n1, err := doc.CreateElementNS("http://foo.bar.baz", "foo:n1")
if !assert.NoError(t, err, "CreateElementNS should succeed") {
return
}
root.AddChild(n1)
_ = root.AddChild(n1)

n2, err := doc.CreateElementNS("http://foo.bar.baz", "bar:n2")
if !assert.NoError(t, err, "CreateElementNS should succeed") {
return
}
root.AddChild(n2)
_ = root.AddChild(n2)

_, err = doc.CreateElementNS("http://foo.bar.baz.quux", "foo:n3")
if !assert.Error(t, err, "CreateElementNS should fail") {
Expand Down
35 changes: 22 additions & 13 deletions libxml2.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
//go:generate go run internal/cmd/genwrapnode/genwrapnode.go -- dom/node_wrap.go
package libxml2

/*
Package libxml2 is an interface to libxml2 library, providing XML and HTML parsers
with DOM interface. The inspiration is Perl5's XML::LibXML module.
import (
"io"

This library is still in very early stages of development. API may still change
without notice.
"github.com/lestrrat-go/libxml2/parser"
"github.com/lestrrat-go/libxml2/types"
)

For the time being, the API is being written so that thye are as close as we
can get to DOM Layer 3, but some methods will, for the time being, be punted
and aliases for simpler methods that don't necessarily check for the DOM's
correctness will be used.
// Parse parses the given buffer and returns a Document.
func Parse(buf []byte, o ...parser.Option) (types.Document, error) {
p := parser.New(o...)
return p.Parse(buf)
}

Also, the return values are still shaky -- I'm still debating how to handle error cases gracefully.
*/
package libxml2
// ParseString parses the given string and returns a Document.
func ParseString(s string, o ...parser.Option) (types.Document, error) {
p := parser.New(o...)
return p.ParseString(s)
}

// ParseReader parses XML from the given io.Reader and returns a Document.
func ParseReader(rdr io.Reader, o ...parser.Option) (types.Document, error) {
p := parser.New(o...)
return p.ParseReader(rdr)
}
14 changes: 7 additions & 7 deletions libxml2_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestBenchmarkLibxml2Xmlpath(t *testing.T) {
if !assert.NoError(t, err, "xpath.NewContext succeeds") {
return
}
xpc.RegisterNS("atom", "http://www.w3.org/2005/Atom")
_ = xpc.RegisterNS("atom", "http://www.w3.org/2005/Atom")

res, err := xpc.Find(`//atom:entry`)
if !assert.NoError(t, err, "xpc.Find succeeds") {
Expand Down Expand Up @@ -97,7 +97,7 @@ func BenchmarkLibxml2Xmlpath(b *testing.B) {
if err != nil {
b.Fatalf("%s", err)
}
xpc.RegisterNS("atom", "http://www.w3.org/2005/Atom")
_ = xpc.RegisterNS("atom", "http://www.w3.org/2005/Atom")
for i := 0; i < b.N; i++ {
iter := xpath.NodeIter(xpc.Find(`//atom:entry`))
for iter.Next() {
Expand All @@ -123,7 +123,7 @@ func BenchmarkEncodingXMLDOM(b *testing.B) {
for i := 0; i < b.N; i++ {
buf.Reset()
enc := xml.NewEncoder(&buf)
enc.Encode(f)
_ = enc.Encode(f)
}
}

Expand All @@ -142,18 +142,18 @@ func BenchmarkLibxml2DOM(b *testing.B) {
d.Free()
panic(err)
}
d.SetDocumentElement(root)
_ = d.SetDocumentElement(root)

f1xml, err := d.CreateElement("Field1")
if err != nil {
d.Free()
panic(err)
}
root.AddChild(f1xml)
_ = root.AddChild(f1xml)

f1xml.SetAttribute("Field2", f.Field2)
_ = f1xml.SetAttribute("Field2", f.Field2)

f1xml.AppendText(f.Field1)
_ = f1xml.AppendText(f.Field1)
buf.Reset()
buf.WriteString(d.Dump(false))
d.Free()
Expand Down
6 changes: 3 additions & 3 deletions libxml2_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ExampleXML() {
}
defer doc.Free()

doc.Walk(func(n types.Node) error {
_ = doc.Walk(func(n types.Node) error {
log.Println(n.NodeName())
return nil
})
Expand All @@ -45,7 +45,7 @@ func ExampleXML() {
}
defer ctx.Free()

ctx.RegisterNS("atom", "http://www.w3.org/2005/Atom")
_ = ctx.RegisterNS("atom", "http://www.w3.org/2005/Atom")
title := xpath.String(ctx.Find("/atom:feed/atom:title/text()"))
log.Printf("feed title = %s", title)
}
Expand All @@ -65,7 +65,7 @@ func ExampleHTML() {
}
defer doc.Free()

doc.Walk(func(n types.Node) error {
_ = doc.Walk(func(n types.Node) error {
log.Println(n.NodeName())
return nil
})
Expand Down
19 changes: 10 additions & 9 deletions parser_test.go → libxml2_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package libxml2
package libxml2_test

import (
"regexp"
"testing"

"github.com/lestrrat-go/libxml2"
"github.com/lestrrat-go/libxml2/dom"
"github.com/lestrrat-go/libxml2/types"

Expand Down Expand Up @@ -74,7 +75,7 @@ var (
func parseShouldSucceed(t *testing.T, opts parser.Option, inputs []string) {
t.Logf("Test parsing with parser %v", opts)
for _, s := range inputs {
d, err := ParseString(s, opts)
d, err := libxml2.ParseString(s, opts)
if !assert.NoError(t, err, "Parse should succeed") {
return
}
Expand All @@ -84,7 +85,7 @@ func parseShouldSucceed(t *testing.T, opts parser.Option, inputs []string) {

func parseShouldFail(t *testing.T, opts parser.Option, inputs []string) {
for _, s := range inputs {
d, err := ParseString(s, opts)
d, err := libxml2.ParseString(s, opts)
if err == nil {
d.Free()
t.Errorf("Expected failure to parse '%s'", s)
Expand Down Expand Up @@ -201,7 +202,7 @@ func TestParseOptionStringer(t *testing.T) {
}

func TestParseEmpty(t *testing.T) {
doc, err := ParseString(``)
doc, err := libxml2.ParseString(``)
if err == nil {
t.Errorf("Parse of empty string should fail")
defer doc.Free()
Expand Down Expand Up @@ -245,7 +246,7 @@ func TestParseNoBlanks(t *testing.T) {
}

func TestRoundtripNoBlanks(t *testing.T) {
doc, err := ParseString(`<a> <b/> </a>`, parser.XMLParseNoBlanks)
doc, err := libxml2.ParseString(`<a> <b/> </a>`, parser.XMLParseNoBlanks)
if err != nil {
t.Errorf("failed to parse string: %s", err)
return
Expand Down Expand Up @@ -275,7 +276,7 @@ func TestGHIssue23(t *testing.T) {
<goodbye>Goodbye!</goodbye>
</rootnode>`

doc, err := ParseString(src, parser.XMLParseRecover, parser.XMLParseNoWarning, parser.XMLParseNoError)
doc, err := libxml2.ParseString(src, parser.XMLParseRecover, parser.XMLParseNoWarning, parser.XMLParseNoError)
if !assert.NoError(t, err, "should pass") {
return
}
Expand All @@ -286,7 +287,7 @@ func TestCommentWrapNodeIssue(t *testing.T) {
// should wrap comment node
const testHTML = "<p><!-- test --></p><!-- test --><p><!-- test --></p>"

doc, err := ParseHTMLString(testHTML, parser.HTMLParseRecover)
doc, err := libxml2.ParseHTMLString(testHTML, parser.HTMLParseRecover)
if err != nil {
t.Fatalf("Got error when parsing HTML: %v", err)
}
Expand All @@ -309,7 +310,7 @@ func TestCommentWrapNodeIssue(t *testing.T) {
func TestPiWrapNodeIssue(t *testing.T) {
// should wrap Pi node
const textXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<a>test <?test?></a>\n"
doc, err := ParseString(textXML)
doc, err := libxml2.ParseString(textXML)
if err != nil {
t.Fatalf("Got error when parsing xml: %v", err)
}
Expand All @@ -334,7 +335,7 @@ func TestPiWrapNodeIssue(t *testing.T) {

func TestGetNonexistentAttributeReturnsRecoverableError(t *testing.T) {
const src = `<?xml version="1.0"?><rootnode/>`
doc, err := ParseString(src)
doc, err := libxml2.ParseString(src)
if !assert.NoError(t, err, "Should parse") {
return
}
Expand Down
26 changes: 0 additions & 26 deletions parser.go

This file was deleted.

Loading

0 comments on commit 44d59d7

Please sign in to comment.