Initial Commit
This commit is contained in:
commit
972b495734
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
qrz
|
||||||
|
*.ini
|
65
README.md
Normal file
65
README.md
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
# qrz
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
qrz is a small program that frs identifiers from hamstation.eu to database.
|
||||||
|
Currently WIP
|
||||||
|
|
||||||
|
## Howto
|
||||||
|
|
||||||
|
### Build
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go build
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sample config in qrz.ini
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[qrz]
|
||||||
|
db_hostname="localhost"
|
||||||
|
db_name="database"
|
||||||
|
db_username="username"
|
||||||
|
db_password="password"
|
||||||
|
db_schema="CREATE TABLE qrz (qrz VARCHAR(25) PRIMARY KEY, name VARCHAR(25), address VARCHAR(50), city VARCHAR(25), zipcode VARCHAR(25), dept VARCHAR(25), country VARCHAR(25)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
|
||||||
|
frs_url="http://groupe-frs.hamstation.eu/index_qrz_liste_14.htm"
|
||||||
|
frs_xpath="//tr"
|
||||||
|
frs_regex="^[0-9]{1,4}\s[A-Z]{1,4}\s[0-9]{1,4}"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./qrz -configfile qrz.ini
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
```text
|
||||||
|
Copyright (c) 2019, PaulBSD
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
The views and conclusions contained in the software and documentation are those
|
||||||
|
of the authors and should not be interpreted as representing official policies,
|
||||||
|
either expressed or implied, of the fuelprices project.
|
||||||
|
```
|
124
functions.go
Normal file
124
functions.go
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/antchfx/htmlquery"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
"golang.org/x/text/encoding/charmap"
|
||||||
|
"gopkg.in/ini.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetConfig fetch configuration
|
||||||
|
func (config *Config) GetConfig() error {
|
||||||
|
var configfile string
|
||||||
|
|
||||||
|
flag.Usage = Usage
|
||||||
|
|
||||||
|
flag.StringVar(&configfile, "configfile", "qrz.ini", "config file to use with qrz section")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
cfg, err := ini.Load(configfile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
qrzsection := cfg.Section("qrz")
|
||||||
|
|
||||||
|
config.DbHostname = qrzsection.Key("db_hostname").MustString("localhost")
|
||||||
|
config.DbName = qrzsection.Key("db_name").MustString("database")
|
||||||
|
config.DbUsername = qrzsection.Key("db_username").MustString("username")
|
||||||
|
config.DbPassword = qrzsection.Key("db_password").MustString("password")
|
||||||
|
config.DbSchema = qrzsection.Key("db_schema").MustString("")
|
||||||
|
config.DbTable = qrzsection.Key("db_table").MustString("qrz")
|
||||||
|
config.FrsConfig.URL = qrzsection.Key("frs_url").MustString("")
|
||||||
|
config.FrsConfig.XPath = qrzsection.Key("frs_xpath").MustString("//tr")
|
||||||
|
config.FrsConfig.Regex = qrzsection.Key("frs_regex").MustString(".*")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBody fetch html page
|
||||||
|
func GetBody(config Config) (string, error) {
|
||||||
|
var client http.Client
|
||||||
|
|
||||||
|
resp, err := client.Get(config.FrsConfig.URL)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
dec := charmap.ISO8859_1.NewDecoder()
|
||||||
|
output, _ := dec.Bytes(body)
|
||||||
|
bodyString := string(output)
|
||||||
|
|
||||||
|
return bodyString, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFrsEntries get FRS entries from html body
|
||||||
|
func GetFrsEntries(config Config, body string) ([]FrsEntry, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
var frsentries []FrsEntry
|
||||||
|
re := regexp.MustCompile(config.FrsConfig.Regex)
|
||||||
|
|
||||||
|
htmlpage, err := htmlquery.Parse(strings.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
return frsentries, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, n := range htmlquery.Find(htmlpage, config.FrsConfig.XPath) {
|
||||||
|
var frs FrsEntry
|
||||||
|
td := htmlquery.Find(n, "//td")
|
||||||
|
if re.MatchString(htmlquery.InnerText(td[0])) {
|
||||||
|
frs.QRZ = strings.Replace(htmlquery.InnerText(td[0]), "'", "\\'", -1)
|
||||||
|
frs.Name = strings.Replace(htmlquery.InnerText(td[1]), "'", "\\'", -1)
|
||||||
|
frs.City = strings.Replace(htmlquery.InnerText(td[2]), "'", "\\'", -1)
|
||||||
|
frs.Dept = strings.Replace(htmlquery.InnerText(td[3]), "'", "\\'", -1)
|
||||||
|
frs.Country = strings.Replace(htmlquery.InnerText(td[4]), "'", "\\'", -1)
|
||||||
|
|
||||||
|
frsentries = append(frsentries, frs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return frsentries, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertFrsEntryToDB inserts frs entries to database
|
||||||
|
func InsertFrsEntryToDB(config Config, db sqlx.DB, frsPeople []FrsEntry) error {
|
||||||
|
var err error
|
||||||
|
tx := db.MustBegin()
|
||||||
|
var qrzNum int
|
||||||
|
|
||||||
|
fmt.Println(fmt.Sprintf("Starting insert %d entries", len(frsPeople)))
|
||||||
|
|
||||||
|
for _, j := range frsPeople {
|
||||||
|
query := fmt.Sprintf(config.DbInsertStatement, config.DbTable, j.QRZ, j.Name, j.City, j.Dept, j.Country)
|
||||||
|
tx.MustExec(query)
|
||||||
|
qrzNum++
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(fmt.Sprintf("Commited %d entries", qrzNum))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Usage displays possible arguments
|
||||||
|
func Usage() {
|
||||||
|
flag.PrintDefaults()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
15
go.mod
Normal file
15
go.mod
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
module qrz
|
||||||
|
|
||||||
|
go 1.12
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/antchfx/htmlquery v1.0.0
|
||||||
|
github.com/antchfx/xpath v1.0.0 // indirect
|
||||||
|
github.com/go-sql-driver/mysql v1.4.1
|
||||||
|
github.com/jmoiron/sqlx v1.2.0
|
||||||
|
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
|
||||||
|
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 // indirect
|
||||||
|
golang.org/x/text v0.3.2
|
||||||
|
google.golang.org/appengine v1.6.2 // indirect
|
||||||
|
gopkg.in/ini.v1 v1.46.0
|
||||||
|
)
|
42
go.sum
Normal file
42
go.sum
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
github.com/antchfx/htmlquery v1.0.0 h1:O5IXz8fZF3B3MW+B33MZWbTHBlYmcfw0BAxgErHuaMA=
|
||||||
|
github.com/antchfx/htmlquery v1.0.0/go.mod h1:MS9yksVSQXls00iXkiMqXr0J+umL/AmxXKuP28SUJM8=
|
||||||
|
github.com/antchfx/xpath v1.0.0 h1:Q5gFgh2O40VTSwMOVbFE7nFNRBu3tS21Tn0KAWeEjtk=
|
||||||
|
github.com/antchfx/xpath v1.0.0/go.mod h1:Yee4kTMuNiPYJ7nSNorELQMr1J33uOpXDMByNYhvtNk=
|
||||||
|
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||||
|
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
|
||||||
|
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||||
|
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
||||||
|
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||||
|
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
|
||||||
|
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
|
||||||
|
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||||
|
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||||
|
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||||
|
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
|
||||||
|
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||||
|
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
|
||||||
|
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||||
|
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 h1:WN9BUFbdyOsSH/XohnWpXOlq9NBD5sGAB2FciQMUEe8=
|
||||||
|
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||||
|
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM=
|
||||||
|
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||||
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
|
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
|
||||||
|
google.golang.org/appengine v1.6.2 h1:j8RI1yW0SkI+paT6uGwMlrMI/6zwYA6/CFil8rxOzGI=
|
||||||
|
google.golang.org/appengine v1.6.2/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
|
||||||
|
gopkg.in/ini.v1 v1.46.0 h1:VeDZbLYGaupuvIrsYCEOe/L/2Pcs5n7hdO1ZTjporag=
|
||||||
|
gopkg.in/ini.v1 v1.46.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
45
qrz.go
Normal file
45
qrz.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var config Config
|
||||||
|
|
||||||
|
config.GetConfig()
|
||||||
|
|
||||||
|
config.DbSchema = fmt.Sprintf("CREATE TABLE IF NOT EXISTS `%s` (`id` int(8) NOT NULL AUTO_INCREMENT, `qrz` varchar(25) NOT NULL, `name` varchar(25) DEFAULT NULL, `address` varchar(50) DEFAULT NULL, `city` varchar(50) DEFAULT NULL, `zipcode` varchar(5) DEFAULT NULL, `dept` varchar(50) DEFAULT NULL, `country` varchar(25) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `qrz` (`qrz`,`name`,`city`,`dept`) USING BTREE, KEY `test` (`country`), FULLTEXT KEY `city` (`city`), FULLTEXT KEY `dept` (`dept`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;", config.DbTable)
|
||||||
|
|
||||||
|
config.DbInsertStatement = "INSERT IGNORE INTO %s (qrz,name,city,dept,country) VALUES ('%s','%s','%s','%s','%s');"
|
||||||
|
|
||||||
|
db, err := sqlx.Connect("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s", config.DbUsername, config.DbPassword, config.DbHostname, config.DbName))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyString, err := GetBody(config)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
frsPeople, err := GetFrsEntries(config, bodyString)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = db.Exec(config.DbSchema)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = InsertFrsEntryToDB(config, *db, frsPeople)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
}
|
9
qrz.ini.sample
Normal file
9
qrz.ini.sample
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[qrz]
|
||||||
|
db_hostname="hostname"
|
||||||
|
db_name="database"
|
||||||
|
db_username="username"
|
||||||
|
db_password="password"
|
||||||
|
db_table="qrz_test"
|
||||||
|
frs_url="http://groupe-frs.hamstation.eu/index_qrz_liste_14.htm"
|
||||||
|
frs_xpath="//tr"
|
||||||
|
frs_regex="^[0-9]{1,4}\s[A-Z]{1,4}\s[0-9]{1,4}"
|
26
types.go
Normal file
26
types.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// Config is the global config of g2g
|
||||||
|
type Config struct {
|
||||||
|
DbHostname string
|
||||||
|
DbName string
|
||||||
|
DbUsername string
|
||||||
|
DbPassword string
|
||||||
|
DbSchema string
|
||||||
|
DbTable string
|
||||||
|
DbInsertStatement string
|
||||||
|
FrsConfig struct {
|
||||||
|
URL string
|
||||||
|
XPath string
|
||||||
|
Regex string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FrsEntry describe FRS people
|
||||||
|
type FrsEntry struct {
|
||||||
|
QRZ string `db:"qrz"`
|
||||||
|
Name string `db:"name"`
|
||||||
|
City string `db:"city"`
|
||||||
|
Dept string `db:"dept"`
|
||||||
|
Country string `db:"country"`
|
||||||
|
}
|
32
vendor/github.com/antchfx/htmlquery/.gitignore
generated
vendored
Normal file
32
vendor/github.com/antchfx/htmlquery/.gitignore
generated
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# vscode
|
||||||
|
.vscode
|
||||||
|
debug
|
||||||
|
*.test
|
||||||
|
|
||||||
|
./build
|
||||||
|
|
||||||
|
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
|
||||||
|
|
||||||
|
# Folders
|
||||||
|
_obj
|
||||||
|
_test
|
||||||
|
|
||||||
|
# Architecture specific extensions/prefixes
|
||||||
|
*.[568vq]
|
||||||
|
[568vq].out
|
||||||
|
|
||||||
|
*.cgo1.go
|
||||||
|
*.cgo2.c
|
||||||
|
_cgo_defun.c
|
||||||
|
_cgo_gotypes.go
|
||||||
|
_cgo_export.*
|
||||||
|
|
||||||
|
_testmain.go
|
||||||
|
|
||||||
|
*.exe
|
||||||
|
*.test
|
||||||
|
*.prof
|
15
vendor/github.com/antchfx/htmlquery/.travis.yml
generated
vendored
Normal file
15
vendor/github.com/antchfx/htmlquery/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.6
|
||||||
|
- 1.7
|
||||||
|
- 1.8
|
||||||
|
|
||||||
|
install:
|
||||||
|
- go get golang.org/x/net/html/charset
|
||||||
|
- go get golang.org/x/net/html
|
||||||
|
- go get github.com/antchfx/xpath
|
||||||
|
- go get github.com/mattn/goveralls
|
||||||
|
|
||||||
|
script:
|
||||||
|
- $HOME/gopath/bin/goveralls -service=travis-ci
|
17
vendor/github.com/antchfx/htmlquery/LICENSE
generated
vendored
Normal file
17
vendor/github.com/antchfx/htmlquery/LICENSE
generated
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
102
vendor/github.com/antchfx/htmlquery/README.md
generated
vendored
Normal file
102
vendor/github.com/antchfx/htmlquery/README.md
generated
vendored
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
htmlquery
|
||||||
|
====
|
||||||
|
[![Build Status](https://travis-ci.org/antchfx/htmlquery.svg?branch=master)](https://travis-ci.org/antchfx/htmlquery)
|
||||||
|
[![Coverage Status](https://coveralls.io/repos/github/antchfx/htmlquery/badge.svg?branch=master)](https://coveralls.io/github/antchfx/htmlquery?branch=master)
|
||||||
|
[![GoDoc](https://godoc.org/github.com/antchfx/htmlquery?status.svg)](https://godoc.org/github.com/antchfx/htmlquery)
|
||||||
|
[![Go Report Card](https://goreportcard.com/badge/github.com/antchfx/htmlquery)](https://goreportcard.com/report/github.com/antchfx/htmlquery)
|
||||||
|
|
||||||
|
Overview
|
||||||
|
====
|
||||||
|
|
||||||
|
htmlquery is an XPath query package for HTML, lets you extract data or evaluate from HTML documents by an XPath expression.
|
||||||
|
|
||||||
|
Changelogs
|
||||||
|
===
|
||||||
|
|
||||||
|
2019-02-04
|
||||||
|
- [#7](https://github.com/antchfx/htmlquery/issues/7) Removed deprecated `FindEach()` and `FindEachWithBreak()` methods.
|
||||||
|
|
||||||
|
2018-12-28
|
||||||
|
- Avoid adding duplicate elements to list for `Find()` method. [#6](https://github.com/antchfx/htmlquery/issues/6)
|
||||||
|
|
||||||
|
Installation
|
||||||
|
====
|
||||||
|
|
||||||
|
> $ go get github.com/antchfx/htmlquery
|
||||||
|
|
||||||
|
Getting Started
|
||||||
|
====
|
||||||
|
|
||||||
|
#### Load HTML document from URL.
|
||||||
|
|
||||||
|
```go
|
||||||
|
doc, err := htmlquery.LoadURL("http://example.com/")
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Load HTML document from string.
|
||||||
|
|
||||||
|
```go
|
||||||
|
s := `<html>....</html>`
|
||||||
|
doc, err := htmlquery.Parse(strings.NewReader(s))
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Find all A elements.
|
||||||
|
|
||||||
|
```go
|
||||||
|
list := htmlquery.Find(doc, "//a")
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Find all A elements that have `href` attribute.
|
||||||
|
|
||||||
|
```go
|
||||||
|
list := range htmlquery.Find(doc, "//a[@href]")
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Find all A elements and only get `href` attribute self.
|
||||||
|
|
||||||
|
```go
|
||||||
|
list := range htmlquery.Find(doc, "//a/@href")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Find the third A element.
|
||||||
|
|
||||||
|
```go
|
||||||
|
a := htmlquery.FindOne(doc, "//a[3]")
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Evaluate the number of all IMG element.
|
||||||
|
|
||||||
|
```go
|
||||||
|
expr, _ := xpath.Compile("count(//img)")
|
||||||
|
v := expr.Evaluate(htmlquery.CreateXPathNavigator(doc)).(float64)
|
||||||
|
fmt.Printf("total count is %f", v)
|
||||||
|
```
|
||||||
|
|
||||||
|
Quick Tutorial
|
||||||
|
===
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
doc, err := htmlquery.LoadURL("https://www.bing.com/search?q=golang")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
// Find all news item.
|
||||||
|
for i, n := range htmlquery.Find(doc, "//ol/li") {
|
||||||
|
a := htmlquery.FindOne(n, "//a")
|
||||||
|
fmt.Printf("%d %s(%s)\n", i, htmlquery.InnerText(a), htmlquery.SelectAttr(a, "href"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
List of supported XPath query packages
|
||||||
|
===
|
||||||
|
|Name |Description |
|
||||||
|
|--------------------------|----------------|
|
||||||
|
|[htmlquery](https://github.com/antchfx/htmlquery) | XPath query package for the HTML document|
|
||||||
|
|[xmlquery](https://github.com/antchfx/xmlquery) | XPath query package for the XML document|
|
||||||
|
|[jsonquery](https://github.com/antchfx/jsonquery) | XPath query package for the JSON document|
|
||||||
|
|
||||||
|
Questions
|
||||||
|
===
|
||||||
|
Please let me know if you have any questions.
|
291
vendor/github.com/antchfx/htmlquery/query.go
generated
vendored
Normal file
291
vendor/github.com/antchfx/htmlquery/query.go
generated
vendored
Normal file
@ -0,0 +1,291 @@
|
|||||||
|
/*
|
||||||
|
Package htmlquery provides extract data from HTML documents using XPath expression.
|
||||||
|
*/
|
||||||
|
package htmlquery
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/antchfx/xpath"
|
||||||
|
"golang.org/x/net/html"
|
||||||
|
"golang.org/x/net/html/charset"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ xpath.NodeNavigator = &NodeNavigator{}
|
||||||
|
|
||||||
|
// CreateXPathNavigator creates a new xpath.NodeNavigator for the specified html.Node.
|
||||||
|
func CreateXPathNavigator(top *html.Node) *NodeNavigator {
|
||||||
|
return &NodeNavigator{curr: top, root: top, attr: -1}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find searches the html.Node that matches by the specified XPath expr.
|
||||||
|
func Find(top *html.Node, expr string) []*html.Node {
|
||||||
|
exp, err := xpath.Compile(expr)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
var elems []*html.Node
|
||||||
|
t := exp.Select(CreateXPathNavigator(top))
|
||||||
|
for t.MoveNext() {
|
||||||
|
nav := t.Current().(*NodeNavigator)
|
||||||
|
n := getCurrentNode(nav)
|
||||||
|
// avoid adding duplicate nodes.
|
||||||
|
if len(elems) > 0 && (elems[0] == n || (nav.NodeType() == xpath.AttributeNode &&
|
||||||
|
nav.LocalName() == elems[0].Data && nav.Value() == InnerText(elems[0]))) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
elems = append(elems, n)
|
||||||
|
}
|
||||||
|
return elems
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindOne searches the html.Node that matches by the specified XPath expr,
|
||||||
|
// and returns first element of matched html.Node.
|
||||||
|
func FindOne(top *html.Node, expr string) *html.Node {
|
||||||
|
var elem *html.Node
|
||||||
|
exp, err := xpath.Compile(expr)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
t := exp.Select(CreateXPathNavigator(top))
|
||||||
|
if t.MoveNext() {
|
||||||
|
elem = getCurrentNode(t.Current().(*NodeNavigator))
|
||||||
|
}
|
||||||
|
return elem
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadURL loads the HTML document from the specified URL.
|
||||||
|
func LoadURL(url string) (*html.Node, error) {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
r, err := charset.NewReader(resp.Body, resp.Header.Get("Content-Type"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return html.Parse(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCurrentNode(n *NodeNavigator) *html.Node {
|
||||||
|
if n.NodeType() == xpath.AttributeNode {
|
||||||
|
childNode := &html.Node{
|
||||||
|
Type: html.TextNode,
|
||||||
|
Data: n.Value(),
|
||||||
|
}
|
||||||
|
return &html.Node{
|
||||||
|
Type: html.ElementNode,
|
||||||
|
Data: n.LocalName(),
|
||||||
|
FirstChild: childNode,
|
||||||
|
LastChild: childNode,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return n.curr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse returns the parse tree for the HTML from the given Reader.
|
||||||
|
func Parse(r io.Reader) (*html.Node, error) {
|
||||||
|
return html.Parse(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// InnerText returns the text between the start and end tags of the object.
|
||||||
|
func InnerText(n *html.Node) string {
|
||||||
|
var output func(*bytes.Buffer, *html.Node)
|
||||||
|
output = func(buf *bytes.Buffer, n *html.Node) {
|
||||||
|
switch n.Type {
|
||||||
|
case html.TextNode:
|
||||||
|
buf.WriteString(n.Data)
|
||||||
|
return
|
||||||
|
case html.CommentNode:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for child := n.FirstChild; child != nil; child = child.NextSibling {
|
||||||
|
output(buf, child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
output(&buf, n)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectAttr returns the attribute value with the specified name.
|
||||||
|
func SelectAttr(n *html.Node, name string) (val string) {
|
||||||
|
if n == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if n.Type == html.ElementNode && n.Parent == nil && name == n.Data {
|
||||||
|
return InnerText(n)
|
||||||
|
}
|
||||||
|
for _, attr := range n.Attr {
|
||||||
|
if attr.Key == name {
|
||||||
|
val = attr.Val
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// OutputHTML returns the text including tags name.
|
||||||
|
func OutputHTML(n *html.Node, self bool) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if self {
|
||||||
|
html.Render(&buf, n)
|
||||||
|
} else {
|
||||||
|
for n := n.FirstChild; n != nil; n = n.NextSibling {
|
||||||
|
html.Render(&buf, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
type NodeNavigator struct {
|
||||||
|
root, curr *html.Node
|
||||||
|
attr int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) Current() *html.Node {
|
||||||
|
return h.curr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) NodeType() xpath.NodeType {
|
||||||
|
switch h.curr.Type {
|
||||||
|
case html.CommentNode:
|
||||||
|
return xpath.CommentNode
|
||||||
|
case html.TextNode:
|
||||||
|
return xpath.TextNode
|
||||||
|
case html.DocumentNode:
|
||||||
|
return xpath.RootNode
|
||||||
|
case html.ElementNode:
|
||||||
|
if h.attr != -1 {
|
||||||
|
return xpath.AttributeNode
|
||||||
|
}
|
||||||
|
return xpath.ElementNode
|
||||||
|
case html.DoctypeNode:
|
||||||
|
// ignored <!DOCTYPE HTML> declare and as Root-Node type.
|
||||||
|
return xpath.RootNode
|
||||||
|
}
|
||||||
|
panic(fmt.Sprintf("unknown HTML node type: %v", h.curr.Type))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) LocalName() string {
|
||||||
|
if h.attr != -1 {
|
||||||
|
return h.curr.Attr[h.attr].Key
|
||||||
|
}
|
||||||
|
return h.curr.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*NodeNavigator) Prefix() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) Value() string {
|
||||||
|
switch h.curr.Type {
|
||||||
|
case html.CommentNode:
|
||||||
|
return h.curr.Data
|
||||||
|
case html.ElementNode:
|
||||||
|
if h.attr != -1 {
|
||||||
|
return h.curr.Attr[h.attr].Val
|
||||||
|
}
|
||||||
|
return InnerText(h.curr)
|
||||||
|
case html.TextNode:
|
||||||
|
return h.curr.Data
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) Copy() xpath.NodeNavigator {
|
||||||
|
n := *h
|
||||||
|
return &n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) MoveToRoot() {
|
||||||
|
h.curr = h.root
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) MoveToParent() bool {
|
||||||
|
if h.attr != -1 {
|
||||||
|
h.attr = -1
|
||||||
|
return true
|
||||||
|
} else if node := h.curr.Parent; node != nil {
|
||||||
|
h.curr = node
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) MoveToNextAttribute() bool {
|
||||||
|
if h.attr >= len(h.curr.Attr)-1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
h.attr++
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) MoveToChild() bool {
|
||||||
|
if h.attr != -1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if node := h.curr.FirstChild; node != nil {
|
||||||
|
h.curr = node
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) MoveToFirst() bool {
|
||||||
|
if h.attr != -1 || h.curr.PrevSibling == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
node := h.curr.PrevSibling
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
h.curr = node
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) String() string {
|
||||||
|
return h.Value()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) MoveToNext() bool {
|
||||||
|
if h.attr != -1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if node := h.curr.NextSibling; node != nil {
|
||||||
|
h.curr = node
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) MoveToPrevious() bool {
|
||||||
|
if h.attr != -1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if node := h.curr.PrevSibling; node != nil {
|
||||||
|
h.curr = node
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *NodeNavigator) MoveTo(other xpath.NodeNavigator) bool {
|
||||||
|
node, ok := other.(*NodeNavigator)
|
||||||
|
if !ok || node.root != h.root {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
h.curr = node.curr
|
||||||
|
h.attr = node.attr
|
||||||
|
return true
|
||||||
|
}
|
32
vendor/github.com/antchfx/xpath/.gitignore
generated
vendored
Normal file
32
vendor/github.com/antchfx/xpath/.gitignore
generated
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# vscode
|
||||||
|
.vscode
|
||||||
|
debug
|
||||||
|
*.test
|
||||||
|
|
||||||
|
./build
|
||||||
|
|
||||||
|
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
|
||||||
|
|
||||||
|
# Folders
|
||||||
|
_obj
|
||||||
|
_test
|
||||||
|
|
||||||
|
# Architecture specific extensions/prefixes
|
||||||
|
*.[568vq]
|
||||||
|
[568vq].out
|
||||||
|
|
||||||
|
*.cgo1.go
|
||||||
|
*.cgo2.c
|
||||||
|
_cgo_defun.c
|
||||||
|
_cgo_gotypes.go
|
||||||
|
_cgo_export.*
|
||||||
|
|
||||||
|
_testmain.go
|
||||||
|
|
||||||
|
*.exe
|
||||||
|
*.test
|
||||||
|
*.prof
|
12
vendor/github.com/antchfx/xpath/.travis.yml
generated
vendored
Normal file
12
vendor/github.com/antchfx/xpath/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.6
|
||||||
|
- 1.9
|
||||||
|
- '1.10'
|
||||||
|
|
||||||
|
install:
|
||||||
|
- go get github.com/mattn/goveralls
|
||||||
|
|
||||||
|
script:
|
||||||
|
- $HOME/gopath/bin/goveralls -service=travis-ci
|
17
vendor/github.com/antchfx/xpath/LICENSE
generated
vendored
Normal file
17
vendor/github.com/antchfx/xpath/LICENSE
generated
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
167
vendor/github.com/antchfx/xpath/README.md
generated
vendored
Normal file
167
vendor/github.com/antchfx/xpath/README.md
generated
vendored
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
XPath
|
||||||
|
====
|
||||||
|
[![GoDoc](https://godoc.org/github.com/antchfx/xpath?status.svg)](https://godoc.org/github.com/antchfx/xpath)
|
||||||
|
[![Coverage Status](https://coveralls.io/repos/github/antchfx/xpath/badge.svg?branch=master)](https://coveralls.io/github/antchfx/xpath?branch=master)
|
||||||
|
[![Build Status](https://travis-ci.org/antchfx/xpath.svg?branch=master)](https://travis-ci.org/antchfx/xpath)
|
||||||
|
[![Go Report Card](https://goreportcard.com/badge/github.com/antchfx/xpath)](https://goreportcard.com/report/github.com/antchfx/xpath)
|
||||||
|
|
||||||
|
XPath is Go package provides selecting nodes from XML, HTML or other documents using XPath expression.
|
||||||
|
|
||||||
|
Implementation
|
||||||
|
===
|
||||||
|
|
||||||
|
- [htmlquery](https://github.com/antchfx/htmlquery) - an XPath query package for HTML document
|
||||||
|
|
||||||
|
- [xmlquery](https://github.com/antchfx/xmlquery) - an XPath query package for XML document.
|
||||||
|
|
||||||
|
- [jsonquery](https://github.com/antchfx/jsonquery) - an XPath query package for JSON document
|
||||||
|
|
||||||
|
Supported Features
|
||||||
|
===
|
||||||
|
|
||||||
|
#### The basic XPath patterns.
|
||||||
|
|
||||||
|
> The basic XPath patterns cover 90% of the cases that most stylesheets will need.
|
||||||
|
|
||||||
|
- `node` : Selects all child elements with nodeName of node.
|
||||||
|
|
||||||
|
- `*` : Selects all child elements.
|
||||||
|
|
||||||
|
- `@attr` : Selects the attribute attr.
|
||||||
|
|
||||||
|
- `@*` : Selects all attributes.
|
||||||
|
|
||||||
|
- `node()` : Matches an org.w3c.dom.Node.
|
||||||
|
|
||||||
|
- `text()` : Matches a org.w3c.dom.Text node.
|
||||||
|
|
||||||
|
- `comment()` : Matches a comment.
|
||||||
|
|
||||||
|
- `.` : Selects the current node.
|
||||||
|
|
||||||
|
- `..` : Selects the parent of current node.
|
||||||
|
|
||||||
|
- `/` : Selects the document node.
|
||||||
|
|
||||||
|
- `a[expr]` : Select only those nodes matching a which also satisfy the expression expr.
|
||||||
|
|
||||||
|
- `a[n]` : Selects the nth matching node matching a When a filter's expression is a number, XPath selects based on position.
|
||||||
|
|
||||||
|
- `a/b` : For each node matching a, add the nodes matching b to the result.
|
||||||
|
|
||||||
|
- `a//b` : For each node matching a, add the descendant nodes matching b to the result.
|
||||||
|
|
||||||
|
- `//b` : Returns elements in the entire document matching b.
|
||||||
|
|
||||||
|
- `a|b` : All nodes matching a or b, union operation(not boolean or).
|
||||||
|
|
||||||
|
- `(a, b, c)` : Evaluates each of its operands and concatenates the resulting sequences, in order, into a single result sequence
|
||||||
|
|
||||||
|
|
||||||
|
#### Node Axes
|
||||||
|
|
||||||
|
- `child::*` : The child axis selects children of the current node.
|
||||||
|
|
||||||
|
- `descendant::*` : The descendant axis selects descendants of the current node. It is equivalent to '//'.
|
||||||
|
|
||||||
|
- `descendant-or-self::*` : Selects descendants including the current node.
|
||||||
|
|
||||||
|
- `attribute::*` : Selects attributes of the current element. It is equivalent to @*
|
||||||
|
|
||||||
|
- `following-sibling::*` : Selects nodes after the current node.
|
||||||
|
|
||||||
|
- `preceding-sibling::*` : Selects nodes before the current node.
|
||||||
|
|
||||||
|
- `following::*` : Selects the first matching node following in document order, excluding descendants.
|
||||||
|
|
||||||
|
- `preceding::*` : Selects the first matching node preceding in document order, excluding ancestors.
|
||||||
|
|
||||||
|
- `parent::*` : Selects the parent if it matches. The '..' pattern from the core is equivalent to 'parent::node()'.
|
||||||
|
|
||||||
|
- `ancestor::*` : Selects matching ancestors.
|
||||||
|
|
||||||
|
- `ancestor-or-self::*` : Selects ancestors including the current node.
|
||||||
|
|
||||||
|
- `self::*` : Selects the current node. '.' is equivalent to 'self::node()'.
|
||||||
|
|
||||||
|
#### Expressions
|
||||||
|
|
||||||
|
The gxpath supported three types: number, boolean, string.
|
||||||
|
|
||||||
|
- `path` : Selects nodes based on the path.
|
||||||
|
|
||||||
|
- `a = b` : Standard comparisons.
|
||||||
|
|
||||||
|
* a = b True if a equals b.
|
||||||
|
* a != b True if a is not equal to b.
|
||||||
|
* a < b True if a is less than b.
|
||||||
|
* a <= b True if a is less than or equal to b.
|
||||||
|
* a > b True if a is greater than b.
|
||||||
|
* a >= b True if a is greater than or equal to b.
|
||||||
|
|
||||||
|
- `a + b` : Arithmetic expressions.
|
||||||
|
|
||||||
|
* `- a` Unary minus
|
||||||
|
* a + b Add
|
||||||
|
* a - b Substract
|
||||||
|
* a * b Multiply
|
||||||
|
* a div b Divide
|
||||||
|
* a mod b Floating point mod, like Java.
|
||||||
|
|
||||||
|
- `a or b` : Boolean `or` operation.
|
||||||
|
|
||||||
|
- `a and b` : Boolean `and` operation.
|
||||||
|
|
||||||
|
- `(expr)` : Parenthesized expressions.
|
||||||
|
|
||||||
|
- `fun(arg1, ..., argn)` : Function calls:
|
||||||
|
|
||||||
|
| Function | Supported |
|
||||||
|
| --- | --- |
|
||||||
|
`boolean()`| ✓ |
|
||||||
|
`ceiling()`| ✓ |
|
||||||
|
`choose()`| ✗ |
|
||||||
|
`concat()`| ✓ |
|
||||||
|
`contains()`| ✓ |
|
||||||
|
`count()`| ✓ |
|
||||||
|
`current()`| ✗ |
|
||||||
|
`document()`| ✗ |
|
||||||
|
`element-available()`| ✗ |
|
||||||
|
`ends-with()`| ✓ |
|
||||||
|
`false()`| ✓ |
|
||||||
|
`floor()`| ✓ |
|
||||||
|
`format-number()`| ✗ |
|
||||||
|
`function-available()`| ✗ |
|
||||||
|
`generate-id()`| ✗ |
|
||||||
|
`id()`| ✗ |
|
||||||
|
`key()`| ✗ |
|
||||||
|
`lang()`| ✗ |
|
||||||
|
`last()`| ✓ |
|
||||||
|
`local-name()`| ✓ |
|
||||||
|
`name()`| ✓ |
|
||||||
|
`namespace-uri()`| ✓ |
|
||||||
|
`normalize-space()`| ✓ |
|
||||||
|
`not()`| ✓ |
|
||||||
|
`number()`| ✓ |
|
||||||
|
`position()`| ✓ |
|
||||||
|
`round()`| ✓ |
|
||||||
|
`starts-with()`| ✓ |
|
||||||
|
`string()`| ✓ |
|
||||||
|
`string-length()`| ✓ |
|
||||||
|
`substring()`| ✓ |
|
||||||
|
`substring-after()`| ✓ |
|
||||||
|
`substring-before()`| ✓ |
|
||||||
|
`sum()`| ✓ |
|
||||||
|
`system-property()`| ✗ |
|
||||||
|
`translate()`| ✓ |
|
||||||
|
`true()`| ✓ |
|
||||||
|
`unparsed-entity-url()` | ✗ |
|
||||||
|
|
||||||
|
Changelogs
|
||||||
|
===
|
||||||
|
|
||||||
|
2019-01-29
|
||||||
|
- improvement `normalize-space` function. [#32](https://github.com/antchfx/xpath/issues/32)
|
||||||
|
|
||||||
|
2018-12-07
|
||||||
|
- supports XPath 2.0 Sequence expressions. [#30](https://github.com/antchfx/xpath/pull/30) by [@minherz](https://github.com/minherz).
|
483
vendor/github.com/antchfx/xpath/build.go
generated
vendored
Normal file
483
vendor/github.com/antchfx/xpath/build.go
generated
vendored
Normal file
@ -0,0 +1,483 @@
|
|||||||
|
package xpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type flag int
|
||||||
|
|
||||||
|
const (
|
||||||
|
noneFlag flag = iota
|
||||||
|
filterFlag
|
||||||
|
)
|
||||||
|
|
||||||
|
// builder provides building an XPath expressions.
|
||||||
|
type builder struct {
|
||||||
|
depth int
|
||||||
|
flag flag
|
||||||
|
firstInput query
|
||||||
|
}
|
||||||
|
|
||||||
|
// axisPredicate creates a predicate to predicating for this axis node.
|
||||||
|
func axisPredicate(root *axisNode) func(NodeNavigator) bool {
|
||||||
|
// get current axix node type.
|
||||||
|
typ := ElementNode
|
||||||
|
switch root.AxeType {
|
||||||
|
case "attribute":
|
||||||
|
typ = AttributeNode
|
||||||
|
case "self", "parent":
|
||||||
|
typ = allNode
|
||||||
|
default:
|
||||||
|
switch root.Prop {
|
||||||
|
case "comment":
|
||||||
|
typ = CommentNode
|
||||||
|
case "text":
|
||||||
|
typ = TextNode
|
||||||
|
// case "processing-instruction":
|
||||||
|
// typ = ProcessingInstructionNode
|
||||||
|
case "node":
|
||||||
|
typ = allNode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nametest := root.LocalName != "" || root.Prefix != ""
|
||||||
|
predicate := func(n NodeNavigator) bool {
|
||||||
|
if typ == n.NodeType() || typ == allNode || typ == TextNode {
|
||||||
|
if nametest {
|
||||||
|
if root.LocalName == n.LocalName() && root.Prefix == n.Prefix() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return predicate
|
||||||
|
}
|
||||||
|
|
||||||
|
// processAxisNode processes a query for the XPath axis node.
|
||||||
|
func (b *builder) processAxisNode(root *axisNode) (query, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
qyInput query
|
||||||
|
qyOutput query
|
||||||
|
predicate = axisPredicate(root)
|
||||||
|
)
|
||||||
|
|
||||||
|
if root.Input == nil {
|
||||||
|
qyInput = &contextQuery{}
|
||||||
|
} else {
|
||||||
|
if root.AxeType == "child" && (root.Input.Type() == nodeAxis) {
|
||||||
|
if input := root.Input.(*axisNode); input.AxeType == "descendant-or-self" {
|
||||||
|
var qyGrandInput query
|
||||||
|
if input.Input != nil {
|
||||||
|
qyGrandInput, _ = b.processNode(input.Input)
|
||||||
|
} else {
|
||||||
|
qyGrandInput = &contextQuery{}
|
||||||
|
}
|
||||||
|
qyOutput = &descendantQuery{Input: qyGrandInput, Predicate: predicate, Self: true}
|
||||||
|
return qyOutput, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
qyInput, err = b.processNode(root.Input)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch root.AxeType {
|
||||||
|
case "ancestor":
|
||||||
|
qyOutput = &ancestorQuery{Input: qyInput, Predicate: predicate}
|
||||||
|
case "ancestor-or-self":
|
||||||
|
qyOutput = &ancestorQuery{Input: qyInput, Predicate: predicate, Self: true}
|
||||||
|
case "attribute":
|
||||||
|
qyOutput = &attributeQuery{Input: qyInput, Predicate: predicate}
|
||||||
|
case "child":
|
||||||
|
filter := func(n NodeNavigator) bool {
|
||||||
|
v := predicate(n)
|
||||||
|
switch root.Prop {
|
||||||
|
case "text":
|
||||||
|
v = v && n.NodeType() == TextNode
|
||||||
|
case "node":
|
||||||
|
v = v && (n.NodeType() == ElementNode || n.NodeType() == TextNode)
|
||||||
|
case "comment":
|
||||||
|
v = v && n.NodeType() == CommentNode
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
qyOutput = &childQuery{Input: qyInput, Predicate: filter}
|
||||||
|
case "descendant":
|
||||||
|
qyOutput = &descendantQuery{Input: qyInput, Predicate: predicate}
|
||||||
|
case "descendant-or-self":
|
||||||
|
qyOutput = &descendantQuery{Input: qyInput, Predicate: predicate, Self: true}
|
||||||
|
case "following":
|
||||||
|
qyOutput = &followingQuery{Input: qyInput, Predicate: predicate}
|
||||||
|
case "following-sibling":
|
||||||
|
qyOutput = &followingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
|
||||||
|
case "parent":
|
||||||
|
qyOutput = &parentQuery{Input: qyInput, Predicate: predicate}
|
||||||
|
case "preceding":
|
||||||
|
qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate}
|
||||||
|
case "preceding-sibling":
|
||||||
|
qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
|
||||||
|
case "self":
|
||||||
|
qyOutput = &selfQuery{Input: qyInput, Predicate: predicate}
|
||||||
|
case "namespace":
|
||||||
|
// haha,what will you do someting??
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("unknown axe type: %s", root.AxeType)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return qyOutput, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// processFilterNode builds query for the XPath filter predicate.
|
||||||
|
func (b *builder) processFilterNode(root *filterNode) (query, error) {
|
||||||
|
b.flag |= filterFlag
|
||||||
|
|
||||||
|
qyInput, err := b.processNode(root.Input)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyCond, err := b.processNode(root.Condition)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyOutput := &filterQuery{Input: qyInput, Predicate: qyCond}
|
||||||
|
return qyOutput, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// processFunctionNode processes query for the XPath function node.
|
||||||
|
func (b *builder) processFunctionNode(root *functionNode) (query, error) {
|
||||||
|
var qyOutput query
|
||||||
|
switch root.FuncName {
|
||||||
|
case "starts-with":
|
||||||
|
arg1, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
arg2, err := b.processNode(root.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{Input: b.firstInput, Func: startwithFunc(arg1, arg2)}
|
||||||
|
case "ends-with":
|
||||||
|
arg1, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
arg2, err := b.processNode(root.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{Input: b.firstInput, Func: endwithFunc(arg1, arg2)}
|
||||||
|
case "contains":
|
||||||
|
arg1, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
arg2, err := b.processNode(root.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
qyOutput = &functionQuery{Input: b.firstInput, Func: containsFunc(arg1, arg2)}
|
||||||
|
case "substring":
|
||||||
|
//substring( string , start [, length] )
|
||||||
|
if len(root.Args) < 2 {
|
||||||
|
return nil, errors.New("xpath: substring function must have at least two parameter")
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
arg1, arg2, arg3 query
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if arg1, err = b.processNode(root.Args[0]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if arg2, err = b.processNode(root.Args[1]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(root.Args) == 3 {
|
||||||
|
if arg3, err = b.processNode(root.Args[2]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{Input: b.firstInput, Func: substringFunc(arg1, arg2, arg3)}
|
||||||
|
case "substring-before", "substring-after":
|
||||||
|
//substring-xxxx( haystack, needle )
|
||||||
|
if len(root.Args) != 2 {
|
||||||
|
return nil, errors.New("xpath: substring-before function must have two parameters")
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
arg1, arg2 query
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if arg1, err = b.processNode(root.Args[0]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if arg2, err = b.processNode(root.Args[1]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{
|
||||||
|
Input: b.firstInput,
|
||||||
|
Func: substringIndFunc(arg1, arg2, root.FuncName == "substring-after"),
|
||||||
|
}
|
||||||
|
case "string-length":
|
||||||
|
// string-length( [string] )
|
||||||
|
if len(root.Args) < 1 {
|
||||||
|
return nil, errors.New("xpath: string-length function must have at least one parameter")
|
||||||
|
}
|
||||||
|
arg1, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{Input: b.firstInput, Func: stringLengthFunc(arg1)}
|
||||||
|
case "normalize-space":
|
||||||
|
if len(root.Args) == 0 {
|
||||||
|
return nil, errors.New("xpath: normalize-space function must have at least one parameter")
|
||||||
|
}
|
||||||
|
argQuery, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{Input: argQuery, Func: normalizespaceFunc}
|
||||||
|
case "translate":
|
||||||
|
//translate( string , string, string )
|
||||||
|
if len(root.Args) != 3 {
|
||||||
|
return nil, errors.New("xpath: translate function must have three parameters")
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
arg1, arg2, arg3 query
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if arg1, err = b.processNode(root.Args[0]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if arg2, err = b.processNode(root.Args[1]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if arg3, err = b.processNode(root.Args[2]); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{Input: b.firstInput, Func: translateFunc(arg1, arg2, arg3)}
|
||||||
|
case "not":
|
||||||
|
if len(root.Args) == 0 {
|
||||||
|
return nil, errors.New("xpath: not function must have at least one parameter")
|
||||||
|
}
|
||||||
|
argQuery, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{Input: argQuery, Func: notFunc}
|
||||||
|
case "name", "local-name", "namespace-uri":
|
||||||
|
inp := b.firstInput
|
||||||
|
if len(root.Args) > 1 {
|
||||||
|
return nil, fmt.Errorf("xpath: %s function must have at most one parameter", root.FuncName)
|
||||||
|
}
|
||||||
|
if len(root.Args) == 1 {
|
||||||
|
argQuery, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
inp = argQuery
|
||||||
|
}
|
||||||
|
f := &functionQuery{Input: inp}
|
||||||
|
switch root.FuncName {
|
||||||
|
case "name":
|
||||||
|
f.Func = nameFunc
|
||||||
|
case "local-name":
|
||||||
|
f.Func = localNameFunc
|
||||||
|
case "namespace-uri":
|
||||||
|
f.Func = namespaceFunc
|
||||||
|
}
|
||||||
|
qyOutput = f
|
||||||
|
case "true", "false":
|
||||||
|
val := root.FuncName == "true"
|
||||||
|
qyOutput = &functionQuery{
|
||||||
|
Input: b.firstInput,
|
||||||
|
Func: func(_ query, _ iterator) interface{} {
|
||||||
|
return val
|
||||||
|
},
|
||||||
|
}
|
||||||
|
case "last":
|
||||||
|
qyOutput = &functionQuery{Input: b.firstInput, Func: lastFunc}
|
||||||
|
case "position":
|
||||||
|
qyOutput = &functionQuery{Input: b.firstInput, Func: positionFunc}
|
||||||
|
case "boolean", "number", "string":
|
||||||
|
inp := b.firstInput
|
||||||
|
if len(root.Args) > 1 {
|
||||||
|
return nil, fmt.Errorf("xpath: %s function must have at most one parameter", root.FuncName)
|
||||||
|
}
|
||||||
|
if len(root.Args) == 1 {
|
||||||
|
argQuery, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
inp = argQuery
|
||||||
|
}
|
||||||
|
f := &functionQuery{Input: inp}
|
||||||
|
switch root.FuncName {
|
||||||
|
case "boolean":
|
||||||
|
f.Func = booleanFunc
|
||||||
|
case "string":
|
||||||
|
f.Func = stringFunc
|
||||||
|
case "number":
|
||||||
|
f.Func = numberFunc
|
||||||
|
}
|
||||||
|
qyOutput = f
|
||||||
|
case "count":
|
||||||
|
//if b.firstInput == nil {
|
||||||
|
// return nil, errors.New("xpath: expression must evaluate to node-set")
|
||||||
|
//}
|
||||||
|
if len(root.Args) == 0 {
|
||||||
|
return nil, fmt.Errorf("xpath: count(node-sets) function must with have parameters node-sets")
|
||||||
|
}
|
||||||
|
argQuery, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{Input: argQuery, Func: countFunc}
|
||||||
|
case "sum":
|
||||||
|
if len(root.Args) == 0 {
|
||||||
|
return nil, fmt.Errorf("xpath: sum(node-sets) function must with have parameters node-sets")
|
||||||
|
}
|
||||||
|
argQuery, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{Input: argQuery, Func: sumFunc}
|
||||||
|
case "ceiling", "floor", "round":
|
||||||
|
if len(root.Args) == 0 {
|
||||||
|
return nil, fmt.Errorf("xpath: ceiling(node-sets) function must with have parameters node-sets")
|
||||||
|
}
|
||||||
|
argQuery, err := b.processNode(root.Args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
f := &functionQuery{Input: argQuery}
|
||||||
|
switch root.FuncName {
|
||||||
|
case "ceiling":
|
||||||
|
f.Func = ceilingFunc
|
||||||
|
case "floor":
|
||||||
|
f.Func = floorFunc
|
||||||
|
case "round":
|
||||||
|
f.Func = roundFunc
|
||||||
|
}
|
||||||
|
qyOutput = f
|
||||||
|
case "concat":
|
||||||
|
if len(root.Args) < 2 {
|
||||||
|
return nil, fmt.Errorf("xpath: concat() must have at least two arguments")
|
||||||
|
}
|
||||||
|
var args []query
|
||||||
|
for _, v := range root.Args {
|
||||||
|
q, err := b.processNode(v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
args = append(args, q)
|
||||||
|
}
|
||||||
|
qyOutput = &functionQuery{Input: b.firstInput, Func: concatFunc(args...)}
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("not yet support this function %s()", root.FuncName)
|
||||||
|
}
|
||||||
|
return qyOutput, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *builder) processOperatorNode(root *operatorNode) (query, error) {
|
||||||
|
left, err := b.processNode(root.Left)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
right, err := b.processNode(root.Right)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var qyOutput query
|
||||||
|
switch root.Op {
|
||||||
|
case "+", "-", "div", "mod": // Numeric operator
|
||||||
|
var exprFunc func(interface{}, interface{}) interface{}
|
||||||
|
switch root.Op {
|
||||||
|
case "+":
|
||||||
|
exprFunc = plusFunc
|
||||||
|
case "-":
|
||||||
|
exprFunc = minusFunc
|
||||||
|
case "div":
|
||||||
|
exprFunc = divFunc
|
||||||
|
case "mod":
|
||||||
|
exprFunc = modFunc
|
||||||
|
}
|
||||||
|
qyOutput = &numericQuery{Left: left, Right: right, Do: exprFunc}
|
||||||
|
case "=", ">", ">=", "<", "<=", "!=":
|
||||||
|
var exprFunc func(iterator, interface{}, interface{}) interface{}
|
||||||
|
switch root.Op {
|
||||||
|
case "=":
|
||||||
|
exprFunc = eqFunc
|
||||||
|
case ">":
|
||||||
|
exprFunc = gtFunc
|
||||||
|
case ">=":
|
||||||
|
exprFunc = geFunc
|
||||||
|
case "<":
|
||||||
|
exprFunc = ltFunc
|
||||||
|
case "<=":
|
||||||
|
exprFunc = leFunc
|
||||||
|
case "!=":
|
||||||
|
exprFunc = neFunc
|
||||||
|
}
|
||||||
|
qyOutput = &logicalQuery{Left: left, Right: right, Do: exprFunc}
|
||||||
|
case "or", "and":
|
||||||
|
isOr := false
|
||||||
|
if root.Op == "or" {
|
||||||
|
isOr = true
|
||||||
|
}
|
||||||
|
qyOutput = &booleanQuery{Left: left, Right: right, IsOr: isOr}
|
||||||
|
case "|":
|
||||||
|
qyOutput = &unionQuery{Left: left, Right: right}
|
||||||
|
}
|
||||||
|
return qyOutput, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *builder) processNode(root node) (q query, err error) {
|
||||||
|
if b.depth = b.depth + 1; b.depth > 1024 {
|
||||||
|
err = errors.New("the xpath expressions is too complex")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch root.Type() {
|
||||||
|
case nodeConstantOperand:
|
||||||
|
n := root.(*operandNode)
|
||||||
|
q = &constantQuery{Val: n.Val}
|
||||||
|
case nodeRoot:
|
||||||
|
q = &contextQuery{Root: true}
|
||||||
|
case nodeAxis:
|
||||||
|
q, err = b.processAxisNode(root.(*axisNode))
|
||||||
|
b.firstInput = q
|
||||||
|
case nodeFilter:
|
||||||
|
q, err = b.processFilterNode(root.(*filterNode))
|
||||||
|
case nodeFunction:
|
||||||
|
q, err = b.processFunctionNode(root.(*functionNode))
|
||||||
|
case nodeOperator:
|
||||||
|
q, err = b.processOperatorNode(root.(*operatorNode))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// build builds a specified XPath expressions expr.
|
||||||
|
func build(expr string) (q query, err error) {
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e != nil {
|
||||||
|
switch x := e.(type) {
|
||||||
|
case string:
|
||||||
|
err = errors.New(x)
|
||||||
|
case error:
|
||||||
|
err = x
|
||||||
|
default:
|
||||||
|
err = errors.New("unknown panic")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
root := parse(expr)
|
||||||
|
b := &builder{}
|
||||||
|
return b.processNode(root)
|
||||||
|
}
|
484
vendor/github.com/antchfx/xpath/func.go
generated
vendored
Normal file
484
vendor/github.com/antchfx/xpath/func.go
generated
vendored
Normal file
@ -0,0 +1,484 @@
|
|||||||
|
package xpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The XPath function list.
|
||||||
|
|
||||||
|
func predicate(q query) func(NodeNavigator) bool {
|
||||||
|
type Predicater interface {
|
||||||
|
Test(NodeNavigator) bool
|
||||||
|
}
|
||||||
|
if p, ok := q.(Predicater); ok {
|
||||||
|
return p.Test
|
||||||
|
}
|
||||||
|
return func(NodeNavigator) bool { return true }
|
||||||
|
}
|
||||||
|
|
||||||
|
// positionFunc is a XPath Node Set functions position().
|
||||||
|
func positionFunc(q query, t iterator) interface{} {
|
||||||
|
var (
|
||||||
|
count = 1
|
||||||
|
node = t.Current()
|
||||||
|
)
|
||||||
|
test := predicate(q)
|
||||||
|
for node.MoveToPrevious() {
|
||||||
|
if test(node) {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return float64(count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// lastFunc is a XPath Node Set functions last().
|
||||||
|
func lastFunc(q query, t iterator) interface{} {
|
||||||
|
var (
|
||||||
|
count = 0
|
||||||
|
node = t.Current()
|
||||||
|
)
|
||||||
|
node.MoveToFirst()
|
||||||
|
test := predicate(q)
|
||||||
|
for {
|
||||||
|
if test(node) {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
if !node.MoveToNext() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return float64(count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// countFunc is a XPath Node Set functions count(node-set).
|
||||||
|
func countFunc(q query, t iterator) interface{} {
|
||||||
|
var count = 0
|
||||||
|
test := predicate(q)
|
||||||
|
switch typ := q.Evaluate(t).(type) {
|
||||||
|
case query:
|
||||||
|
for node := typ.Select(t); node != nil; node = typ.Select(t) {
|
||||||
|
if test(node) {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return float64(count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sumFunc is a XPath Node Set functions sum(node-set).
|
||||||
|
func sumFunc(q query, t iterator) interface{} {
|
||||||
|
var sum float64
|
||||||
|
switch typ := q.Evaluate(t).(type) {
|
||||||
|
case query:
|
||||||
|
for node := typ.Select(t); node != nil; node = typ.Select(t) {
|
||||||
|
if v, err := strconv.ParseFloat(node.Value(), 64); err == nil {
|
||||||
|
sum += v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case float64:
|
||||||
|
sum = typ
|
||||||
|
case string:
|
||||||
|
v, err := strconv.ParseFloat(typ, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic(errors.New("sum() function argument type must be a node-set or number"))
|
||||||
|
}
|
||||||
|
sum = v
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
func asNumber(t iterator, o interface{}) float64 {
|
||||||
|
switch typ := o.(type) {
|
||||||
|
case query:
|
||||||
|
node := typ.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return float64(0)
|
||||||
|
}
|
||||||
|
if v, err := strconv.ParseFloat(node.Value(), 64); err == nil {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
case float64:
|
||||||
|
return typ
|
||||||
|
case string:
|
||||||
|
v, err := strconv.ParseFloat(typ, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic(errors.New("ceiling() function argument type must be a node-set or number"))
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// ceilingFunc is a XPath Node Set functions ceiling(node-set).
|
||||||
|
func ceilingFunc(q query, t iterator) interface{} {
|
||||||
|
val := asNumber(t, q.Evaluate(t))
|
||||||
|
return math.Ceil(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// floorFunc is a XPath Node Set functions floor(node-set).
|
||||||
|
func floorFunc(q query, t iterator) interface{} {
|
||||||
|
val := asNumber(t, q.Evaluate(t))
|
||||||
|
return math.Floor(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// roundFunc is a XPath Node Set functions round(node-set).
|
||||||
|
func roundFunc(q query, t iterator) interface{} {
|
||||||
|
val := asNumber(t, q.Evaluate(t))
|
||||||
|
//return math.Round(val)
|
||||||
|
return round(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// nameFunc is a XPath functions name([node-set]).
|
||||||
|
func nameFunc(q query, t iterator) interface{} {
|
||||||
|
v := q.Select(t)
|
||||||
|
if v == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
ns := v.Prefix()
|
||||||
|
if ns == "" {
|
||||||
|
return v.LocalName()
|
||||||
|
}
|
||||||
|
return ns + ":" + v.LocalName()
|
||||||
|
}
|
||||||
|
|
||||||
|
// localNameFunc is a XPath functions local-name([node-set]).
|
||||||
|
func localNameFunc(q query, t iterator) interface{} {
|
||||||
|
v := q.Select(t)
|
||||||
|
if v == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return v.LocalName()
|
||||||
|
}
|
||||||
|
|
||||||
|
// namespaceFunc is a XPath functions namespace-uri([node-set]).
|
||||||
|
func namespaceFunc(q query, t iterator) interface{} {
|
||||||
|
v := q.Select(t)
|
||||||
|
if v == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return v.Prefix()
|
||||||
|
}
|
||||||
|
|
||||||
|
func asBool(t iterator, v interface{}) bool {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case nil:
|
||||||
|
return false
|
||||||
|
case *NodeIterator:
|
||||||
|
return v.MoveNext()
|
||||||
|
case bool:
|
||||||
|
return bool(v)
|
||||||
|
case float64:
|
||||||
|
return v != 0
|
||||||
|
case string:
|
||||||
|
return v != ""
|
||||||
|
case query:
|
||||||
|
return v.Select(t) != nil
|
||||||
|
default:
|
||||||
|
panic(fmt.Errorf("unexpected type: %T", v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func asString(t iterator, v interface{}) string {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case nil:
|
||||||
|
return ""
|
||||||
|
case bool:
|
||||||
|
if v {
|
||||||
|
return "true"
|
||||||
|
}
|
||||||
|
return "false"
|
||||||
|
case float64:
|
||||||
|
return strconv.FormatFloat(v, 'g', -1, 64)
|
||||||
|
case string:
|
||||||
|
return v
|
||||||
|
case query:
|
||||||
|
node := v.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return node.Value()
|
||||||
|
default:
|
||||||
|
panic(fmt.Errorf("unexpected type: %T", v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// booleanFunc is a XPath functions boolean([node-set]).
|
||||||
|
func booleanFunc(q query, t iterator) interface{} {
|
||||||
|
v := q.Evaluate(t)
|
||||||
|
return asBool(t, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// numberFunc is a XPath functions number([node-set]).
|
||||||
|
func numberFunc(q query, t iterator) interface{} {
|
||||||
|
v := q.Evaluate(t)
|
||||||
|
return asNumber(t, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// stringFunc is a XPath functions string([node-set]).
|
||||||
|
func stringFunc(q query, t iterator) interface{} {
|
||||||
|
v := q.Evaluate(t)
|
||||||
|
return asString(t, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// startwithFunc is a XPath functions starts-with(string, string).
|
||||||
|
func startwithFunc(arg1, arg2 query) func(query, iterator) interface{} {
|
||||||
|
return func(q query, t iterator) interface{} {
|
||||||
|
var (
|
||||||
|
m, n string
|
||||||
|
ok bool
|
||||||
|
)
|
||||||
|
switch typ := arg1.Evaluate(t).(type) {
|
||||||
|
case string:
|
||||||
|
m = typ
|
||||||
|
case query:
|
||||||
|
node := typ.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m = node.Value()
|
||||||
|
default:
|
||||||
|
panic(errors.New("starts-with() function argument type must be string"))
|
||||||
|
}
|
||||||
|
n, ok = arg2.Evaluate(t).(string)
|
||||||
|
if !ok {
|
||||||
|
panic(errors.New("starts-with() function argument type must be string"))
|
||||||
|
}
|
||||||
|
return strings.HasPrefix(m, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// endwithFunc is a XPath functions ends-with(string, string).
|
||||||
|
func endwithFunc(arg1, arg2 query) func(query, iterator) interface{} {
|
||||||
|
return func(q query, t iterator) interface{} {
|
||||||
|
var (
|
||||||
|
m, n string
|
||||||
|
ok bool
|
||||||
|
)
|
||||||
|
switch typ := arg1.Evaluate(t).(type) {
|
||||||
|
case string:
|
||||||
|
m = typ
|
||||||
|
case query:
|
||||||
|
node := typ.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m = node.Value()
|
||||||
|
default:
|
||||||
|
panic(errors.New("ends-with() function argument type must be string"))
|
||||||
|
}
|
||||||
|
n, ok = arg2.Evaluate(t).(string)
|
||||||
|
if !ok {
|
||||||
|
panic(errors.New("ends-with() function argument type must be string"))
|
||||||
|
}
|
||||||
|
return strings.HasSuffix(m, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// containsFunc is a XPath functions contains(string or @attr, string).
|
||||||
|
func containsFunc(arg1, arg2 query) func(query, iterator) interface{} {
|
||||||
|
return func(q query, t iterator) interface{} {
|
||||||
|
var (
|
||||||
|
m, n string
|
||||||
|
ok bool
|
||||||
|
)
|
||||||
|
|
||||||
|
switch typ := arg1.Evaluate(t).(type) {
|
||||||
|
case string:
|
||||||
|
m = typ
|
||||||
|
case query:
|
||||||
|
node := typ.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m = node.Value()
|
||||||
|
default:
|
||||||
|
panic(errors.New("contains() function argument type must be string"))
|
||||||
|
}
|
||||||
|
|
||||||
|
n, ok = arg2.Evaluate(t).(string)
|
||||||
|
if !ok {
|
||||||
|
panic(errors.New("contains() function argument type must be string"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Contains(m, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
regnewline = regexp.MustCompile(`[\r\n\t]`)
|
||||||
|
regseqspace = regexp.MustCompile(`\s{2,}`)
|
||||||
|
)
|
||||||
|
|
||||||
|
// normalizespaceFunc is XPath functions normalize-space(string?)
|
||||||
|
func normalizespaceFunc(q query, t iterator) interface{} {
|
||||||
|
var m string
|
||||||
|
switch typ := q.Evaluate(t).(type) {
|
||||||
|
case string:
|
||||||
|
m = typ
|
||||||
|
case query:
|
||||||
|
node := typ.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
m = node.Value()
|
||||||
|
}
|
||||||
|
m = strings.TrimSpace(m)
|
||||||
|
m = regnewline.ReplaceAllString(m, " ")
|
||||||
|
m = regseqspace.ReplaceAllString(m, " ")
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
// substringFunc is XPath functions substring function returns a part of a given string.
|
||||||
|
func substringFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} {
|
||||||
|
return func(q query, t iterator) interface{} {
|
||||||
|
var m string
|
||||||
|
switch typ := arg1.Evaluate(t).(type) {
|
||||||
|
case string:
|
||||||
|
m = typ
|
||||||
|
case query:
|
||||||
|
node := typ.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
m = node.Value()
|
||||||
|
}
|
||||||
|
|
||||||
|
var start, length float64
|
||||||
|
var ok bool
|
||||||
|
|
||||||
|
if start, ok = arg2.Evaluate(t).(float64); !ok {
|
||||||
|
panic(errors.New("substring() function first argument type must be int"))
|
||||||
|
} else if start < 1 {
|
||||||
|
panic(errors.New("substring() function first argument type must be >= 1"))
|
||||||
|
}
|
||||||
|
start--
|
||||||
|
if arg3 != nil {
|
||||||
|
if length, ok = arg3.Evaluate(t).(float64); !ok {
|
||||||
|
panic(errors.New("substring() function second argument type must be int"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (len(m) - int(start)) < int(length) {
|
||||||
|
panic(errors.New("substring() function start and length argument out of range"))
|
||||||
|
}
|
||||||
|
if length > 0 {
|
||||||
|
return m[int(start):int(length+start)]
|
||||||
|
}
|
||||||
|
return m[int(start):]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// substringIndFunc is XPath functions substring-before/substring-after function returns a part of a given string.
|
||||||
|
func substringIndFunc(arg1, arg2 query, after bool) func(query, iterator) interface{} {
|
||||||
|
return func(q query, t iterator) interface{} {
|
||||||
|
var str string
|
||||||
|
switch v := arg1.Evaluate(t).(type) {
|
||||||
|
case string:
|
||||||
|
str = v
|
||||||
|
case query:
|
||||||
|
node := v.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
str = node.Value()
|
||||||
|
}
|
||||||
|
var word string
|
||||||
|
switch v := arg2.Evaluate(t).(type) {
|
||||||
|
case string:
|
||||||
|
word = v
|
||||||
|
case query:
|
||||||
|
node := v.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
word = node.Value()
|
||||||
|
}
|
||||||
|
if word == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
i := strings.Index(str, word)
|
||||||
|
if i < 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if after {
|
||||||
|
return str[i+len(word):]
|
||||||
|
}
|
||||||
|
return str[:i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// stringLengthFunc is XPATH string-length( [string] ) function that returns a number
|
||||||
|
// equal to the number of characters in a given string.
|
||||||
|
func stringLengthFunc(arg1 query) func(query, iterator) interface{} {
|
||||||
|
return func(q query, t iterator) interface{} {
|
||||||
|
switch v := arg1.Evaluate(t).(type) {
|
||||||
|
case string:
|
||||||
|
return float64(len(v))
|
||||||
|
case query:
|
||||||
|
node := v.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return float64(len(node.Value()))
|
||||||
|
}
|
||||||
|
return float64(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// translateFunc is XPath functions translate() function returns a replaced string.
|
||||||
|
func translateFunc(arg1, arg2, arg3 query) func(query, iterator) interface{} {
|
||||||
|
return func(q query, t iterator) interface{} {
|
||||||
|
str := asString(t, arg1.Evaluate(t))
|
||||||
|
src := asString(t, arg2.Evaluate(t))
|
||||||
|
dst := asString(t, arg3.Evaluate(t))
|
||||||
|
|
||||||
|
var replace []string
|
||||||
|
for i, s := range src {
|
||||||
|
d := ""
|
||||||
|
if i < len(dst) {
|
||||||
|
d = string(dst[i])
|
||||||
|
}
|
||||||
|
replace = append(replace, string(s), d)
|
||||||
|
}
|
||||||
|
return strings.NewReplacer(replace...).Replace(str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// notFunc is XPATH functions not(expression) function operation.
|
||||||
|
func notFunc(q query, t iterator) interface{} {
|
||||||
|
switch v := q.Evaluate(t).(type) {
|
||||||
|
case bool:
|
||||||
|
return !v
|
||||||
|
case query:
|
||||||
|
node := v.Select(t)
|
||||||
|
return node == nil
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// concatFunc is the concat function concatenates two or more
|
||||||
|
// strings and returns the resulting string.
|
||||||
|
// concat( string1 , string2 [, stringn]* )
|
||||||
|
func concatFunc(args ...query) func(query, iterator) interface{} {
|
||||||
|
return func(q query, t iterator) interface{} {
|
||||||
|
var a []string
|
||||||
|
for _, v := range args {
|
||||||
|
switch v := v.Evaluate(t).(type) {
|
||||||
|
case string:
|
||||||
|
a = append(a, v)
|
||||||
|
case query:
|
||||||
|
node := v.Select(t)
|
||||||
|
if node != nil {
|
||||||
|
a = append(a, node.Value())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strings.Join(a, "")
|
||||||
|
}
|
||||||
|
}
|
9
vendor/github.com/antchfx/xpath/func_go110.go
generated
vendored
Normal file
9
vendor/github.com/antchfx/xpath/func_go110.go
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// +build go1.10
|
||||||
|
|
||||||
|
package xpath
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func round(f float64) int {
|
||||||
|
return int(math.Round(f))
|
||||||
|
}
|
15
vendor/github.com/antchfx/xpath/func_pre_go110.go
generated
vendored
Normal file
15
vendor/github.com/antchfx/xpath/func_pre_go110.go
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// +build !go1.10
|
||||||
|
|
||||||
|
package xpath
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
// math.Round() is supported by Go 1.10+,
|
||||||
|
// This method just compatible for version <1.10.
|
||||||
|
// https://github.com/golang/go/issues/20100
|
||||||
|
func round(f float64) int {
|
||||||
|
if math.Abs(f) < 0.5 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return int(f + math.Copysign(0.5, f))
|
||||||
|
}
|
295
vendor/github.com/antchfx/xpath/operator.go
generated
vendored
Normal file
295
vendor/github.com/antchfx/xpath/operator.go
generated
vendored
Normal file
@ -0,0 +1,295 @@
|
|||||||
|
package xpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The XPath number operator function list.
|
||||||
|
|
||||||
|
// valueType is a return value type.
|
||||||
|
type valueType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
booleanType valueType = iota
|
||||||
|
numberType
|
||||||
|
stringType
|
||||||
|
nodeSetType
|
||||||
|
)
|
||||||
|
|
||||||
|
func getValueType(i interface{}) valueType {
|
||||||
|
v := reflect.ValueOf(i)
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Float64:
|
||||||
|
return numberType
|
||||||
|
case reflect.String:
|
||||||
|
return stringType
|
||||||
|
case reflect.Bool:
|
||||||
|
return booleanType
|
||||||
|
default:
|
||||||
|
if _, ok := i.(query); ok {
|
||||||
|
return nodeSetType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
panic(fmt.Errorf("xpath unknown value type: %v", v.Kind()))
|
||||||
|
}
|
||||||
|
|
||||||
|
type logical func(iterator, string, interface{}, interface{}) bool
|
||||||
|
|
||||||
|
var logicalFuncs = [][]logical{
|
||||||
|
{cmpBooleanBoolean, nil, nil, nil},
|
||||||
|
{nil, cmpNumericNumeric, cmpNumericString, cmpNumericNodeSet},
|
||||||
|
{nil, cmpStringNumeric, cmpStringString, cmpStringNodeSet},
|
||||||
|
{nil, cmpNodeSetNumeric, cmpNodeSetString, cmpNodeSetNodeSet},
|
||||||
|
}
|
||||||
|
|
||||||
|
// number vs number
|
||||||
|
func cmpNumberNumberF(op string, a, b float64) bool {
|
||||||
|
switch op {
|
||||||
|
case "=":
|
||||||
|
return a == b
|
||||||
|
case ">":
|
||||||
|
return a > b
|
||||||
|
case "<":
|
||||||
|
return a < b
|
||||||
|
case ">=":
|
||||||
|
return a >= b
|
||||||
|
case "<=":
|
||||||
|
return a <= b
|
||||||
|
case "!=":
|
||||||
|
return a != b
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// string vs string
|
||||||
|
func cmpStringStringF(op string, a, b string) bool {
|
||||||
|
switch op {
|
||||||
|
case "=":
|
||||||
|
return a == b
|
||||||
|
case ">":
|
||||||
|
return a > b
|
||||||
|
case "<":
|
||||||
|
return a < b
|
||||||
|
case ">=":
|
||||||
|
return a >= b
|
||||||
|
case "<=":
|
||||||
|
return a <= b
|
||||||
|
case "!=":
|
||||||
|
return a != b
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpBooleanBooleanF(op string, a, b bool) bool {
|
||||||
|
switch op {
|
||||||
|
case "or":
|
||||||
|
return a || b
|
||||||
|
case "and":
|
||||||
|
return a && b
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpNumericNumeric(t iterator, op string, m, n interface{}) bool {
|
||||||
|
a := m.(float64)
|
||||||
|
b := n.(float64)
|
||||||
|
return cmpNumberNumberF(op, a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpNumericString(t iterator, op string, m, n interface{}) bool {
|
||||||
|
a := m.(float64)
|
||||||
|
b := n.(string)
|
||||||
|
num, err := strconv.ParseFloat(b, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return cmpNumberNumberF(op, a, num)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpNumericNodeSet(t iterator, op string, m, n interface{}) bool {
|
||||||
|
a := m.(float64)
|
||||||
|
b := n.(query)
|
||||||
|
|
||||||
|
for {
|
||||||
|
node := b.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
num, err := strconv.ParseFloat(node.Value(), 64)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if cmpNumberNumberF(op, a, num) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpNodeSetNumeric(t iterator, op string, m, n interface{}) bool {
|
||||||
|
a := m.(query)
|
||||||
|
b := n.(float64)
|
||||||
|
for {
|
||||||
|
node := a.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
num, err := strconv.ParseFloat(node.Value(), 64)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if cmpNumberNumberF(op, num, b) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpNodeSetString(t iterator, op string, m, n interface{}) bool {
|
||||||
|
a := m.(query)
|
||||||
|
b := n.(string)
|
||||||
|
for {
|
||||||
|
node := a.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if cmpStringStringF(op, b, node.Value()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpNodeSetNodeSet(t iterator, op string, m, n interface{}) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpStringNumeric(t iterator, op string, m, n interface{}) bool {
|
||||||
|
a := m.(string)
|
||||||
|
b := n.(float64)
|
||||||
|
num, err := strconv.ParseFloat(a, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return cmpNumberNumberF(op, b, num)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpStringString(t iterator, op string, m, n interface{}) bool {
|
||||||
|
a := m.(string)
|
||||||
|
b := n.(string)
|
||||||
|
return cmpStringStringF(op, a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpStringNodeSet(t iterator, op string, m, n interface{}) bool {
|
||||||
|
a := m.(string)
|
||||||
|
b := n.(query)
|
||||||
|
for {
|
||||||
|
node := b.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if cmpStringStringF(op, a, node.Value()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmpBooleanBoolean(t iterator, op string, m, n interface{}) bool {
|
||||||
|
a := m.(bool)
|
||||||
|
b := n.(bool)
|
||||||
|
return cmpBooleanBooleanF(op, a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// eqFunc is an `=` operator.
|
||||||
|
func eqFunc(t iterator, m, n interface{}) interface{} {
|
||||||
|
t1 := getValueType(m)
|
||||||
|
t2 := getValueType(n)
|
||||||
|
return logicalFuncs[t1][t2](t, "=", m, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// gtFunc is an `>` operator.
|
||||||
|
func gtFunc(t iterator, m, n interface{}) interface{} {
|
||||||
|
t1 := getValueType(m)
|
||||||
|
t2 := getValueType(n)
|
||||||
|
return logicalFuncs[t1][t2](t, ">", m, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// geFunc is an `>=` operator.
|
||||||
|
func geFunc(t iterator, m, n interface{}) interface{} {
|
||||||
|
t1 := getValueType(m)
|
||||||
|
t2 := getValueType(n)
|
||||||
|
return logicalFuncs[t1][t2](t, ">=", m, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ltFunc is an `<` operator.
|
||||||
|
func ltFunc(t iterator, m, n interface{}) interface{} {
|
||||||
|
t1 := getValueType(m)
|
||||||
|
t2 := getValueType(n)
|
||||||
|
return logicalFuncs[t1][t2](t, "<", m, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// leFunc is an `<=` operator.
|
||||||
|
func leFunc(t iterator, m, n interface{}) interface{} {
|
||||||
|
t1 := getValueType(m)
|
||||||
|
t2 := getValueType(n)
|
||||||
|
return logicalFuncs[t1][t2](t, "<=", m, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// neFunc is an `!=` operator.
|
||||||
|
func neFunc(t iterator, m, n interface{}) interface{} {
|
||||||
|
t1 := getValueType(m)
|
||||||
|
t2 := getValueType(n)
|
||||||
|
return logicalFuncs[t1][t2](t, "!=", m, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// orFunc is an `or` operator.
|
||||||
|
var orFunc = func(t iterator, m, n interface{}) interface{} {
|
||||||
|
t1 := getValueType(m)
|
||||||
|
t2 := getValueType(n)
|
||||||
|
return logicalFuncs[t1][t2](t, "or", m, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func numericExpr(m, n interface{}, cb func(float64, float64) float64) float64 {
|
||||||
|
typ := reflect.TypeOf(float64(0))
|
||||||
|
a := reflect.ValueOf(m).Convert(typ)
|
||||||
|
b := reflect.ValueOf(n).Convert(typ)
|
||||||
|
return cb(a.Float(), b.Float())
|
||||||
|
}
|
||||||
|
|
||||||
|
// plusFunc is an `+` operator.
|
||||||
|
var plusFunc = func(m, n interface{}) interface{} {
|
||||||
|
return numericExpr(m, n, func(a, b float64) float64 {
|
||||||
|
return a + b
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// minusFunc is an `-` operator.
|
||||||
|
var minusFunc = func(m, n interface{}) interface{} {
|
||||||
|
return numericExpr(m, n, func(a, b float64) float64 {
|
||||||
|
return a - b
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// mulFunc is an `*` operator.
|
||||||
|
var mulFunc = func(m, n interface{}) interface{} {
|
||||||
|
return numericExpr(m, n, func(a, b float64) float64 {
|
||||||
|
return a * b
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// divFunc is an `DIV` operator.
|
||||||
|
var divFunc = func(m, n interface{}) interface{} {
|
||||||
|
return numericExpr(m, n, func(a, b float64) float64 {
|
||||||
|
return a / b
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// modFunc is an 'MOD' operator.
|
||||||
|
var modFunc = func(m, n interface{}) interface{} {
|
||||||
|
return numericExpr(m, n, func(a, b float64) float64 {
|
||||||
|
return float64(int(a) % int(b))
|
||||||
|
})
|
||||||
|
}
|
1186
vendor/github.com/antchfx/xpath/parse.go
generated
vendored
Normal file
1186
vendor/github.com/antchfx/xpath/parse.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
824
vendor/github.com/antchfx/xpath/query.go
generated
vendored
Normal file
824
vendor/github.com/antchfx/xpath/query.go
generated
vendored
Normal file
@ -0,0 +1,824 @@
|
|||||||
|
package xpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"hash/fnv"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
type iterator interface {
|
||||||
|
Current() NodeNavigator
|
||||||
|
}
|
||||||
|
|
||||||
|
// An XPath query interface.
|
||||||
|
type query interface {
|
||||||
|
// Select traversing iterator returns a query matched node NodeNavigator.
|
||||||
|
Select(iterator) NodeNavigator
|
||||||
|
|
||||||
|
// Evaluate evaluates query and returns values of the current query.
|
||||||
|
Evaluate(iterator) interface{}
|
||||||
|
|
||||||
|
Clone() query
|
||||||
|
}
|
||||||
|
|
||||||
|
// contextQuery is returns current node on the iterator object query.
|
||||||
|
type contextQuery struct {
|
||||||
|
count int
|
||||||
|
Root bool // Moving to root-level node in the current context iterator.
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *contextQuery) Select(t iterator) (n NodeNavigator) {
|
||||||
|
if c.count == 0 {
|
||||||
|
c.count++
|
||||||
|
n = t.Current().Copy()
|
||||||
|
if c.Root {
|
||||||
|
n.MoveToRoot()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *contextQuery) Evaluate(iterator) interface{} {
|
||||||
|
c.count = 0
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *contextQuery) Clone() query {
|
||||||
|
return &contextQuery{count: 0, Root: c.Root}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ancestorQuery is an XPath ancestor node query.(ancestor::*|ancestor-self::*)
|
||||||
|
type ancestorQuery struct {
|
||||||
|
iterator func() NodeNavigator
|
||||||
|
|
||||||
|
Self bool
|
||||||
|
Input query
|
||||||
|
Predicate func(NodeNavigator) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ancestorQuery) Select(t iterator) NodeNavigator {
|
||||||
|
for {
|
||||||
|
if a.iterator == nil {
|
||||||
|
node := a.Input.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
first := true
|
||||||
|
a.iterator = func() NodeNavigator {
|
||||||
|
if first && a.Self {
|
||||||
|
first = false
|
||||||
|
if a.Predicate(node) {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for node.MoveToParent() {
|
||||||
|
if !a.Predicate(node) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if node := a.iterator(); node != nil {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
a.iterator = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ancestorQuery) Evaluate(t iterator) interface{} {
|
||||||
|
a.Input.Evaluate(t)
|
||||||
|
a.iterator = nil
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ancestorQuery) Test(n NodeNavigator) bool {
|
||||||
|
return a.Predicate(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ancestorQuery) Clone() query {
|
||||||
|
return &ancestorQuery{Self: a.Self, Input: a.Input.Clone(), Predicate: a.Predicate}
|
||||||
|
}
|
||||||
|
|
||||||
|
// attributeQuery is an XPath attribute node query.(@*)
|
||||||
|
type attributeQuery struct {
|
||||||
|
iterator func() NodeNavigator
|
||||||
|
|
||||||
|
Input query
|
||||||
|
Predicate func(NodeNavigator) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *attributeQuery) Select(t iterator) NodeNavigator {
|
||||||
|
for {
|
||||||
|
if a.iterator == nil {
|
||||||
|
node := a.Input.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
a.iterator = func() NodeNavigator {
|
||||||
|
for {
|
||||||
|
onAttr := node.MoveToNextAttribute()
|
||||||
|
if !onAttr {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if a.Predicate(node) {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if node := a.iterator(); node != nil {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
a.iterator = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *attributeQuery) Evaluate(t iterator) interface{} {
|
||||||
|
a.Input.Evaluate(t)
|
||||||
|
a.iterator = nil
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *attributeQuery) Test(n NodeNavigator) bool {
|
||||||
|
return a.Predicate(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *attributeQuery) Clone() query {
|
||||||
|
return &attributeQuery{Input: a.Input.Clone(), Predicate: a.Predicate}
|
||||||
|
}
|
||||||
|
|
||||||
|
// childQuery is an XPath child node query.(child::*)
|
||||||
|
type childQuery struct {
|
||||||
|
posit int
|
||||||
|
iterator func() NodeNavigator
|
||||||
|
|
||||||
|
Input query
|
||||||
|
Predicate func(NodeNavigator) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *childQuery) Select(t iterator) NodeNavigator {
|
||||||
|
for {
|
||||||
|
if c.iterator == nil {
|
||||||
|
c.posit = 0
|
||||||
|
node := c.Input.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
first := true
|
||||||
|
c.iterator = func() NodeNavigator {
|
||||||
|
for {
|
||||||
|
if (first && !node.MoveToChild()) || (!first && !node.MoveToNext()) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
first = false
|
||||||
|
if c.Predicate(node) {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if node := c.iterator(); node != nil {
|
||||||
|
c.posit++
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
c.iterator = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *childQuery) Evaluate(t iterator) interface{} {
|
||||||
|
c.Input.Evaluate(t)
|
||||||
|
c.iterator = nil
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *childQuery) Test(n NodeNavigator) bool {
|
||||||
|
return c.Predicate(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *childQuery) Clone() query {
|
||||||
|
return &childQuery{Input: c.Input.Clone(), Predicate: c.Predicate}
|
||||||
|
}
|
||||||
|
|
||||||
|
// position returns a position of current NodeNavigator.
|
||||||
|
func (c *childQuery) position() int {
|
||||||
|
return c.posit
|
||||||
|
}
|
||||||
|
|
||||||
|
// descendantQuery is an XPath descendant node query.(descendant::* | descendant-or-self::*)
|
||||||
|
type descendantQuery struct {
|
||||||
|
iterator func() NodeNavigator
|
||||||
|
posit int
|
||||||
|
|
||||||
|
Self bool
|
||||||
|
Input query
|
||||||
|
Predicate func(NodeNavigator) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *descendantQuery) Select(t iterator) NodeNavigator {
|
||||||
|
for {
|
||||||
|
if d.iterator == nil {
|
||||||
|
d.posit = 0
|
||||||
|
node := d.Input.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
level := 0
|
||||||
|
first := true
|
||||||
|
d.iterator = func() NodeNavigator {
|
||||||
|
if first && d.Self {
|
||||||
|
first = false
|
||||||
|
if d.Predicate(node) {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if node.MoveToChild() {
|
||||||
|
level++
|
||||||
|
} else {
|
||||||
|
for {
|
||||||
|
if level == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if node.MoveToNext() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
node.MoveToParent()
|
||||||
|
level--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if d.Predicate(node) {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if node := d.iterator(); node != nil {
|
||||||
|
d.posit++
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
d.iterator = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *descendantQuery) Evaluate(t iterator) interface{} {
|
||||||
|
d.Input.Evaluate(t)
|
||||||
|
d.iterator = nil
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *descendantQuery) Test(n NodeNavigator) bool {
|
||||||
|
return d.Predicate(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// position returns a position of current NodeNavigator.
|
||||||
|
func (d *descendantQuery) position() int {
|
||||||
|
return d.posit
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *descendantQuery) Clone() query {
|
||||||
|
return &descendantQuery{Self: d.Self, Input: d.Input.Clone(), Predicate: d.Predicate}
|
||||||
|
}
|
||||||
|
|
||||||
|
// followingQuery is an XPath following node query.(following::*|following-sibling::*)
|
||||||
|
type followingQuery struct {
|
||||||
|
iterator func() NodeNavigator
|
||||||
|
|
||||||
|
Input query
|
||||||
|
Sibling bool // The matching sibling node of current node.
|
||||||
|
Predicate func(NodeNavigator) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *followingQuery) Select(t iterator) NodeNavigator {
|
||||||
|
for {
|
||||||
|
if f.iterator == nil {
|
||||||
|
node := f.Input.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
if f.Sibling {
|
||||||
|
f.iterator = func() NodeNavigator {
|
||||||
|
for {
|
||||||
|
if !node.MoveToNext() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if f.Predicate(node) {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var q query // descendant query
|
||||||
|
f.iterator = func() NodeNavigator {
|
||||||
|
for {
|
||||||
|
if q == nil {
|
||||||
|
for !node.MoveToNext() {
|
||||||
|
if !node.MoveToParent() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
q = &descendantQuery{
|
||||||
|
Self: true,
|
||||||
|
Input: &contextQuery{},
|
||||||
|
Predicate: f.Predicate,
|
||||||
|
}
|
||||||
|
t.Current().MoveTo(node)
|
||||||
|
}
|
||||||
|
if node := q.Select(t); node != nil {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
q = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if node := f.iterator(); node != nil {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
f.iterator = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *followingQuery) Evaluate(t iterator) interface{} {
|
||||||
|
f.Input.Evaluate(t)
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *followingQuery) Test(n NodeNavigator) bool {
|
||||||
|
return f.Predicate(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *followingQuery) Clone() query {
|
||||||
|
return &followingQuery{Input: f.Input.Clone(), Sibling: f.Sibling, Predicate: f.Predicate}
|
||||||
|
}
|
||||||
|
|
||||||
|
// precedingQuery is an XPath preceding node query.(preceding::*)
|
||||||
|
type precedingQuery struct {
|
||||||
|
iterator func() NodeNavigator
|
||||||
|
Input query
|
||||||
|
Sibling bool // The matching sibling node of current node.
|
||||||
|
Predicate func(NodeNavigator) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *precedingQuery) Select(t iterator) NodeNavigator {
|
||||||
|
for {
|
||||||
|
if p.iterator == nil {
|
||||||
|
node := p.Input.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
if p.Sibling {
|
||||||
|
p.iterator = func() NodeNavigator {
|
||||||
|
for {
|
||||||
|
for !node.MoveToPrevious() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if p.Predicate(node) {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var q query
|
||||||
|
p.iterator = func() NodeNavigator {
|
||||||
|
for {
|
||||||
|
if q == nil {
|
||||||
|
for !node.MoveToPrevious() {
|
||||||
|
if !node.MoveToParent() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
q = &descendantQuery{
|
||||||
|
Self: true,
|
||||||
|
Input: &contextQuery{},
|
||||||
|
Predicate: p.Predicate,
|
||||||
|
}
|
||||||
|
t.Current().MoveTo(node)
|
||||||
|
}
|
||||||
|
if node := q.Select(t); node != nil {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
q = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if node := p.iterator(); node != nil {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
p.iterator = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *precedingQuery) Evaluate(t iterator) interface{} {
|
||||||
|
p.Input.Evaluate(t)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *precedingQuery) Test(n NodeNavigator) bool {
|
||||||
|
return p.Predicate(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *precedingQuery) Clone() query {
|
||||||
|
return &precedingQuery{Input: p.Input.Clone(), Sibling: p.Sibling, Predicate: p.Predicate}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parentQuery is an XPath parent node query.(parent::*)
|
||||||
|
type parentQuery struct {
|
||||||
|
Input query
|
||||||
|
Predicate func(NodeNavigator) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parentQuery) Select(t iterator) NodeNavigator {
|
||||||
|
for {
|
||||||
|
node := p.Input.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
if node.MoveToParent() && p.Predicate(node) {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parentQuery) Evaluate(t iterator) interface{} {
|
||||||
|
p.Input.Evaluate(t)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parentQuery) Clone() query {
|
||||||
|
return &parentQuery{Input: p.Input.Clone(), Predicate: p.Predicate}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *parentQuery) Test(n NodeNavigator) bool {
|
||||||
|
return p.Predicate(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// selfQuery is an Self node query.(self::*)
|
||||||
|
type selfQuery struct {
|
||||||
|
Input query
|
||||||
|
Predicate func(NodeNavigator) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *selfQuery) Select(t iterator) NodeNavigator {
|
||||||
|
for {
|
||||||
|
node := s.Input.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.Predicate(node) {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *selfQuery) Evaluate(t iterator) interface{} {
|
||||||
|
s.Input.Evaluate(t)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *selfQuery) Test(n NodeNavigator) bool {
|
||||||
|
return s.Predicate(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *selfQuery) Clone() query {
|
||||||
|
return &selfQuery{Input: s.Input.Clone(), Predicate: s.Predicate}
|
||||||
|
}
|
||||||
|
|
||||||
|
// filterQuery is an XPath query for predicate filter.
|
||||||
|
type filterQuery struct {
|
||||||
|
Input query
|
||||||
|
Predicate query
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filterQuery) do(t iterator) bool {
|
||||||
|
val := reflect.ValueOf(f.Predicate.Evaluate(t))
|
||||||
|
switch val.Kind() {
|
||||||
|
case reflect.Bool:
|
||||||
|
return val.Bool()
|
||||||
|
case reflect.String:
|
||||||
|
return len(val.String()) > 0
|
||||||
|
case reflect.Float64:
|
||||||
|
pt := float64(getNodePosition(f.Input))
|
||||||
|
return int(val.Float()) == int(pt)
|
||||||
|
default:
|
||||||
|
if q, ok := f.Predicate.(query); ok {
|
||||||
|
return q.Select(t) != nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filterQuery) Select(t iterator) NodeNavigator {
|
||||||
|
for {
|
||||||
|
node := f.Input.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
//fmt.Println(node.LocalName())
|
||||||
|
|
||||||
|
t.Current().MoveTo(node)
|
||||||
|
if f.do(t) {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filterQuery) Evaluate(t iterator) interface{} {
|
||||||
|
f.Input.Evaluate(t)
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filterQuery) Clone() query {
|
||||||
|
return &filterQuery{Input: f.Input.Clone(), Predicate: f.Predicate.Clone()}
|
||||||
|
}
|
||||||
|
|
||||||
|
// functionQuery is an XPath function that call a function to returns
|
||||||
|
// value of current NodeNavigator node.
|
||||||
|
type functionQuery struct {
|
||||||
|
Input query // Node Set
|
||||||
|
Func func(query, iterator) interface{} // The xpath function.
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *functionQuery) Select(t iterator) NodeNavigator {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluate call a specified function that will returns the
|
||||||
|
// following value type: number,string,boolean.
|
||||||
|
func (f *functionQuery) Evaluate(t iterator) interface{} {
|
||||||
|
return f.Func(f.Input, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *functionQuery) Clone() query {
|
||||||
|
return &functionQuery{Input: f.Input.Clone(), Func: f.Func}
|
||||||
|
}
|
||||||
|
|
||||||
|
// constantQuery is an XPath constant operand.
|
||||||
|
type constantQuery struct {
|
||||||
|
Val interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *constantQuery) Select(t iterator) NodeNavigator {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *constantQuery) Evaluate(t iterator) interface{} {
|
||||||
|
return c.Val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *constantQuery) Clone() query {
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// logicalQuery is an XPath logical expression.
|
||||||
|
type logicalQuery struct {
|
||||||
|
Left, Right query
|
||||||
|
|
||||||
|
Do func(iterator, interface{}, interface{}) interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logicalQuery) Select(t iterator) NodeNavigator {
|
||||||
|
// When a XPath expr is logical expression.
|
||||||
|
node := t.Current().Copy()
|
||||||
|
val := l.Evaluate(t)
|
||||||
|
switch val.(type) {
|
||||||
|
case bool:
|
||||||
|
if val.(bool) == true {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logicalQuery) Evaluate(t iterator) interface{} {
|
||||||
|
m := l.Left.Evaluate(t)
|
||||||
|
n := l.Right.Evaluate(t)
|
||||||
|
return l.Do(t, m, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logicalQuery) Clone() query {
|
||||||
|
return &logicalQuery{Left: l.Left.Clone(), Right: l.Right.Clone(), Do: l.Do}
|
||||||
|
}
|
||||||
|
|
||||||
|
// numericQuery is an XPath numeric operator expression.
|
||||||
|
type numericQuery struct {
|
||||||
|
Left, Right query
|
||||||
|
|
||||||
|
Do func(interface{}, interface{}) interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *numericQuery) Select(t iterator) NodeNavigator {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *numericQuery) Evaluate(t iterator) interface{} {
|
||||||
|
m := n.Left.Evaluate(t)
|
||||||
|
k := n.Right.Evaluate(t)
|
||||||
|
return n.Do(m, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *numericQuery) Clone() query {
|
||||||
|
return &numericQuery{Left: n.Left.Clone(), Right: n.Right.Clone(), Do: n.Do}
|
||||||
|
}
|
||||||
|
|
||||||
|
type booleanQuery struct {
|
||||||
|
IsOr bool
|
||||||
|
Left, Right query
|
||||||
|
iterator func() NodeNavigator
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *booleanQuery) Select(t iterator) NodeNavigator {
|
||||||
|
if b.iterator == nil {
|
||||||
|
var list []NodeNavigator
|
||||||
|
i := 0
|
||||||
|
root := t.Current().Copy()
|
||||||
|
if b.IsOr {
|
||||||
|
for {
|
||||||
|
node := b.Left.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
list = append(list, node)
|
||||||
|
}
|
||||||
|
t.Current().MoveTo(root)
|
||||||
|
for {
|
||||||
|
node := b.Right.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
list = append(list, node)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var m []NodeNavigator
|
||||||
|
var n []NodeNavigator
|
||||||
|
for {
|
||||||
|
node := b.Left.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
list = append(m, node)
|
||||||
|
}
|
||||||
|
t.Current().MoveTo(root)
|
||||||
|
for {
|
||||||
|
node := b.Right.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
node = node.Copy()
|
||||||
|
list = append(n, node)
|
||||||
|
}
|
||||||
|
for _, k := range m {
|
||||||
|
for _, j := range n {
|
||||||
|
if k == j {
|
||||||
|
list = append(list, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b.iterator = func() NodeNavigator {
|
||||||
|
if i >= len(list) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
node := list[i]
|
||||||
|
i++
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b.iterator()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *booleanQuery) Evaluate(t iterator) interface{} {
|
||||||
|
m := b.Left.Evaluate(t)
|
||||||
|
left := asBool(t, m)
|
||||||
|
if b.IsOr && left {
|
||||||
|
return true
|
||||||
|
} else if !b.IsOr && !left {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m = b.Right.Evaluate(t)
|
||||||
|
return asBool(t, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *booleanQuery) Clone() query {
|
||||||
|
return &booleanQuery{IsOr: b.IsOr, Left: b.Left.Clone(), Right: b.Right.Clone()}
|
||||||
|
}
|
||||||
|
|
||||||
|
type unionQuery struct {
|
||||||
|
Left, Right query
|
||||||
|
iterator func() NodeNavigator
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *unionQuery) Select(t iterator) NodeNavigator {
|
||||||
|
if u.iterator == nil {
|
||||||
|
var m = make(map[uint64]NodeNavigator)
|
||||||
|
root := t.Current().Copy()
|
||||||
|
for {
|
||||||
|
node := u.Left.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
code := getHashCode(node.Copy())
|
||||||
|
if _, ok := m[code]; !ok {
|
||||||
|
m[code] = node.Copy()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Current().MoveTo(root)
|
||||||
|
for {
|
||||||
|
node := u.Right.Select(t)
|
||||||
|
if node == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
code := getHashCode(node.Copy())
|
||||||
|
if _, ok := m[code]; !ok {
|
||||||
|
m[code] = node.Copy()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list := make([]NodeNavigator, len(m))
|
||||||
|
var i int
|
||||||
|
for _, v := range m {
|
||||||
|
list[i] = v
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
i = 0
|
||||||
|
u.iterator = func() NodeNavigator {
|
||||||
|
if i >= len(list) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
node := list[i]
|
||||||
|
i++
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return u.iterator()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *unionQuery) Evaluate(t iterator) interface{} {
|
||||||
|
u.iterator = nil
|
||||||
|
u.Left.Evaluate(t)
|
||||||
|
u.Right.Evaluate(t)
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *unionQuery) Clone() query {
|
||||||
|
return &unionQuery{Left: u.Left.Clone(), Right: u.Right.Clone()}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHashCode(n NodeNavigator) uint64 {
|
||||||
|
var sb bytes.Buffer
|
||||||
|
switch n.NodeType() {
|
||||||
|
case AttributeNode, TextNode, CommentNode:
|
||||||
|
sb.WriteString(fmt.Sprintf("%s=%s", n.LocalName(), n.Value()))
|
||||||
|
if n.MoveToParent() {
|
||||||
|
sb.WriteString(n.LocalName())
|
||||||
|
}
|
||||||
|
case ElementNode:
|
||||||
|
sb.WriteString(n.Prefix() + n.LocalName())
|
||||||
|
d := 1
|
||||||
|
for n.MoveToPrevious() {
|
||||||
|
d++
|
||||||
|
}
|
||||||
|
sb.WriteString(fmt.Sprintf("-%d", d))
|
||||||
|
|
||||||
|
for n.MoveToParent() {
|
||||||
|
d = 1
|
||||||
|
for n.MoveToPrevious() {
|
||||||
|
d++
|
||||||
|
}
|
||||||
|
sb.WriteString(fmt.Sprintf("-%d", d))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h := fnv.New64a()
|
||||||
|
h.Write([]byte(sb.String()))
|
||||||
|
return h.Sum64()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNodePosition(q query) int {
|
||||||
|
type Position interface {
|
||||||
|
position() int
|
||||||
|
}
|
||||||
|
if count, ok := q.(Position); ok {
|
||||||
|
return count.position()
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
}
|
157
vendor/github.com/antchfx/xpath/xpath.go
generated
vendored
Normal file
157
vendor/github.com/antchfx/xpath/xpath.go
generated
vendored
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
package xpath
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NodeType represents a type of XPath node.
|
||||||
|
type NodeType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// RootNode is a root node of the XML document or node tree.
|
||||||
|
RootNode NodeType = iota
|
||||||
|
|
||||||
|
// ElementNode is an element, such as <element>.
|
||||||
|
ElementNode
|
||||||
|
|
||||||
|
// AttributeNode is an attribute, such as id='123'.
|
||||||
|
AttributeNode
|
||||||
|
|
||||||
|
// TextNode is the text content of a node.
|
||||||
|
TextNode
|
||||||
|
|
||||||
|
// CommentNode is a comment node, such as <!-- my comment -->
|
||||||
|
CommentNode
|
||||||
|
|
||||||
|
// allNode is any types of node, used by xpath package only to predicate match.
|
||||||
|
allNode
|
||||||
|
)
|
||||||
|
|
||||||
|
// NodeNavigator provides cursor model for navigating XML data.
|
||||||
|
type NodeNavigator interface {
|
||||||
|
// NodeType returns the XPathNodeType of the current node.
|
||||||
|
NodeType() NodeType
|
||||||
|
|
||||||
|
// LocalName gets the Name of the current node.
|
||||||
|
LocalName() string
|
||||||
|
|
||||||
|
// Prefix returns namespace prefix associated with the current node.
|
||||||
|
Prefix() string
|
||||||
|
|
||||||
|
// Value gets the value of current node.
|
||||||
|
Value() string
|
||||||
|
|
||||||
|
// Copy does a deep copy of the NodeNavigator and all its components.
|
||||||
|
Copy() NodeNavigator
|
||||||
|
|
||||||
|
// MoveToRoot moves the NodeNavigator to the root node of the current node.
|
||||||
|
MoveToRoot()
|
||||||
|
|
||||||
|
// MoveToParent moves the NodeNavigator to the parent node of the current node.
|
||||||
|
MoveToParent() bool
|
||||||
|
|
||||||
|
// MoveToNextAttribute moves the NodeNavigator to the next attribute on current node.
|
||||||
|
MoveToNextAttribute() bool
|
||||||
|
|
||||||
|
// MoveToChild moves the NodeNavigator to the first child node of the current node.
|
||||||
|
MoveToChild() bool
|
||||||
|
|
||||||
|
// MoveToFirst moves the NodeNavigator to the first sibling node of the current node.
|
||||||
|
MoveToFirst() bool
|
||||||
|
|
||||||
|
// MoveToNext moves the NodeNavigator to the next sibling node of the current node.
|
||||||
|
MoveToNext() bool
|
||||||
|
|
||||||
|
// MoveToPrevious moves the NodeNavigator to the previous sibling node of the current node.
|
||||||
|
MoveToPrevious() bool
|
||||||
|
|
||||||
|
// MoveTo moves the NodeNavigator to the same position as the specified NodeNavigator.
|
||||||
|
MoveTo(NodeNavigator) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NodeIterator holds all matched Node object.
|
||||||
|
type NodeIterator struct {
|
||||||
|
node NodeNavigator
|
||||||
|
query query
|
||||||
|
}
|
||||||
|
|
||||||
|
// Current returns current node which matched.
|
||||||
|
func (t *NodeIterator) Current() NodeNavigator {
|
||||||
|
return t.node
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveNext moves Navigator to the next match node.
|
||||||
|
func (t *NodeIterator) MoveNext() bool {
|
||||||
|
n := t.query.Select(t)
|
||||||
|
if n != nil {
|
||||||
|
if !t.node.MoveTo(n) {
|
||||||
|
t.node = n.Copy()
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select selects a node set using the specified XPath expression.
|
||||||
|
// This method is deprecated, recommend using Expr.Select() method instead.
|
||||||
|
func Select(root NodeNavigator, expr string) *NodeIterator {
|
||||||
|
exp, err := Compile(expr)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return exp.Select(root)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expr is an XPath expression for query.
|
||||||
|
type Expr struct {
|
||||||
|
s string
|
||||||
|
q query
|
||||||
|
}
|
||||||
|
|
||||||
|
type iteratorFunc func() NodeNavigator
|
||||||
|
|
||||||
|
func (f iteratorFunc) Current() NodeNavigator {
|
||||||
|
return f()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Evaluate returns the result of the expression.
|
||||||
|
// The result type of the expression is one of the follow: bool,float64,string,NodeIterator).
|
||||||
|
func (expr *Expr) Evaluate(root NodeNavigator) interface{} {
|
||||||
|
val := expr.q.Evaluate(iteratorFunc(func() NodeNavigator { return root }))
|
||||||
|
switch val.(type) {
|
||||||
|
case query:
|
||||||
|
return &NodeIterator{query: expr.q.Clone(), node: root}
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select selects a node set using the specified XPath expression.
|
||||||
|
func (expr *Expr) Select(root NodeNavigator) *NodeIterator {
|
||||||
|
return &NodeIterator{query: expr.q.Clone(), node: root}
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns XPath expression string.
|
||||||
|
func (expr *Expr) String() string {
|
||||||
|
return expr.s
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile compiles an XPath expression string.
|
||||||
|
func Compile(expr string) (*Expr, error) {
|
||||||
|
if expr == "" {
|
||||||
|
return nil, errors.New("expr expression is nil")
|
||||||
|
}
|
||||||
|
qy, err := build(expr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Expr{s: expr, q: qy}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustCompile compiles an XPath expression string and ignored error.
|
||||||
|
func MustCompile(expr string) *Expr {
|
||||||
|
exp, err := Compile(expr)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return exp
|
||||||
|
}
|
9
vendor/github.com/go-sql-driver/mysql/.gitignore
generated
vendored
Normal file
9
vendor/github.com/go-sql-driver/mysql/.gitignore
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
.DS_Store
|
||||||
|
.DS_Store?
|
||||||
|
._*
|
||||||
|
.Spotlight-V100
|
||||||
|
.Trashes
|
||||||
|
Icon?
|
||||||
|
ehthumbs.db
|
||||||
|
Thumbs.db
|
||||||
|
.idea
|
107
vendor/github.com/go-sql-driver/mysql/.travis.yml
generated
vendored
Normal file
107
vendor/github.com/go-sql-driver/mysql/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
sudo: false
|
||||||
|
language: go
|
||||||
|
go:
|
||||||
|
- 1.7.x
|
||||||
|
- 1.8.x
|
||||||
|
- 1.9.x
|
||||||
|
- 1.10.x
|
||||||
|
- master
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- go get golang.org/x/tools/cmd/cover
|
||||||
|
- go get github.com/mattn/goveralls
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- echo -e "[server]\ninnodb_log_file_size=256MB\ninnodb_buffer_pool_size=512MB\nmax_allowed_packet=16MB" | sudo tee -a /etc/mysql/my.cnf
|
||||||
|
- sudo service mysql restart
|
||||||
|
- .travis/wait_mysql.sh
|
||||||
|
- mysql -e 'create database gotest;'
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- env: DB=MYSQL8
|
||||||
|
sudo: required
|
||||||
|
dist: trusty
|
||||||
|
go: 1.10.x
|
||||||
|
services:
|
||||||
|
- docker
|
||||||
|
before_install:
|
||||||
|
- go get golang.org/x/tools/cmd/cover
|
||||||
|
- go get github.com/mattn/goveralls
|
||||||
|
- docker pull mysql:8.0
|
||||||
|
- docker run -d -p 127.0.0.1:3307:3306 --name mysqld -e MYSQL_DATABASE=gotest -e MYSQL_USER=gotest -e MYSQL_PASSWORD=secret -e MYSQL_ROOT_PASSWORD=verysecret
|
||||||
|
mysql:8.0 --innodb_log_file_size=256MB --innodb_buffer_pool_size=512MB --max_allowed_packet=16MB --local-infile=1
|
||||||
|
- cp .travis/docker.cnf ~/.my.cnf
|
||||||
|
- .travis/wait_mysql.sh
|
||||||
|
before_script:
|
||||||
|
- export MYSQL_TEST_USER=gotest
|
||||||
|
- export MYSQL_TEST_PASS=secret
|
||||||
|
- export MYSQL_TEST_ADDR=127.0.0.1:3307
|
||||||
|
- export MYSQL_TEST_CONCURRENT=1
|
||||||
|
|
||||||
|
- env: DB=MYSQL57
|
||||||
|
sudo: required
|
||||||
|
dist: trusty
|
||||||
|
go: 1.10.x
|
||||||
|
services:
|
||||||
|
- docker
|
||||||
|
before_install:
|
||||||
|
- go get golang.org/x/tools/cmd/cover
|
||||||
|
- go get github.com/mattn/goveralls
|
||||||
|
- docker pull mysql:5.7
|
||||||
|
- docker run -d -p 127.0.0.1:3307:3306 --name mysqld -e MYSQL_DATABASE=gotest -e MYSQL_USER=gotest -e MYSQL_PASSWORD=secret -e MYSQL_ROOT_PASSWORD=verysecret
|
||||||
|
mysql:5.7 --innodb_log_file_size=256MB --innodb_buffer_pool_size=512MB --max_allowed_packet=16MB --local-infile=1
|
||||||
|
- cp .travis/docker.cnf ~/.my.cnf
|
||||||
|
- .travis/wait_mysql.sh
|
||||||
|
before_script:
|
||||||
|
- export MYSQL_TEST_USER=gotest
|
||||||
|
- export MYSQL_TEST_PASS=secret
|
||||||
|
- export MYSQL_TEST_ADDR=127.0.0.1:3307
|
||||||
|
- export MYSQL_TEST_CONCURRENT=1
|
||||||
|
|
||||||
|
- env: DB=MARIA55
|
||||||
|
sudo: required
|
||||||
|
dist: trusty
|
||||||
|
go: 1.10.x
|
||||||
|
services:
|
||||||
|
- docker
|
||||||
|
before_install:
|
||||||
|
- go get golang.org/x/tools/cmd/cover
|
||||||
|
- go get github.com/mattn/goveralls
|
||||||
|
- docker pull mariadb:5.5
|
||||||
|
- docker run -d -p 127.0.0.1:3307:3306 --name mysqld -e MYSQL_DATABASE=gotest -e MYSQL_USER=gotest -e MYSQL_PASSWORD=secret -e MYSQL_ROOT_PASSWORD=verysecret
|
||||||
|
mariadb:5.5 --innodb_log_file_size=256MB --innodb_buffer_pool_size=512MB --max_allowed_packet=16MB --local-infile=1
|
||||||
|
- cp .travis/docker.cnf ~/.my.cnf
|
||||||
|
- .travis/wait_mysql.sh
|
||||||
|
before_script:
|
||||||
|
- export MYSQL_TEST_USER=gotest
|
||||||
|
- export MYSQL_TEST_PASS=secret
|
||||||
|
- export MYSQL_TEST_ADDR=127.0.0.1:3307
|
||||||
|
- export MYSQL_TEST_CONCURRENT=1
|
||||||
|
|
||||||
|
- env: DB=MARIA10_1
|
||||||
|
sudo: required
|
||||||
|
dist: trusty
|
||||||
|
go: 1.10.x
|
||||||
|
services:
|
||||||
|
- docker
|
||||||
|
before_install:
|
||||||
|
- go get golang.org/x/tools/cmd/cover
|
||||||
|
- go get github.com/mattn/goveralls
|
||||||
|
- docker pull mariadb:10.1
|
||||||
|
- docker run -d -p 127.0.0.1:3307:3306 --name mysqld -e MYSQL_DATABASE=gotest -e MYSQL_USER=gotest -e MYSQL_PASSWORD=secret -e MYSQL_ROOT_PASSWORD=verysecret
|
||||||
|
mariadb:10.1 --innodb_log_file_size=256MB --innodb_buffer_pool_size=512MB --max_allowed_packet=16MB --local-infile=1
|
||||||
|
- cp .travis/docker.cnf ~/.my.cnf
|
||||||
|
- .travis/wait_mysql.sh
|
||||||
|
before_script:
|
||||||
|
- export MYSQL_TEST_USER=gotest
|
||||||
|
- export MYSQL_TEST_PASS=secret
|
||||||
|
- export MYSQL_TEST_ADDR=127.0.0.1:3307
|
||||||
|
- export MYSQL_TEST_CONCURRENT=1
|
||||||
|
|
||||||
|
script:
|
||||||
|
- go test -v -covermode=count -coverprofile=coverage.out
|
||||||
|
- go vet ./...
|
||||||
|
- .travis/gofmt.sh
|
||||||
|
after_script:
|
||||||
|
- $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci
|
89
vendor/github.com/go-sql-driver/mysql/AUTHORS
generated
vendored
Normal file
89
vendor/github.com/go-sql-driver/mysql/AUTHORS
generated
vendored
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
# This is the official list of Go-MySQL-Driver authors for copyright purposes.
|
||||||
|
|
||||||
|
# If you are submitting a patch, please add your name or the name of the
|
||||||
|
# organization which holds the copyright to this list in alphabetical order.
|
||||||
|
|
||||||
|
# Names should be added to this file as
|
||||||
|
# Name <email address>
|
||||||
|
# The email address is not required for organizations.
|
||||||
|
# Please keep the list sorted.
|
||||||
|
|
||||||
|
|
||||||
|
# Individual Persons
|
||||||
|
|
||||||
|
Aaron Hopkins <go-sql-driver at die.net>
|
||||||
|
Achille Roussel <achille.roussel at gmail.com>
|
||||||
|
Alexey Palazhchenko <alexey.palazhchenko at gmail.com>
|
||||||
|
Andrew Reid <andrew.reid at tixtrack.com>
|
||||||
|
Arne Hormann <arnehormann at gmail.com>
|
||||||
|
Asta Xie <xiemengjun at gmail.com>
|
||||||
|
Bulat Gaifullin <gaifullinbf at gmail.com>
|
||||||
|
Carlos Nieto <jose.carlos at menteslibres.net>
|
||||||
|
Chris Moos <chris at tech9computers.com>
|
||||||
|
Craig Wilson <craiggwilson at gmail.com>
|
||||||
|
Daniel Montoya <dsmontoyam at gmail.com>
|
||||||
|
Daniel Nichter <nil at codenode.com>
|
||||||
|
Daniël van Eeden <git at myname.nl>
|
||||||
|
Dave Protasowski <dprotaso at gmail.com>
|
||||||
|
DisposaBoy <disposaboy at dby.me>
|
||||||
|
Egor Smolyakov <egorsmkv at gmail.com>
|
||||||
|
Evan Shaw <evan at vendhq.com>
|
||||||
|
Frederick Mayle <frederickmayle at gmail.com>
|
||||||
|
Gustavo Kristic <gkristic at gmail.com>
|
||||||
|
Hajime Nakagami <nakagami at gmail.com>
|
||||||
|
Hanno Braun <mail at hannobraun.com>
|
||||||
|
Henri Yandell <flamefew at gmail.com>
|
||||||
|
Hirotaka Yamamoto <ymmt2005 at gmail.com>
|
||||||
|
ICHINOSE Shogo <shogo82148 at gmail.com>
|
||||||
|
INADA Naoki <songofacandy at gmail.com>
|
||||||
|
Jacek Szwec <szwec.jacek at gmail.com>
|
||||||
|
James Harr <james.harr at gmail.com>
|
||||||
|
Jeff Hodges <jeff at somethingsimilar.com>
|
||||||
|
Jeffrey Charles <jeffreycharles at gmail.com>
|
||||||
|
Jian Zhen <zhenjl at gmail.com>
|
||||||
|
Joshua Prunier <joshua.prunier at gmail.com>
|
||||||
|
Julien Lefevre <julien.lefevr at gmail.com>
|
||||||
|
Julien Schmidt <go-sql-driver at julienschmidt.com>
|
||||||
|
Justin Li <jli at j-li.net>
|
||||||
|
Justin Nuß <nuss.justin at gmail.com>
|
||||||
|
Kamil Dziedzic <kamil at klecza.pl>
|
||||||
|
Kevin Malachowski <kevin at chowski.com>
|
||||||
|
Kieron Woodhouse <kieron.woodhouse at infosum.com>
|
||||||
|
Lennart Rudolph <lrudolph at hmc.edu>
|
||||||
|
Leonardo YongUk Kim <dalinaum at gmail.com>
|
||||||
|
Linh Tran Tuan <linhduonggnu at gmail.com>
|
||||||
|
Lion Yang <lion at aosc.xyz>
|
||||||
|
Luca Looz <luca.looz92 at gmail.com>
|
||||||
|
Lucas Liu <extrafliu at gmail.com>
|
||||||
|
Luke Scott <luke at webconnex.com>
|
||||||
|
Maciej Zimnoch <maciej.zimnoch at codilime.com>
|
||||||
|
Michael Woolnough <michael.woolnough at gmail.com>
|
||||||
|
Nicola Peduzzi <thenikso at gmail.com>
|
||||||
|
Olivier Mengué <dolmen at cpan.org>
|
||||||
|
oscarzhao <oscarzhaosl at gmail.com>
|
||||||
|
Paul Bonser <misterpib at gmail.com>
|
||||||
|
Peter Schultz <peter.schultz at classmarkets.com>
|
||||||
|
Rebecca Chin <rchin at pivotal.io>
|
||||||
|
Reed Allman <rdallman10 at gmail.com>
|
||||||
|
Richard Wilkes <wilkes at me.com>
|
||||||
|
Robert Russell <robert at rrbrussell.com>
|
||||||
|
Runrioter Wung <runrioter at gmail.com>
|
||||||
|
Shuode Li <elemount at qq.com>
|
||||||
|
Soroush Pour <me at soroushjp.com>
|
||||||
|
Stan Putrya <root.vagner at gmail.com>
|
||||||
|
Stanley Gunawan <gunawan.stanley at gmail.com>
|
||||||
|
Xiangyu Hu <xiangyu.hu at outlook.com>
|
||||||
|
Xiaobing Jiang <s7v7nislands at gmail.com>
|
||||||
|
Xiuming Chen <cc at cxm.cc>
|
||||||
|
Zhenye Xie <xiezhenye at gmail.com>
|
||||||
|
|
||||||
|
# Organizations
|
||||||
|
|
||||||
|
Barracuda Networks, Inc.
|
||||||
|
Counting Ltd.
|
||||||
|
Google Inc.
|
||||||
|
InfoSum Ltd.
|
||||||
|
Keybase Inc.
|
||||||
|
Percona LLC
|
||||||
|
Pivotal Inc.
|
||||||
|
Stripe Inc.
|
178
vendor/github.com/go-sql-driver/mysql/CHANGELOG.md
generated
vendored
Normal file
178
vendor/github.com/go-sql-driver/mysql/CHANGELOG.md
generated
vendored
Normal file
@ -0,0 +1,178 @@
|
|||||||
|
## Version 1.4.1 (2018-11-14)
|
||||||
|
|
||||||
|
Bugfixes:
|
||||||
|
|
||||||
|
- Fix TIME format for binary columns (#818)
|
||||||
|
- Fix handling of empty auth plugin names (#835)
|
||||||
|
- Fix caching_sha2_password with empty password (#826)
|
||||||
|
- Fix canceled context broke mysqlConn (#862)
|
||||||
|
- Fix OldAuthSwitchRequest support (#870)
|
||||||
|
- Fix Auth Response packet for cleartext password (#887)
|
||||||
|
|
||||||
|
## Version 1.4 (2018-06-03)
|
||||||
|
|
||||||
|
Changes:
|
||||||
|
|
||||||
|
- Documentation fixes (#530, #535, #567)
|
||||||
|
- Refactoring (#575, #579, #580, #581, #603, #615, #704)
|
||||||
|
- Cache column names (#444)
|
||||||
|
- Sort the DSN parameters in DSNs generated from a config (#637)
|
||||||
|
- Allow native password authentication by default (#644)
|
||||||
|
- Use the default port if it is missing in the DSN (#668)
|
||||||
|
- Removed the `strict` mode (#676)
|
||||||
|
- Do not query `max_allowed_packet` by default (#680)
|
||||||
|
- Dropped support Go 1.6 and lower (#696)
|
||||||
|
- Updated `ConvertValue()` to match the database/sql/driver implementation (#760)
|
||||||
|
- Document the usage of `0000-00-00T00:00:00` as the time.Time zero value (#783)
|
||||||
|
- Improved the compatibility of the authentication system (#807)
|
||||||
|
|
||||||
|
New Features:
|
||||||
|
|
||||||
|
- Multi-Results support (#537)
|
||||||
|
- `rejectReadOnly` DSN option (#604)
|
||||||
|
- `context.Context` support (#608, #612, #627, #761)
|
||||||
|
- Transaction isolation level support (#619, #744)
|
||||||
|
- Read-Only transactions support (#618, #634)
|
||||||
|
- `NewConfig` function which initializes a config with default values (#679)
|
||||||
|
- Implemented the `ColumnType` interfaces (#667, #724)
|
||||||
|
- Support for custom string types in `ConvertValue` (#623)
|
||||||
|
- Implemented `NamedValueChecker`, improving support for uint64 with high bit set (#690, #709, #710)
|
||||||
|
- `caching_sha2_password` authentication plugin support (#794, #800, #801, #802)
|
||||||
|
- Implemented `driver.SessionResetter` (#779)
|
||||||
|
- `sha256_password` authentication plugin support (#808)
|
||||||
|
|
||||||
|
Bugfixes:
|
||||||
|
|
||||||
|
- Use the DSN hostname as TLS default ServerName if `tls=true` (#564, #718)
|
||||||
|
- Fixed LOAD LOCAL DATA INFILE for empty files (#590)
|
||||||
|
- Removed columns definition cache since it sometimes cached invalid data (#592)
|
||||||
|
- Don't mutate registered TLS configs (#600)
|
||||||
|
- Make RegisterTLSConfig concurrency-safe (#613)
|
||||||
|
- Handle missing auth data in the handshake packet correctly (#646)
|
||||||
|
- Do not retry queries when data was written to avoid data corruption (#302, #736)
|
||||||
|
- Cache the connection pointer for error handling before invalidating it (#678)
|
||||||
|
- Fixed imports for appengine/cloudsql (#700)
|
||||||
|
- Fix sending STMT_LONG_DATA for 0 byte data (#734)
|
||||||
|
- Set correct capacity for []bytes read from length-encoded strings (#766)
|
||||||
|
- Make RegisterDial concurrency-safe (#773)
|
||||||
|
|
||||||
|
|
||||||
|
## Version 1.3 (2016-12-01)
|
||||||
|
|
||||||
|
Changes:
|
||||||
|
|
||||||
|
- Go 1.1 is no longer supported
|
||||||
|
- Use decimals fields in MySQL to format time types (#249)
|
||||||
|
- Buffer optimizations (#269)
|
||||||
|
- TLS ServerName defaults to the host (#283)
|
||||||
|
- Refactoring (#400, #410, #437)
|
||||||
|
- Adjusted documentation for second generation CloudSQL (#485)
|
||||||
|
- Documented DSN system var quoting rules (#502)
|
||||||
|
- Made statement.Close() calls idempotent to avoid errors in Go 1.6+ (#512)
|
||||||
|
|
||||||
|
New Features:
|
||||||
|
|
||||||
|
- Enable microsecond resolution on TIME, DATETIME and TIMESTAMP (#249)
|
||||||
|
- Support for returning table alias on Columns() (#289, #359, #382)
|
||||||
|
- Placeholder interpolation, can be actived with the DSN parameter `interpolateParams=true` (#309, #318, #490)
|
||||||
|
- Support for uint64 parameters with high bit set (#332, #345)
|
||||||
|
- Cleartext authentication plugin support (#327)
|
||||||
|
- Exported ParseDSN function and the Config struct (#403, #419, #429)
|
||||||
|
- Read / Write timeouts (#401)
|
||||||
|
- Support for JSON field type (#414)
|
||||||
|
- Support for multi-statements and multi-results (#411, #431)
|
||||||
|
- DSN parameter to set the driver-side max_allowed_packet value manually (#489)
|
||||||
|
- Native password authentication plugin support (#494, #524)
|
||||||
|
|
||||||
|
Bugfixes:
|
||||||
|
|
||||||
|
- Fixed handling of queries without columns and rows (#255)
|
||||||
|
- Fixed a panic when SetKeepAlive() failed (#298)
|
||||||
|
- Handle ERR packets while reading rows (#321)
|
||||||
|
- Fixed reading NULL length-encoded integers in MySQL 5.6+ (#349)
|
||||||
|
- Fixed absolute paths support in LOAD LOCAL DATA INFILE (#356)
|
||||||
|
- Actually zero out bytes in handshake response (#378)
|
||||||
|
- Fixed race condition in registering LOAD DATA INFILE handler (#383)
|
||||||
|
- Fixed tests with MySQL 5.7.9+ (#380)
|
||||||
|
- QueryUnescape TLS config names (#397)
|
||||||
|
- Fixed "broken pipe" error by writing to closed socket (#390)
|
||||||
|
- Fixed LOAD LOCAL DATA INFILE buffering (#424)
|
||||||
|
- Fixed parsing of floats into float64 when placeholders are used (#434)
|
||||||
|
- Fixed DSN tests with Go 1.7+ (#459)
|
||||||
|
- Handle ERR packets while waiting for EOF (#473)
|
||||||
|
- Invalidate connection on error while discarding additional results (#513)
|
||||||
|
- Allow terminating packets of length 0 (#516)
|
||||||
|
|
||||||
|
|
||||||
|
## Version 1.2 (2014-06-03)
|
||||||
|
|
||||||
|
Changes:
|
||||||
|
|
||||||
|
- We switched back to a "rolling release". `go get` installs the current master branch again
|
||||||
|
- Version v1 of the driver will not be maintained anymore. Go 1.0 is no longer supported by this driver
|
||||||
|
- Exported errors to allow easy checking from application code
|
||||||
|
- Enabled TCP Keepalives on TCP connections
|
||||||
|
- Optimized INFILE handling (better buffer size calculation, lazy init, ...)
|
||||||
|
- The DSN parser also checks for a missing separating slash
|
||||||
|
- Faster binary date / datetime to string formatting
|
||||||
|
- Also exported the MySQLWarning type
|
||||||
|
- mysqlConn.Close returns the first error encountered instead of ignoring all errors
|
||||||
|
- writePacket() automatically writes the packet size to the header
|
||||||
|
- readPacket() uses an iterative approach instead of the recursive approach to merge splitted packets
|
||||||
|
|
||||||
|
New Features:
|
||||||
|
|
||||||
|
- `RegisterDial` allows the usage of a custom dial function to establish the network connection
|
||||||
|
- Setting the connection collation is possible with the `collation` DSN parameter. This parameter should be preferred over the `charset` parameter
|
||||||
|
- Logging of critical errors is configurable with `SetLogger`
|
||||||
|
- Google CloudSQL support
|
||||||
|
|
||||||
|
Bugfixes:
|
||||||
|
|
||||||
|
- Allow more than 32 parameters in prepared statements
|
||||||
|
- Various old_password fixes
|
||||||
|
- Fixed TestConcurrent test to pass Go's race detection
|
||||||
|
- Fixed appendLengthEncodedInteger for large numbers
|
||||||
|
- Renamed readLengthEnodedString to readLengthEncodedString and skipLengthEnodedString to skipLengthEncodedString (fixed typo)
|
||||||
|
|
||||||
|
|
||||||
|
## Version 1.1 (2013-11-02)
|
||||||
|
|
||||||
|
Changes:
|
||||||
|
|
||||||
|
- Go-MySQL-Driver now requires Go 1.1
|
||||||
|
- Connections now use the collation `utf8_general_ci` by default. Adding `&charset=UTF8` to the DSN should not be necessary anymore
|
||||||
|
- Made closing rows and connections error tolerant. This allows for example deferring rows.Close() without checking for errors
|
||||||
|
- `[]byte(nil)` is now treated as a NULL value. Before, it was treated like an empty string / `[]byte("")`
|
||||||
|
- DSN parameter values must now be url.QueryEscape'ed. This allows text values to contain special characters, such as '&'.
|
||||||
|
- Use the IO buffer also for writing. This results in zero allocations (by the driver) for most queries
|
||||||
|
- Optimized the buffer for reading
|
||||||
|
- stmt.Query now caches column metadata
|
||||||
|
- New Logo
|
||||||
|
- Changed the copyright header to include all contributors
|
||||||
|
- Improved the LOAD INFILE documentation
|
||||||
|
- The driver struct is now exported to make the driver directly accessible
|
||||||
|
- Refactored the driver tests
|
||||||
|
- Added more benchmarks and moved all to a separate file
|
||||||
|
- Other small refactoring
|
||||||
|
|
||||||
|
New Features:
|
||||||
|
|
||||||
|
- Added *old_passwords* support: Required in some cases, but must be enabled by adding `allowOldPasswords=true` to the DSN since it is insecure
|
||||||
|
- Added a `clientFoundRows` parameter: Return the number of matching rows instead of the number of rows changed on UPDATEs
|
||||||
|
- Added TLS/SSL support: Use a TLS/SSL encrypted connection to the server. Custom TLS configs can be registered and used
|
||||||
|
|
||||||
|
Bugfixes:
|
||||||
|
|
||||||
|
- Fixed MySQL 4.1 support: MySQL 4.1 sends packets with lengths which differ from the specification
|
||||||
|
- Convert to DB timezone when inserting `time.Time`
|
||||||
|
- Splitted packets (more than 16MB) are now merged correctly
|
||||||
|
- Fixed false positive `io.EOF` errors when the data was fully read
|
||||||
|
- Avoid panics on reuse of closed connections
|
||||||
|
- Fixed empty string producing false nil values
|
||||||
|
- Fixed sign byte for positive TIME fields
|
||||||
|
|
||||||
|
|
||||||
|
## Version 1.0 (2013-05-14)
|
||||||
|
|
||||||
|
Initial Release
|
23
vendor/github.com/go-sql-driver/mysql/CONTRIBUTING.md
generated
vendored
Normal file
23
vendor/github.com/go-sql-driver/mysql/CONTRIBUTING.md
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# Contributing Guidelines
|
||||||
|
|
||||||
|
## Reporting Issues
|
||||||
|
|
||||||
|
Before creating a new Issue, please check first if a similar Issue [already exists](https://github.com/go-sql-driver/mysql/issues?state=open) or was [recently closed](https://github.com/go-sql-driver/mysql/issues?direction=desc&page=1&sort=updated&state=closed).
|
||||||
|
|
||||||
|
## Contributing Code
|
||||||
|
|
||||||
|
By contributing to this project, you share your code under the Mozilla Public License 2, as specified in the LICENSE file.
|
||||||
|
Don't forget to add yourself to the AUTHORS file.
|
||||||
|
|
||||||
|
### Code Review
|
||||||
|
|
||||||
|
Everyone is invited to review and comment on pull requests.
|
||||||
|
If it looks fine to you, comment with "LGTM" (Looks good to me).
|
||||||
|
|
||||||
|
If changes are required, notice the reviewers with "PTAL" (Please take another look) after committing the fixes.
|
||||||
|
|
||||||
|
Before merging the Pull Request, at least one [team member](https://github.com/go-sql-driver?tab=members) must have commented with "LGTM".
|
||||||
|
|
||||||
|
## Development Ideas
|
||||||
|
|
||||||
|
If you are looking for ideas for code contributions, please check our [Development Ideas](https://github.com/go-sql-driver/mysql/wiki/Development-Ideas) Wiki page.
|
373
vendor/github.com/go-sql-driver/mysql/LICENSE
generated
vendored
Normal file
373
vendor/github.com/go-sql-driver/mysql/LICENSE
generated
vendored
Normal file
@ -0,0 +1,373 @@
|
|||||||
|
Mozilla Public License Version 2.0
|
||||||
|
==================================
|
||||||
|
|
||||||
|
1. Definitions
|
||||||
|
--------------
|
||||||
|
|
||||||
|
1.1. "Contributor"
|
||||||
|
means each individual or legal entity that creates, contributes to
|
||||||
|
the creation of, or owns Covered Software.
|
||||||
|
|
||||||
|
1.2. "Contributor Version"
|
||||||
|
means the combination of the Contributions of others (if any) used
|
||||||
|
by a Contributor and that particular Contributor's Contribution.
|
||||||
|
|
||||||
|
1.3. "Contribution"
|
||||||
|
means Covered Software of a particular Contributor.
|
||||||
|
|
||||||
|
1.4. "Covered Software"
|
||||||
|
means Source Code Form to which the initial Contributor has attached
|
||||||
|
the notice in Exhibit A, the Executable Form of such Source Code
|
||||||
|
Form, and Modifications of such Source Code Form, in each case
|
||||||
|
including portions thereof.
|
||||||
|
|
||||||
|
1.5. "Incompatible With Secondary Licenses"
|
||||||
|
means
|
||||||
|
|
||||||
|
(a) that the initial Contributor has attached the notice described
|
||||||
|
in Exhibit B to the Covered Software; or
|
||||||
|
|
||||||
|
(b) that the Covered Software was made available under the terms of
|
||||||
|
version 1.1 or earlier of the License, but not also under the
|
||||||
|
terms of a Secondary License.
|
||||||
|
|
||||||
|
1.6. "Executable Form"
|
||||||
|
means any form of the work other than Source Code Form.
|
||||||
|
|
||||||
|
1.7. "Larger Work"
|
||||||
|
means a work that combines Covered Software with other material, in
|
||||||
|
a separate file or files, that is not Covered Software.
|
||||||
|
|
||||||
|
1.8. "License"
|
||||||
|
means this document.
|
||||||
|
|
||||||
|
1.9. "Licensable"
|
||||||
|
means having the right to grant, to the maximum extent possible,
|
||||||
|
whether at the time of the initial grant or subsequently, any and
|
||||||
|
all of the rights conveyed by this License.
|
||||||
|
|
||||||
|
1.10. "Modifications"
|
||||||
|
means any of the following:
|
||||||
|
|
||||||
|
(a) any file in Source Code Form that results from an addition to,
|
||||||
|
deletion from, or modification of the contents of Covered
|
||||||
|
Software; or
|
||||||
|
|
||||||
|
(b) any new file in Source Code Form that contains any Covered
|
||||||
|
Software.
|
||||||
|
|
||||||
|
1.11. "Patent Claims" of a Contributor
|
||||||
|
means any patent claim(s), including without limitation, method,
|
||||||
|
process, and apparatus claims, in any patent Licensable by such
|
||||||
|
Contributor that would be infringed, but for the grant of the
|
||||||
|
License, by the making, using, selling, offering for sale, having
|
||||||
|
made, import, or transfer of either its Contributions or its
|
||||||
|
Contributor Version.
|
||||||
|
|
||||||
|
1.12. "Secondary License"
|
||||||
|
means either the GNU General Public License, Version 2.0, the GNU
|
||||||
|
Lesser General Public License, Version 2.1, the GNU Affero General
|
||||||
|
Public License, Version 3.0, or any later versions of those
|
||||||
|
licenses.
|
||||||
|
|
||||||
|
1.13. "Source Code Form"
|
||||||
|
means the form of the work preferred for making modifications.
|
||||||
|
|
||||||
|
1.14. "You" (or "Your")
|
||||||
|
means an individual or a legal entity exercising rights under this
|
||||||
|
License. For legal entities, "You" includes any entity that
|
||||||
|
controls, is controlled by, or is under common control with You. For
|
||||||
|
purposes of this definition, "control" means (a) the power, direct
|
||||||
|
or indirect, to cause the direction or management of such entity,
|
||||||
|
whether by contract or otherwise, or (b) ownership of more than
|
||||||
|
fifty percent (50%) of the outstanding shares or beneficial
|
||||||
|
ownership of such entity.
|
||||||
|
|
||||||
|
2. License Grants and Conditions
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
2.1. Grants
|
||||||
|
|
||||||
|
Each Contributor hereby grants You a world-wide, royalty-free,
|
||||||
|
non-exclusive license:
|
||||||
|
|
||||||
|
(a) under intellectual property rights (other than patent or trademark)
|
||||||
|
Licensable by such Contributor to use, reproduce, make available,
|
||||||
|
modify, display, perform, distribute, and otherwise exploit its
|
||||||
|
Contributions, either on an unmodified basis, with Modifications, or
|
||||||
|
as part of a Larger Work; and
|
||||||
|
|
||||||
|
(b) under Patent Claims of such Contributor to make, use, sell, offer
|
||||||
|
for sale, have made, import, and otherwise transfer either its
|
||||||
|
Contributions or its Contributor Version.
|
||||||
|
|
||||||
|
2.2. Effective Date
|
||||||
|
|
||||||
|
The licenses granted in Section 2.1 with respect to any Contribution
|
||||||
|
become effective for each Contribution on the date the Contributor first
|
||||||
|
distributes such Contribution.
|
||||||
|
|
||||||
|
2.3. Limitations on Grant Scope
|
||||||
|
|
||||||
|
The licenses granted in this Section 2 are the only rights granted under
|
||||||
|
this License. No additional rights or licenses will be implied from the
|
||||||
|
distribution or licensing of Covered Software under this License.
|
||||||
|
Notwithstanding Section 2.1(b) above, no patent license is granted by a
|
||||||
|
Contributor:
|
||||||
|
|
||||||
|
(a) for any code that a Contributor has removed from Covered Software;
|
||||||
|
or
|
||||||
|
|
||||||
|
(b) for infringements caused by: (i) Your and any other third party's
|
||||||
|
modifications of Covered Software, or (ii) the combination of its
|
||||||
|
Contributions with other software (except as part of its Contributor
|
||||||
|
Version); or
|
||||||
|
|
||||||
|
(c) under Patent Claims infringed by Covered Software in the absence of
|
||||||
|
its Contributions.
|
||||||
|
|
||||||
|
This License does not grant any rights in the trademarks, service marks,
|
||||||
|
or logos of any Contributor (except as may be necessary to comply with
|
||||||
|
the notice requirements in Section 3.4).
|
||||||
|
|
||||||
|
2.4. Subsequent Licenses
|
||||||
|
|
||||||
|
No Contributor makes additional grants as a result of Your choice to
|
||||||
|
distribute the Covered Software under a subsequent version of this
|
||||||
|
License (see Section 10.2) or under the terms of a Secondary License (if
|
||||||
|
permitted under the terms of Section 3.3).
|
||||||
|
|
||||||
|
2.5. Representation
|
||||||
|
|
||||||
|
Each Contributor represents that the Contributor believes its
|
||||||
|
Contributions are its original creation(s) or it has sufficient rights
|
||||||
|
to grant the rights to its Contributions conveyed by this License.
|
||||||
|
|
||||||
|
2.6. Fair Use
|
||||||
|
|
||||||
|
This License is not intended to limit any rights You have under
|
||||||
|
applicable copyright doctrines of fair use, fair dealing, or other
|
||||||
|
equivalents.
|
||||||
|
|
||||||
|
2.7. Conditions
|
||||||
|
|
||||||
|
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
|
||||||
|
in Section 2.1.
|
||||||
|
|
||||||
|
3. Responsibilities
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
3.1. Distribution of Source Form
|
||||||
|
|
||||||
|
All distribution of Covered Software in Source Code Form, including any
|
||||||
|
Modifications that You create or to which You contribute, must be under
|
||||||
|
the terms of this License. You must inform recipients that the Source
|
||||||
|
Code Form of the Covered Software is governed by the terms of this
|
||||||
|
License, and how they can obtain a copy of this License. You may not
|
||||||
|
attempt to alter or restrict the recipients' rights in the Source Code
|
||||||
|
Form.
|
||||||
|
|
||||||
|
3.2. Distribution of Executable Form
|
||||||
|
|
||||||
|
If You distribute Covered Software in Executable Form then:
|
||||||
|
|
||||||
|
(a) such Covered Software must also be made available in Source Code
|
||||||
|
Form, as described in Section 3.1, and You must inform recipients of
|
||||||
|
the Executable Form how they can obtain a copy of such Source Code
|
||||||
|
Form by reasonable means in a timely manner, at a charge no more
|
||||||
|
than the cost of distribution to the recipient; and
|
||||||
|
|
||||||
|
(b) You may distribute such Executable Form under the terms of this
|
||||||
|
License, or sublicense it under different terms, provided that the
|
||||||
|
license for the Executable Form does not attempt to limit or alter
|
||||||
|
the recipients' rights in the Source Code Form under this License.
|
||||||
|
|
||||||
|
3.3. Distribution of a Larger Work
|
||||||
|
|
||||||
|
You may create and distribute a Larger Work under terms of Your choice,
|
||||||
|
provided that You also comply with the requirements of this License for
|
||||||
|
the Covered Software. If the Larger Work is a combination of Covered
|
||||||
|
Software with a work governed by one or more Secondary Licenses, and the
|
||||||
|
Covered Software is not Incompatible With Secondary Licenses, this
|
||||||
|
License permits You to additionally distribute such Covered Software
|
||||||
|
under the terms of such Secondary License(s), so that the recipient of
|
||||||
|
the Larger Work may, at their option, further distribute the Covered
|
||||||
|
Software under the terms of either this License or such Secondary
|
||||||
|
License(s).
|
||||||
|
|
||||||
|
3.4. Notices
|
||||||
|
|
||||||
|
You may not remove or alter the substance of any license notices
|
||||||
|
(including copyright notices, patent notices, disclaimers of warranty,
|
||||||
|
or limitations of liability) contained within the Source Code Form of
|
||||||
|
the Covered Software, except that You may alter any license notices to
|
||||||
|
the extent required to remedy known factual inaccuracies.
|
||||||
|
|
||||||
|
3.5. Application of Additional Terms
|
||||||
|
|
||||||
|
You may choose to offer, and to charge a fee for, warranty, support,
|
||||||
|
indemnity or liability obligations to one or more recipients of Covered
|
||||||
|
Software. However, You may do so only on Your own behalf, and not on
|
||||||
|
behalf of any Contributor. You must make it absolutely clear that any
|
||||||
|
such warranty, support, indemnity, or liability obligation is offered by
|
||||||
|
You alone, and You hereby agree to indemnify every Contributor for any
|
||||||
|
liability incurred by such Contributor as a result of warranty, support,
|
||||||
|
indemnity or liability terms You offer. You may include additional
|
||||||
|
disclaimers of warranty and limitations of liability specific to any
|
||||||
|
jurisdiction.
|
||||||
|
|
||||||
|
4. Inability to Comply Due to Statute or Regulation
|
||||||
|
---------------------------------------------------
|
||||||
|
|
||||||
|
If it is impossible for You to comply with any of the terms of this
|
||||||
|
License with respect to some or all of the Covered Software due to
|
||||||
|
statute, judicial order, or regulation then You must: (a) comply with
|
||||||
|
the terms of this License to the maximum extent possible; and (b)
|
||||||
|
describe the limitations and the code they affect. Such description must
|
||||||
|
be placed in a text file included with all distributions of the Covered
|
||||||
|
Software under this License. Except to the extent prohibited by statute
|
||||||
|
or regulation, such description must be sufficiently detailed for a
|
||||||
|
recipient of ordinary skill to be able to understand it.
|
||||||
|
|
||||||
|
5. Termination
|
||||||
|
--------------
|
||||||
|
|
||||||
|
5.1. The rights granted under this License will terminate automatically
|
||||||
|
if You fail to comply with any of its terms. However, if You become
|
||||||
|
compliant, then the rights granted under this License from a particular
|
||||||
|
Contributor are reinstated (a) provisionally, unless and until such
|
||||||
|
Contributor explicitly and finally terminates Your grants, and (b) on an
|
||||||
|
ongoing basis, if such Contributor fails to notify You of the
|
||||||
|
non-compliance by some reasonable means prior to 60 days after You have
|
||||||
|
come back into compliance. Moreover, Your grants from a particular
|
||||||
|
Contributor are reinstated on an ongoing basis if such Contributor
|
||||||
|
notifies You of the non-compliance by some reasonable means, this is the
|
||||||
|
first time You have received notice of non-compliance with this License
|
||||||
|
from such Contributor, and You become compliant prior to 30 days after
|
||||||
|
Your receipt of the notice.
|
||||||
|
|
||||||
|
5.2. If You initiate litigation against any entity by asserting a patent
|
||||||
|
infringement claim (excluding declaratory judgment actions,
|
||||||
|
counter-claims, and cross-claims) alleging that a Contributor Version
|
||||||
|
directly or indirectly infringes any patent, then the rights granted to
|
||||||
|
You by any and all Contributors for the Covered Software under Section
|
||||||
|
2.1 of this License shall terminate.
|
||||||
|
|
||||||
|
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
|
||||||
|
end user license agreements (excluding distributors and resellers) which
|
||||||
|
have been validly granted by You or Your distributors under this License
|
||||||
|
prior to termination shall survive termination.
|
||||||
|
|
||||||
|
************************************************************************
|
||||||
|
* *
|
||||||
|
* 6. Disclaimer of Warranty *
|
||||||
|
* ------------------------- *
|
||||||
|
* *
|
||||||
|
* Covered Software is provided under this License on an "as is" *
|
||||||
|
* basis, without warranty of any kind, either expressed, implied, or *
|
||||||
|
* statutory, including, without limitation, warranties that the *
|
||||||
|
* Covered Software is free of defects, merchantable, fit for a *
|
||||||
|
* particular purpose or non-infringing. The entire risk as to the *
|
||||||
|
* quality and performance of the Covered Software is with You. *
|
||||||
|
* Should any Covered Software prove defective in any respect, You *
|
||||||
|
* (not any Contributor) assume the cost of any necessary servicing, *
|
||||||
|
* repair, or correction. This disclaimer of warranty constitutes an *
|
||||||
|
* essential part of this License. No use of any Covered Software is *
|
||||||
|
* authorized under this License except under this disclaimer. *
|
||||||
|
* *
|
||||||
|
************************************************************************
|
||||||
|
|
||||||
|
************************************************************************
|
||||||
|
* *
|
||||||
|
* 7. Limitation of Liability *
|
||||||
|
* -------------------------- *
|
||||||
|
* *
|
||||||
|
* Under no circumstances and under no legal theory, whether tort *
|
||||||
|
* (including negligence), contract, or otherwise, shall any *
|
||||||
|
* Contributor, or anyone who distributes Covered Software as *
|
||||||
|
* permitted above, be liable to You for any direct, indirect, *
|
||||||
|
* special, incidental, or consequential damages of any character *
|
||||||
|
* including, without limitation, damages for lost profits, loss of *
|
||||||
|
* goodwill, work stoppage, computer failure or malfunction, or any *
|
||||||
|
* and all other commercial damages or losses, even if such party *
|
||||||
|
* shall have been informed of the possibility of such damages. This *
|
||||||
|
* limitation of liability shall not apply to liability for death or *
|
||||||
|
* personal injury resulting from such party's negligence to the *
|
||||||
|
* extent applicable law prohibits such limitation. Some *
|
||||||
|
* jurisdictions do not allow the exclusion or limitation of *
|
||||||
|
* incidental or consequential damages, so this exclusion and *
|
||||||
|
* limitation may not apply to You. *
|
||||||
|
* *
|
||||||
|
************************************************************************
|
||||||
|
|
||||||
|
8. Litigation
|
||||||
|
-------------
|
||||||
|
|
||||||
|
Any litigation relating to this License may be brought only in the
|
||||||
|
courts of a jurisdiction where the defendant maintains its principal
|
||||||
|
place of business and such litigation shall be governed by laws of that
|
||||||
|
jurisdiction, without reference to its conflict-of-law provisions.
|
||||||
|
Nothing in this Section shall prevent a party's ability to bring
|
||||||
|
cross-claims or counter-claims.
|
||||||
|
|
||||||
|
9. Miscellaneous
|
||||||
|
----------------
|
||||||
|
|
||||||
|
This License represents the complete agreement concerning the subject
|
||||||
|
matter hereof. If any provision of this License is held to be
|
||||||
|
unenforceable, such provision shall be reformed only to the extent
|
||||||
|
necessary to make it enforceable. Any law or regulation which provides
|
||||||
|
that the language of a contract shall be construed against the drafter
|
||||||
|
shall not be used to construe this License against a Contributor.
|
||||||
|
|
||||||
|
10. Versions of the License
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
10.1. New Versions
|
||||||
|
|
||||||
|
Mozilla Foundation is the license steward. Except as provided in Section
|
||||||
|
10.3, no one other than the license steward has the right to modify or
|
||||||
|
publish new versions of this License. Each version will be given a
|
||||||
|
distinguishing version number.
|
||||||
|
|
||||||
|
10.2. Effect of New Versions
|
||||||
|
|
||||||
|
You may distribute the Covered Software under the terms of the version
|
||||||
|
of the License under which You originally received the Covered Software,
|
||||||
|
or under the terms of any subsequent version published by the license
|
||||||
|
steward.
|
||||||
|
|
||||||
|
10.3. Modified Versions
|
||||||
|
|
||||||
|
If you create software not governed by this License, and you want to
|
||||||
|
create a new license for such software, you may create and use a
|
||||||
|
modified version of this License if you rename the license and remove
|
||||||
|
any references to the name of the license steward (except to note that
|
||||||
|
such modified license differs from this License).
|
||||||
|
|
||||||
|
10.4. Distributing Source Code Form that is Incompatible With Secondary
|
||||||
|
Licenses
|
||||||
|
|
||||||
|
If You choose to distribute Source Code Form that is Incompatible With
|
||||||
|
Secondary Licenses under the terms of this version of the License, the
|
||||||
|
notice described in Exhibit B of this License must be attached.
|
||||||
|
|
||||||
|
Exhibit A - Source Code Form License Notice
|
||||||
|
-------------------------------------------
|
||||||
|
|
||||||
|
This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
If it is not possible or desirable to put the notice in a particular
|
||||||
|
file, then You may include the notice in a location (such as a LICENSE
|
||||||
|
file in a relevant directory) where a recipient would be likely to look
|
||||||
|
for such a notice.
|
||||||
|
|
||||||
|
You may add additional accurate notices of copyright ownership.
|
||||||
|
|
||||||
|
Exhibit B - "Incompatible With Secondary Licenses" Notice
|
||||||
|
---------------------------------------------------------
|
||||||
|
|
||||||
|
This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||||
|
defined by the Mozilla Public License, v. 2.0.
|
490
vendor/github.com/go-sql-driver/mysql/README.md
generated
vendored
Normal file
490
vendor/github.com/go-sql-driver/mysql/README.md
generated
vendored
Normal file
@ -0,0 +1,490 @@
|
|||||||
|
# Go-MySQL-Driver
|
||||||
|
|
||||||
|
A MySQL-Driver for Go's [database/sql](https://golang.org/pkg/database/sql/) package
|
||||||
|
|
||||||
|
![Go-MySQL-Driver logo](https://raw.github.com/wiki/go-sql-driver/mysql/gomysql_m.png "Golang Gopher holding the MySQL Dolphin")
|
||||||
|
|
||||||
|
---------------------------------------
|
||||||
|
* [Features](#features)
|
||||||
|
* [Requirements](#requirements)
|
||||||
|
* [Installation](#installation)
|
||||||
|
* [Usage](#usage)
|
||||||
|
* [DSN (Data Source Name)](#dsn-data-source-name)
|
||||||
|
* [Password](#password)
|
||||||
|
* [Protocol](#protocol)
|
||||||
|
* [Address](#address)
|
||||||
|
* [Parameters](#parameters)
|
||||||
|
* [Examples](#examples)
|
||||||
|
* [Connection pool and timeouts](#connection-pool-and-timeouts)
|
||||||
|
* [context.Context Support](#contextcontext-support)
|
||||||
|
* [ColumnType Support](#columntype-support)
|
||||||
|
* [LOAD DATA LOCAL INFILE support](#load-data-local-infile-support)
|
||||||
|
* [time.Time support](#timetime-support)
|
||||||
|
* [Unicode support](#unicode-support)
|
||||||
|
* [Testing / Development](#testing--development)
|
||||||
|
* [License](#license)
|
||||||
|
|
||||||
|
---------------------------------------
|
||||||
|
|
||||||
|
## Features
|
||||||
|
* Lightweight and [fast](https://github.com/go-sql-driver/sql-benchmark "golang MySQL-Driver performance")
|
||||||
|
* Native Go implementation. No C-bindings, just pure Go
|
||||||
|
* Connections over TCP/IPv4, TCP/IPv6, Unix domain sockets or [custom protocols](https://godoc.org/github.com/go-sql-driver/mysql#DialFunc)
|
||||||
|
* Automatic handling of broken connections
|
||||||
|
* Automatic Connection Pooling *(by database/sql package)*
|
||||||
|
* Supports queries larger than 16MB
|
||||||
|
* Full [`sql.RawBytes`](https://golang.org/pkg/database/sql/#RawBytes) support.
|
||||||
|
* Intelligent `LONG DATA` handling in prepared statements
|
||||||
|
* Secure `LOAD DATA LOCAL INFILE` support with file Whitelisting and `io.Reader` support
|
||||||
|
* Optional `time.Time` parsing
|
||||||
|
* Optional placeholder interpolation
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
* Go 1.7 or higher. We aim to support the 3 latest versions of Go.
|
||||||
|
* MySQL (4.1+), MariaDB, Percona Server, Google CloudSQL or Sphinx (2.2.3+)
|
||||||
|
|
||||||
|
---------------------------------------
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
Simple install the package to your [$GOPATH](https://github.com/golang/go/wiki/GOPATH "GOPATH") with the [go tool](https://golang.org/cmd/go/ "go command") from shell:
|
||||||
|
```bash
|
||||||
|
$ go get -u github.com/go-sql-driver/mysql
|
||||||
|
```
|
||||||
|
Make sure [Git is installed](https://git-scm.com/downloads) on your machine and in your system's `PATH`.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
_Go MySQL Driver_ is an implementation of Go's `database/sql/driver` interface. You only need to import the driver and can use the full [`database/sql`](https://golang.org/pkg/database/sql/) API then.
|
||||||
|
|
||||||
|
Use `mysql` as `driverName` and a valid [DSN](#dsn-data-source-name) as `dataSourceName`:
|
||||||
|
```go
|
||||||
|
import "database/sql"
|
||||||
|
import _ "github.com/go-sql-driver/mysql"
|
||||||
|
|
||||||
|
db, err := sql.Open("mysql", "user:password@/dbname")
|
||||||
|
```
|
||||||
|
|
||||||
|
[Examples are available in our Wiki](https://github.com/go-sql-driver/mysql/wiki/Examples "Go-MySQL-Driver Examples").
|
||||||
|
|
||||||
|
|
||||||
|
### DSN (Data Source Name)
|
||||||
|
|
||||||
|
The Data Source Name has a common format, like e.g. [PEAR DB](http://pear.php.net/manual/en/package.database.db.intro-dsn.php) uses it, but without type-prefix (optional parts marked by squared brackets):
|
||||||
|
```
|
||||||
|
[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]
|
||||||
|
```
|
||||||
|
|
||||||
|
A DSN in its fullest form:
|
||||||
|
```
|
||||||
|
username:password@protocol(address)/dbname?param=value
|
||||||
|
```
|
||||||
|
|
||||||
|
Except for the databasename, all values are optional. So the minimal DSN is:
|
||||||
|
```
|
||||||
|
/dbname
|
||||||
|
```
|
||||||
|
|
||||||
|
If you do not want to preselect a database, leave `dbname` empty:
|
||||||
|
```
|
||||||
|
/
|
||||||
|
```
|
||||||
|
This has the same effect as an empty DSN string:
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternatively, [Config.FormatDSN](https://godoc.org/github.com/go-sql-driver/mysql#Config.FormatDSN) can be used to create a DSN string by filling a struct.
|
||||||
|
|
||||||
|
#### Password
|
||||||
|
Passwords can consist of any character. Escaping is **not** necessary.
|
||||||
|
|
||||||
|
#### Protocol
|
||||||
|
See [net.Dial](https://golang.org/pkg/net/#Dial) for more information which networks are available.
|
||||||
|
In general you should use an Unix domain socket if available and TCP otherwise for best performance.
|
||||||
|
|
||||||
|
#### Address
|
||||||
|
For TCP and UDP networks, addresses have the form `host[:port]`.
|
||||||
|
If `port` is omitted, the default port will be used.
|
||||||
|
If `host` is a literal IPv6 address, it must be enclosed in square brackets.
|
||||||
|
The functions [net.JoinHostPort](https://golang.org/pkg/net/#JoinHostPort) and [net.SplitHostPort](https://golang.org/pkg/net/#SplitHostPort) manipulate addresses in this form.
|
||||||
|
|
||||||
|
For Unix domain sockets the address is the absolute path to the MySQL-Server-socket, e.g. `/var/run/mysqld/mysqld.sock` or `/tmp/mysql.sock`.
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
*Parameters are case-sensitive!*
|
||||||
|
|
||||||
|
Notice that any of `true`, `TRUE`, `True` or `1` is accepted to stand for a true boolean value. Not surprisingly, false can be specified as any of: `false`, `FALSE`, `False` or `0`.
|
||||||
|
|
||||||
|
##### `allowAllFiles`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
`allowAllFiles=true` disables the file Whitelist for `LOAD DATA LOCAL INFILE` and allows *all* files.
|
||||||
|
[*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)
|
||||||
|
|
||||||
|
##### `allowCleartextPasswords`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
`allowCleartextPasswords=true` allows using the [cleartext client side plugin](http://dev.mysql.com/doc/en/cleartext-authentication-plugin.html) if required by an account, such as one defined with the [PAM authentication plugin](http://dev.mysql.com/doc/en/pam-authentication-plugin.html). Sending passwords in clear text may be a security problem in some configurations. To avoid problems if there is any possibility that the password would be intercepted, clients should connect to MySQL Server using a method that protects the password. Possibilities include [TLS / SSL](#tls), IPsec, or a private network.
|
||||||
|
|
||||||
|
##### `allowNativePasswords`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: true
|
||||||
|
```
|
||||||
|
`allowNativePasswords=false` disallows the usage of MySQL native password method.
|
||||||
|
|
||||||
|
##### `allowOldPasswords`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
`allowOldPasswords=true` allows the usage of the insecure old password method. This should be avoided, but is necessary in some cases. See also [the old_passwords wiki page](https://github.com/go-sql-driver/mysql/wiki/old_passwords).
|
||||||
|
|
||||||
|
##### `charset`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: string
|
||||||
|
Valid Values: <name>
|
||||||
|
Default: none
|
||||||
|
```
|
||||||
|
|
||||||
|
Sets the charset used for client-server interaction (`"SET NAMES <value>"`). If multiple charsets are set (separated by a comma), the following charset is used if setting the charset failes. This enables for example support for `utf8mb4` ([introduced in MySQL 5.5.3](http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html)) with fallback to `utf8` for older servers (`charset=utf8mb4,utf8`).
|
||||||
|
|
||||||
|
Usage of the `charset` parameter is discouraged because it issues additional queries to the server.
|
||||||
|
Unless you need the fallback behavior, please use `collation` instead.
|
||||||
|
|
||||||
|
##### `collation`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: string
|
||||||
|
Valid Values: <name>
|
||||||
|
Default: utf8_general_ci
|
||||||
|
```
|
||||||
|
|
||||||
|
Sets the collation used for client-server interaction on connection. In contrast to `charset`, `collation` does not issue additional queries. If the specified collation is unavailable on the target server, the connection will fail.
|
||||||
|
|
||||||
|
A list of valid charsets for a server is retrievable with `SHOW COLLATION`.
|
||||||
|
|
||||||
|
##### `clientFoundRows`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
`clientFoundRows=true` causes an UPDATE to return the number of matching rows instead of the number of rows changed.
|
||||||
|
|
||||||
|
##### `columnsWithAlias`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
When `columnsWithAlias` is true, calls to `sql.Rows.Columns()` will return the table alias and the column name separated by a dot. For example:
|
||||||
|
|
||||||
|
```
|
||||||
|
SELECT u.id FROM users as u
|
||||||
|
```
|
||||||
|
|
||||||
|
will return `u.id` instead of just `id` if `columnsWithAlias=true`.
|
||||||
|
|
||||||
|
##### `interpolateParams`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
If `interpolateParams` is true, placeholders (`?`) in calls to `db.Query()` and `db.Exec()` are interpolated into a single query string with given parameters. This reduces the number of roundtrips, since the driver has to prepare a statement, execute it with given parameters and close the statement again with `interpolateParams=false`.
|
||||||
|
|
||||||
|
*This can not be used together with the multibyte encodings BIG5, CP932, GB2312, GBK or SJIS. These are blacklisted as they may [introduce a SQL injection vulnerability](http://stackoverflow.com/a/12118602/3430118)!*
|
||||||
|
|
||||||
|
##### `loc`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: string
|
||||||
|
Valid Values: <escaped name>
|
||||||
|
Default: UTC
|
||||||
|
```
|
||||||
|
|
||||||
|
Sets the location for time.Time values (when using `parseTime=true`). *"Local"* sets the system's location. See [time.LoadLocation](https://golang.org/pkg/time/#LoadLocation) for details.
|
||||||
|
|
||||||
|
Note that this sets the location for time.Time values but does not change MySQL's [time_zone setting](https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html). For that see the [time_zone system variable](#system-variables), which can also be set as a DSN parameter.
|
||||||
|
|
||||||
|
Please keep in mind, that param values must be [url.QueryEscape](https://golang.org/pkg/net/url/#QueryEscape)'ed. Alternatively you can manually replace the `/` with `%2F`. For example `US/Pacific` would be `loc=US%2FPacific`.
|
||||||
|
|
||||||
|
##### `maxAllowedPacket`
|
||||||
|
```
|
||||||
|
Type: decimal number
|
||||||
|
Default: 4194304
|
||||||
|
```
|
||||||
|
|
||||||
|
Max packet size allowed in bytes. The default value is 4 MiB and should be adjusted to match the server settings. `maxAllowedPacket=0` can be used to automatically fetch the `max_allowed_packet` variable from server *on every connection*.
|
||||||
|
|
||||||
|
##### `multiStatements`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
Allow multiple statements in one query. While this allows batch queries, it also greatly increases the risk of SQL injections. Only the result of the first query is returned, all other results are silently discarded.
|
||||||
|
|
||||||
|
When `multiStatements` is used, `?` parameters must only be used in the first statement.
|
||||||
|
|
||||||
|
##### `parseTime`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
`parseTime=true` changes the output type of `DATE` and `DATETIME` values to `time.Time` instead of `[]byte` / `string`
|
||||||
|
The date or datetime like `0000-00-00 00:00:00` is converted into zero value of `time.Time`.
|
||||||
|
|
||||||
|
|
||||||
|
##### `readTimeout`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: duration
|
||||||
|
Default: 0
|
||||||
|
```
|
||||||
|
|
||||||
|
I/O read timeout. The value must be a decimal number with a unit suffix (*"ms"*, *"s"*, *"m"*, *"h"*), such as *"30s"*, *"0.5m"* or *"1m30s"*.
|
||||||
|
|
||||||
|
##### `rejectReadOnly`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
`rejectReadOnly=true` causes the driver to reject read-only connections. This
|
||||||
|
is for a possible race condition during an automatic failover, where the mysql
|
||||||
|
client gets connected to a read-only replica after the failover.
|
||||||
|
|
||||||
|
Note that this should be a fairly rare case, as an automatic failover normally
|
||||||
|
happens when the primary is down, and the race condition shouldn't happen
|
||||||
|
unless it comes back up online as soon as the failover is kicked off. On the
|
||||||
|
other hand, when this happens, a MySQL application can get stuck on a
|
||||||
|
read-only connection until restarted. It is however fairly easy to reproduce,
|
||||||
|
for example, using a manual failover on AWS Aurora's MySQL-compatible cluster.
|
||||||
|
|
||||||
|
If you are not relying on read-only transactions to reject writes that aren't
|
||||||
|
supposed to happen, setting this on some MySQL providers (such as AWS Aurora)
|
||||||
|
is safer for failovers.
|
||||||
|
|
||||||
|
Note that ERROR 1290 can be returned for a `read-only` server and this option will
|
||||||
|
cause a retry for that error. However the same error number is used for some
|
||||||
|
other cases. You should ensure your application will never cause an ERROR 1290
|
||||||
|
except for `read-only` mode when enabling this option.
|
||||||
|
|
||||||
|
|
||||||
|
##### `serverPubKey`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: string
|
||||||
|
Valid Values: <name>
|
||||||
|
Default: none
|
||||||
|
```
|
||||||
|
|
||||||
|
Server public keys can be registered with [`mysql.RegisterServerPubKey`](https://godoc.org/github.com/go-sql-driver/mysql#RegisterServerPubKey), which can then be used by the assigned name in the DSN.
|
||||||
|
Public keys are used to transmit encrypted data, e.g. for authentication.
|
||||||
|
If the server's public key is known, it should be set manually to avoid expensive and potentially insecure transmissions of the public key from the server to the client each time it is required.
|
||||||
|
|
||||||
|
|
||||||
|
##### `timeout`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: duration
|
||||||
|
Default: OS default
|
||||||
|
```
|
||||||
|
|
||||||
|
Timeout for establishing connections, aka dial timeout. The value must be a decimal number with a unit suffix (*"ms"*, *"s"*, *"m"*, *"h"*), such as *"30s"*, *"0.5m"* or *"1m30s"*.
|
||||||
|
|
||||||
|
|
||||||
|
##### `tls`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool / string
|
||||||
|
Valid Values: true, false, skip-verify, <name>
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
`tls=true` enables TLS / SSL encrypted connection to the server. Use `skip-verify` if you want to use a self-signed or invalid certificate (server side). Use a custom value registered with [`mysql.RegisterTLSConfig`](https://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig).
|
||||||
|
|
||||||
|
|
||||||
|
##### `writeTimeout`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: duration
|
||||||
|
Default: 0
|
||||||
|
```
|
||||||
|
|
||||||
|
I/O write timeout. The value must be a decimal number with a unit suffix (*"ms"*, *"s"*, *"m"*, *"h"*), such as *"30s"*, *"0.5m"* or *"1m30s"*.
|
||||||
|
|
||||||
|
|
||||||
|
##### System Variables
|
||||||
|
|
||||||
|
Any other parameters are interpreted as system variables:
|
||||||
|
* `<boolean_var>=<value>`: `SET <boolean_var>=<value>`
|
||||||
|
* `<enum_var>=<value>`: `SET <enum_var>=<value>`
|
||||||
|
* `<string_var>=%27<value>%27`: `SET <string_var>='<value>'`
|
||||||
|
|
||||||
|
Rules:
|
||||||
|
* The values for string variables must be quoted with `'`.
|
||||||
|
* The values must also be [url.QueryEscape](http://golang.org/pkg/net/url/#QueryEscape)'ed!
|
||||||
|
(which implies values of string variables must be wrapped with `%27`).
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
* `autocommit=1`: `SET autocommit=1`
|
||||||
|
* [`time_zone=%27Europe%2FParis%27`](https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html): `SET time_zone='Europe/Paris'`
|
||||||
|
* [`tx_isolation=%27REPEATABLE-READ%27`](https://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_tx_isolation): `SET tx_isolation='REPEATABLE-READ'`
|
||||||
|
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
```
|
||||||
|
user@unix(/path/to/socket)/dbname
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
root:pw@unix(/tmp/mysql.sock)/myDatabase?loc=Local
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
user:password@tcp(localhost:5555)/dbname?tls=skip-verify&autocommit=true
|
||||||
|
```
|
||||||
|
|
||||||
|
Treat warnings as errors by setting the system variable [`sql_mode`](https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html):
|
||||||
|
```
|
||||||
|
user:password@/dbname?sql_mode=TRADITIONAL
|
||||||
|
```
|
||||||
|
|
||||||
|
TCP via IPv6:
|
||||||
|
```
|
||||||
|
user:password@tcp([de:ad:be:ef::ca:fe]:80)/dbname?timeout=90s&collation=utf8mb4_unicode_ci
|
||||||
|
```
|
||||||
|
|
||||||
|
TCP on a remote host, e.g. Amazon RDS:
|
||||||
|
```
|
||||||
|
id:password@tcp(your-amazonaws-uri.com:3306)/dbname
|
||||||
|
```
|
||||||
|
|
||||||
|
Google Cloud SQL on App Engine (First Generation MySQL Server):
|
||||||
|
```
|
||||||
|
user@cloudsql(project-id:instance-name)/dbname
|
||||||
|
```
|
||||||
|
|
||||||
|
Google Cloud SQL on App Engine (Second Generation MySQL Server):
|
||||||
|
```
|
||||||
|
user@cloudsql(project-id:regionname:instance-name)/dbname
|
||||||
|
```
|
||||||
|
|
||||||
|
TCP using default port (3306) on localhost:
|
||||||
|
```
|
||||||
|
user:password@tcp/dbname?charset=utf8mb4,utf8&sys_var=esc%40ped
|
||||||
|
```
|
||||||
|
|
||||||
|
Use the default protocol (tcp) and host (localhost:3306):
|
||||||
|
```
|
||||||
|
user:password@/dbname
|
||||||
|
```
|
||||||
|
|
||||||
|
No Database preselected:
|
||||||
|
```
|
||||||
|
user:password@/
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Connection pool and timeouts
|
||||||
|
The connection pool is managed by Go's database/sql package. For details on how to configure the size of the pool and how long connections stay in the pool see `*DB.SetMaxOpenConns`, `*DB.SetMaxIdleConns`, and `*DB.SetConnMaxLifetime` in the [database/sql documentation](https://golang.org/pkg/database/sql/). The read, write, and dial timeouts for each individual connection are configured with the DSN parameters [`readTimeout`](#readtimeout), [`writeTimeout`](#writetimeout), and [`timeout`](#timeout), respectively.
|
||||||
|
|
||||||
|
## `ColumnType` Support
|
||||||
|
This driver supports the [`ColumnType` interface](https://golang.org/pkg/database/sql/#ColumnType) introduced in Go 1.8, with the exception of [`ColumnType.Length()`](https://golang.org/pkg/database/sql/#ColumnType.Length), which is currently not supported.
|
||||||
|
|
||||||
|
## `context.Context` Support
|
||||||
|
Go 1.8 added `database/sql` support for `context.Context`. This driver supports query timeouts and cancellation via contexts.
|
||||||
|
See [context support in the database/sql package](https://golang.org/doc/go1.8#database_sql) for more details.
|
||||||
|
|
||||||
|
|
||||||
|
### `LOAD DATA LOCAL INFILE` support
|
||||||
|
For this feature you need direct access to the package. Therefore you must change the import path (no `_`):
|
||||||
|
```go
|
||||||
|
import "github.com/go-sql-driver/mysql"
|
||||||
|
```
|
||||||
|
|
||||||
|
Files must be whitelisted by registering them with `mysql.RegisterLocalFile(filepath)` (recommended) or the Whitelist check must be deactivated by using the DSN parameter `allowAllFiles=true` ([*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)).
|
||||||
|
|
||||||
|
To use a `io.Reader` a handler function must be registered with `mysql.RegisterReaderHandler(name, handler)` which returns a `io.Reader` or `io.ReadCloser`. The Reader is available with the filepath `Reader::<name>` then. Choose different names for different handlers and `DeregisterReaderHandler` when you don't need it anymore.
|
||||||
|
|
||||||
|
See the [godoc of Go-MySQL-Driver](https://godoc.org/github.com/go-sql-driver/mysql "golang mysql driver documentation") for details.
|
||||||
|
|
||||||
|
|
||||||
|
### `time.Time` support
|
||||||
|
The default internal output type of MySQL `DATE` and `DATETIME` values is `[]byte` which allows you to scan the value into a `[]byte`, `string` or `sql.RawBytes` variable in your program.
|
||||||
|
|
||||||
|
However, many want to scan MySQL `DATE` and `DATETIME` values into `time.Time` variables, which is the logical opposite in Go to `DATE` and `DATETIME` in MySQL. You can do that by changing the internal output type from `[]byte` to `time.Time` with the DSN parameter `parseTime=true`. You can set the default [`time.Time` location](https://golang.org/pkg/time/#Location) with the `loc` DSN parameter.
|
||||||
|
|
||||||
|
**Caution:** As of Go 1.1, this makes `time.Time` the only variable type you can scan `DATE` and `DATETIME` values into. This breaks for example [`sql.RawBytes` support](https://github.com/go-sql-driver/mysql/wiki/Examples#rawbytes).
|
||||||
|
|
||||||
|
Alternatively you can use the [`NullTime`](https://godoc.org/github.com/go-sql-driver/mysql#NullTime) type as the scan destination, which works with both `time.Time` and `string` / `[]byte`.
|
||||||
|
|
||||||
|
|
||||||
|
### Unicode support
|
||||||
|
Since version 1.1 Go-MySQL-Driver automatically uses the collation `utf8_general_ci` by default.
|
||||||
|
|
||||||
|
Other collations / charsets can be set using the [`collation`](#collation) DSN parameter.
|
||||||
|
|
||||||
|
Version 1.0 of the driver recommended adding `&charset=utf8` (alias for `SET NAMES utf8`) to the DSN to enable proper UTF-8 support. This is not necessary anymore. The [`collation`](#collation) parameter should be preferred to set another collation / charset than the default.
|
||||||
|
|
||||||
|
See http://dev.mysql.com/doc/refman/5.7/en/charset-unicode.html for more details on MySQL's Unicode support.
|
||||||
|
|
||||||
|
## Testing / Development
|
||||||
|
To run the driver tests you may need to adjust the configuration. See the [Testing Wiki-Page](https://github.com/go-sql-driver/mysql/wiki/Testing "Testing") for details.
|
||||||
|
|
||||||
|
Go-MySQL-Driver is not feature-complete yet. Your help is very appreciated.
|
||||||
|
If you want to contribute, you can work on an [open issue](https://github.com/go-sql-driver/mysql/issues?state=open) or review a [pull request](https://github.com/go-sql-driver/mysql/pulls).
|
||||||
|
|
||||||
|
See the [Contribution Guidelines](https://github.com/go-sql-driver/mysql/blob/master/CONTRIBUTING.md) for details.
|
||||||
|
|
||||||
|
---------------------------------------
|
||||||
|
|
||||||
|
## License
|
||||||
|
Go-MySQL-Driver is licensed under the [Mozilla Public License Version 2.0](https://raw.github.com/go-sql-driver/mysql/master/LICENSE)
|
||||||
|
|
||||||
|
Mozilla summarizes the license scope as follows:
|
||||||
|
> MPL: The copyleft applies to any files containing MPLed code.
|
||||||
|
|
||||||
|
|
||||||
|
That means:
|
||||||
|
* You can **use** the **unchanged** source code both in private and commercially.
|
||||||
|
* When distributing, you **must publish** the source code of any **changed files** licensed under the MPL 2.0 under a) the MPL 2.0 itself or b) a compatible license (e.g. GPL 3.0 or Apache License 2.0).
|
||||||
|
* You **needn't publish** the source code of your library as long as the files licensed under the MPL 2.0 are **unchanged**.
|
||||||
|
|
||||||
|
Please read the [MPL 2.0 FAQ](https://www.mozilla.org/en-US/MPL/2.0/FAQ/) if you have further questions regarding the license.
|
||||||
|
|
||||||
|
You can read the full terms here: [LICENSE](https://raw.github.com/go-sql-driver/mysql/master/LICENSE).
|
||||||
|
|
||||||
|
![Go Gopher and MySQL Dolphin](https://raw.github.com/wiki/go-sql-driver/mysql/go-mysql-driver_m.jpg "Golang Gopher transporting the MySQL Dolphin in a wheelbarrow")
|
||||||
|
|
19
vendor/github.com/go-sql-driver/mysql/appengine.go
generated
vendored
Normal file
19
vendor/github.com/go-sql-driver/mysql/appengine.go
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
// +build appengine
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"google.golang.org/appengine/cloudsql"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterDial("cloudsql", cloudsql.Dial)
|
||||||
|
}
|
420
vendor/github.com/go-sql-driver/mysql/auth.go
generated
vendored
Normal file
420
vendor/github.com/go-sql-driver/mysql/auth.go
generated
vendored
Normal file
@ -0,0 +1,420 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/sha1"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// server pub keys registry
|
||||||
|
var (
|
||||||
|
serverPubKeyLock sync.RWMutex
|
||||||
|
serverPubKeyRegistry map[string]*rsa.PublicKey
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegisterServerPubKey registers a server RSA public key which can be used to
|
||||||
|
// send data in a secure manner to the server without receiving the public key
|
||||||
|
// in a potentially insecure way from the server first.
|
||||||
|
// Registered keys can afterwards be used adding serverPubKey=<name> to the DSN.
|
||||||
|
//
|
||||||
|
// Note: The provided rsa.PublicKey instance is exclusively owned by the driver
|
||||||
|
// after registering it and may not be modified.
|
||||||
|
//
|
||||||
|
// data, err := ioutil.ReadFile("mykey.pem")
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatal(err)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// block, _ := pem.Decode(data)
|
||||||
|
// if block == nil || block.Type != "PUBLIC KEY" {
|
||||||
|
// log.Fatal("failed to decode PEM block containing public key")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// pub, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatal(err)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if rsaPubKey, ok := pub.(*rsa.PublicKey); ok {
|
||||||
|
// mysql.RegisterServerPubKey("mykey", rsaPubKey)
|
||||||
|
// } else {
|
||||||
|
// log.Fatal("not a RSA public key")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
func RegisterServerPubKey(name string, pubKey *rsa.PublicKey) {
|
||||||
|
serverPubKeyLock.Lock()
|
||||||
|
if serverPubKeyRegistry == nil {
|
||||||
|
serverPubKeyRegistry = make(map[string]*rsa.PublicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
serverPubKeyRegistry[name] = pubKey
|
||||||
|
serverPubKeyLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeregisterServerPubKey removes the public key registered with the given name.
|
||||||
|
func DeregisterServerPubKey(name string) {
|
||||||
|
serverPubKeyLock.Lock()
|
||||||
|
if serverPubKeyRegistry != nil {
|
||||||
|
delete(serverPubKeyRegistry, name)
|
||||||
|
}
|
||||||
|
serverPubKeyLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getServerPubKey(name string) (pubKey *rsa.PublicKey) {
|
||||||
|
serverPubKeyLock.RLock()
|
||||||
|
if v, ok := serverPubKeyRegistry[name]; ok {
|
||||||
|
pubKey = v
|
||||||
|
}
|
||||||
|
serverPubKeyLock.RUnlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash password using pre 4.1 (old password) method
|
||||||
|
// https://github.com/atcurtis/mariadb/blob/master/mysys/my_rnd.c
|
||||||
|
type myRnd struct {
|
||||||
|
seed1, seed2 uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
const myRndMaxVal = 0x3FFFFFFF
|
||||||
|
|
||||||
|
// Pseudo random number generator
|
||||||
|
func newMyRnd(seed1, seed2 uint32) *myRnd {
|
||||||
|
return &myRnd{
|
||||||
|
seed1: seed1 % myRndMaxVal,
|
||||||
|
seed2: seed2 % myRndMaxVal,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tested to be equivalent to MariaDB's floating point variant
|
||||||
|
// http://play.golang.org/p/QHvhd4qved
|
||||||
|
// http://play.golang.org/p/RG0q4ElWDx
|
||||||
|
func (r *myRnd) NextByte() byte {
|
||||||
|
r.seed1 = (r.seed1*3 + r.seed2) % myRndMaxVal
|
||||||
|
r.seed2 = (r.seed1 + r.seed2 + 33) % myRndMaxVal
|
||||||
|
|
||||||
|
return byte(uint64(r.seed1) * 31 / myRndMaxVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate binary hash from byte string using insecure pre 4.1 method
|
||||||
|
func pwHash(password []byte) (result [2]uint32) {
|
||||||
|
var add uint32 = 7
|
||||||
|
var tmp uint32
|
||||||
|
|
||||||
|
result[0] = 1345345333
|
||||||
|
result[1] = 0x12345671
|
||||||
|
|
||||||
|
for _, c := range password {
|
||||||
|
// skip spaces and tabs in password
|
||||||
|
if c == ' ' || c == '\t' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = uint32(c)
|
||||||
|
result[0] ^= (((result[0] & 63) + add) * tmp) + (result[0] << 8)
|
||||||
|
result[1] += (result[1] << 8) ^ result[0]
|
||||||
|
add += tmp
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove sign bit (1<<31)-1)
|
||||||
|
result[0] &= 0x7FFFFFFF
|
||||||
|
result[1] &= 0x7FFFFFFF
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash password using insecure pre 4.1 method
|
||||||
|
func scrambleOldPassword(scramble []byte, password string) []byte {
|
||||||
|
if len(password) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
scramble = scramble[:8]
|
||||||
|
|
||||||
|
hashPw := pwHash([]byte(password))
|
||||||
|
hashSc := pwHash(scramble)
|
||||||
|
|
||||||
|
r := newMyRnd(hashPw[0]^hashSc[0], hashPw[1]^hashSc[1])
|
||||||
|
|
||||||
|
var out [8]byte
|
||||||
|
for i := range out {
|
||||||
|
out[i] = r.NextByte() + 64
|
||||||
|
}
|
||||||
|
|
||||||
|
mask := r.NextByte()
|
||||||
|
for i := range out {
|
||||||
|
out[i] ^= mask
|
||||||
|
}
|
||||||
|
|
||||||
|
return out[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash password using 4.1+ method (SHA1)
|
||||||
|
func scramblePassword(scramble []byte, password string) []byte {
|
||||||
|
if len(password) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// stage1Hash = SHA1(password)
|
||||||
|
crypt := sha1.New()
|
||||||
|
crypt.Write([]byte(password))
|
||||||
|
stage1 := crypt.Sum(nil)
|
||||||
|
|
||||||
|
// scrambleHash = SHA1(scramble + SHA1(stage1Hash))
|
||||||
|
// inner Hash
|
||||||
|
crypt.Reset()
|
||||||
|
crypt.Write(stage1)
|
||||||
|
hash := crypt.Sum(nil)
|
||||||
|
|
||||||
|
// outer Hash
|
||||||
|
crypt.Reset()
|
||||||
|
crypt.Write(scramble)
|
||||||
|
crypt.Write(hash)
|
||||||
|
scramble = crypt.Sum(nil)
|
||||||
|
|
||||||
|
// token = scrambleHash XOR stage1Hash
|
||||||
|
for i := range scramble {
|
||||||
|
scramble[i] ^= stage1[i]
|
||||||
|
}
|
||||||
|
return scramble
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash password using MySQL 8+ method (SHA256)
|
||||||
|
func scrambleSHA256Password(scramble []byte, password string) []byte {
|
||||||
|
if len(password) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// XOR(SHA256(password), SHA256(SHA256(SHA256(password)), scramble))
|
||||||
|
|
||||||
|
crypt := sha256.New()
|
||||||
|
crypt.Write([]byte(password))
|
||||||
|
message1 := crypt.Sum(nil)
|
||||||
|
|
||||||
|
crypt.Reset()
|
||||||
|
crypt.Write(message1)
|
||||||
|
message1Hash := crypt.Sum(nil)
|
||||||
|
|
||||||
|
crypt.Reset()
|
||||||
|
crypt.Write(message1Hash)
|
||||||
|
crypt.Write(scramble)
|
||||||
|
message2 := crypt.Sum(nil)
|
||||||
|
|
||||||
|
for i := range message1 {
|
||||||
|
message1[i] ^= message2[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
return message1
|
||||||
|
}
|
||||||
|
|
||||||
|
func encryptPassword(password string, seed []byte, pub *rsa.PublicKey) ([]byte, error) {
|
||||||
|
plain := make([]byte, len(password)+1)
|
||||||
|
copy(plain, password)
|
||||||
|
for i := range plain {
|
||||||
|
j := i % len(seed)
|
||||||
|
plain[i] ^= seed[j]
|
||||||
|
}
|
||||||
|
sha1 := sha1.New()
|
||||||
|
return rsa.EncryptOAEP(sha1, rand.Reader, pub, plain, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) sendEncryptedPassword(seed []byte, pub *rsa.PublicKey) error {
|
||||||
|
enc, err := encryptPassword(mc.cfg.Passwd, seed, pub)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return mc.writeAuthSwitchPacket(enc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) auth(authData []byte, plugin string) ([]byte, error) {
|
||||||
|
switch plugin {
|
||||||
|
case "caching_sha2_password":
|
||||||
|
authResp := scrambleSHA256Password(authData, mc.cfg.Passwd)
|
||||||
|
return authResp, nil
|
||||||
|
|
||||||
|
case "mysql_old_password":
|
||||||
|
if !mc.cfg.AllowOldPasswords {
|
||||||
|
return nil, ErrOldPassword
|
||||||
|
}
|
||||||
|
// Note: there are edge cases where this should work but doesn't;
|
||||||
|
// this is currently "wontfix":
|
||||||
|
// https://github.com/go-sql-driver/mysql/issues/184
|
||||||
|
authResp := append(scrambleOldPassword(authData[:8], mc.cfg.Passwd), 0)
|
||||||
|
return authResp, nil
|
||||||
|
|
||||||
|
case "mysql_clear_password":
|
||||||
|
if !mc.cfg.AllowCleartextPasswords {
|
||||||
|
return nil, ErrCleartextPassword
|
||||||
|
}
|
||||||
|
// http://dev.mysql.com/doc/refman/5.7/en/cleartext-authentication-plugin.html
|
||||||
|
// http://dev.mysql.com/doc/refman/5.7/en/pam-authentication-plugin.html
|
||||||
|
return append([]byte(mc.cfg.Passwd), 0), nil
|
||||||
|
|
||||||
|
case "mysql_native_password":
|
||||||
|
if !mc.cfg.AllowNativePasswords {
|
||||||
|
return nil, ErrNativePassword
|
||||||
|
}
|
||||||
|
// https://dev.mysql.com/doc/internals/en/secure-password-authentication.html
|
||||||
|
// Native password authentication only need and will need 20-byte challenge.
|
||||||
|
authResp := scramblePassword(authData[:20], mc.cfg.Passwd)
|
||||||
|
return authResp, nil
|
||||||
|
|
||||||
|
case "sha256_password":
|
||||||
|
if len(mc.cfg.Passwd) == 0 {
|
||||||
|
return []byte{0}, nil
|
||||||
|
}
|
||||||
|
if mc.cfg.tls != nil || mc.cfg.Net == "unix" {
|
||||||
|
// write cleartext auth packet
|
||||||
|
return append([]byte(mc.cfg.Passwd), 0), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKey := mc.cfg.pubKey
|
||||||
|
if pubKey == nil {
|
||||||
|
// request public key from server
|
||||||
|
return []byte{1}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// encrypted password
|
||||||
|
enc, err := encryptPassword(mc.cfg.Passwd, authData, pubKey)
|
||||||
|
return enc, err
|
||||||
|
|
||||||
|
default:
|
||||||
|
errLog.Print("unknown auth plugin:", plugin)
|
||||||
|
return nil, ErrUnknownPlugin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) handleAuthResult(oldAuthData []byte, plugin string) error {
|
||||||
|
// Read Result Packet
|
||||||
|
authData, newPlugin, err := mc.readAuthResult()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle auth plugin switch, if requested
|
||||||
|
if newPlugin != "" {
|
||||||
|
// If CLIENT_PLUGIN_AUTH capability is not supported, no new cipher is
|
||||||
|
// sent and we have to keep using the cipher sent in the init packet.
|
||||||
|
if authData == nil {
|
||||||
|
authData = oldAuthData
|
||||||
|
} else {
|
||||||
|
// copy data from read buffer to owned slice
|
||||||
|
copy(oldAuthData, authData)
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin = newPlugin
|
||||||
|
|
||||||
|
authResp, err := mc.auth(authData, plugin)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = mc.writeAuthSwitchPacket(authResp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read Result Packet
|
||||||
|
authData, newPlugin, err = mc.readAuthResult()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do not allow to change the auth plugin more than once
|
||||||
|
if newPlugin != "" {
|
||||||
|
return ErrMalformPkt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch plugin {
|
||||||
|
|
||||||
|
// https://insidemysql.com/preparing-your-community-connector-for-mysql-8-part-2-sha256/
|
||||||
|
case "caching_sha2_password":
|
||||||
|
switch len(authData) {
|
||||||
|
case 0:
|
||||||
|
return nil // auth successful
|
||||||
|
case 1:
|
||||||
|
switch authData[0] {
|
||||||
|
case cachingSha2PasswordFastAuthSuccess:
|
||||||
|
if err = mc.readResultOK(); err == nil {
|
||||||
|
return nil // auth successful
|
||||||
|
}
|
||||||
|
|
||||||
|
case cachingSha2PasswordPerformFullAuthentication:
|
||||||
|
if mc.cfg.tls != nil || mc.cfg.Net == "unix" {
|
||||||
|
// write cleartext auth packet
|
||||||
|
err = mc.writeAuthSwitchPacket(append([]byte(mc.cfg.Passwd), 0))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
pubKey := mc.cfg.pubKey
|
||||||
|
if pubKey == nil {
|
||||||
|
// request public key from server
|
||||||
|
data := mc.buf.takeSmallBuffer(4 + 1)
|
||||||
|
data[4] = cachingSha2PasswordRequestPublicKey
|
||||||
|
mc.writePacket(data)
|
||||||
|
|
||||||
|
// parse public key
|
||||||
|
data, err := mc.readPacket()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
block, _ := pem.Decode(data[1:])
|
||||||
|
pkix, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
pubKey = pkix.(*rsa.PublicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// send encrypted password
|
||||||
|
err = mc.sendEncryptedPassword(oldAuthData, pubKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mc.readResultOK()
|
||||||
|
|
||||||
|
default:
|
||||||
|
return ErrMalformPkt
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return ErrMalformPkt
|
||||||
|
}
|
||||||
|
|
||||||
|
case "sha256_password":
|
||||||
|
switch len(authData) {
|
||||||
|
case 0:
|
||||||
|
return nil // auth successful
|
||||||
|
default:
|
||||||
|
block, _ := pem.Decode(authData)
|
||||||
|
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// send encrypted password
|
||||||
|
err = mc.sendEncryptedPassword(oldAuthData, pub.(*rsa.PublicKey))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return mc.readResultOK()
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil // auth successful
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
147
vendor/github.com/go-sql-driver/mysql/buffer.go
generated
vendored
Normal file
147
vendor/github.com/go-sql-driver/mysql/buffer.go
generated
vendored
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultBufSize = 4096
|
||||||
|
|
||||||
|
// A buffer which is used for both reading and writing.
|
||||||
|
// This is possible since communication on each connection is synchronous.
|
||||||
|
// In other words, we can't write and read simultaneously on the same connection.
|
||||||
|
// The buffer is similar to bufio.Reader / Writer but zero-copy-ish
|
||||||
|
// Also highly optimized for this particular use case.
|
||||||
|
type buffer struct {
|
||||||
|
buf []byte
|
||||||
|
nc net.Conn
|
||||||
|
idx int
|
||||||
|
length int
|
||||||
|
timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBuffer(nc net.Conn) buffer {
|
||||||
|
var b [defaultBufSize]byte
|
||||||
|
return buffer{
|
||||||
|
buf: b[:],
|
||||||
|
nc: nc,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fill reads into the buffer until at least _need_ bytes are in it
|
||||||
|
func (b *buffer) fill(need int) error {
|
||||||
|
n := b.length
|
||||||
|
|
||||||
|
// move existing data to the beginning
|
||||||
|
if n > 0 && b.idx > 0 {
|
||||||
|
copy(b.buf[0:n], b.buf[b.idx:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// grow buffer if necessary
|
||||||
|
// TODO: let the buffer shrink again at some point
|
||||||
|
// Maybe keep the org buf slice and swap back?
|
||||||
|
if need > len(b.buf) {
|
||||||
|
// Round up to the next multiple of the default size
|
||||||
|
newBuf := make([]byte, ((need/defaultBufSize)+1)*defaultBufSize)
|
||||||
|
copy(newBuf, b.buf)
|
||||||
|
b.buf = newBuf
|
||||||
|
}
|
||||||
|
|
||||||
|
b.idx = 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
if b.timeout > 0 {
|
||||||
|
if err := b.nc.SetReadDeadline(time.Now().Add(b.timeout)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nn, err := b.nc.Read(b.buf[n:])
|
||||||
|
n += nn
|
||||||
|
|
||||||
|
switch err {
|
||||||
|
case nil:
|
||||||
|
if n < need {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
b.length = n
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case io.EOF:
|
||||||
|
if n >= need {
|
||||||
|
b.length = n
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns next N bytes from buffer.
|
||||||
|
// The returned slice is only guaranteed to be valid until the next read
|
||||||
|
func (b *buffer) readNext(need int) ([]byte, error) {
|
||||||
|
if b.length < need {
|
||||||
|
// refill
|
||||||
|
if err := b.fill(need); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
offset := b.idx
|
||||||
|
b.idx += need
|
||||||
|
b.length -= need
|
||||||
|
return b.buf[offset:b.idx], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns a buffer with the requested size.
|
||||||
|
// If possible, a slice from the existing buffer is returned.
|
||||||
|
// Otherwise a bigger buffer is made.
|
||||||
|
// Only one buffer (total) can be used at a time.
|
||||||
|
func (b *buffer) takeBuffer(length int) []byte {
|
||||||
|
if b.length > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// test (cheap) general case first
|
||||||
|
if length <= defaultBufSize || length <= cap(b.buf) {
|
||||||
|
return b.buf[:length]
|
||||||
|
}
|
||||||
|
|
||||||
|
if length < maxPacketSize {
|
||||||
|
b.buf = make([]byte, length)
|
||||||
|
return b.buf
|
||||||
|
}
|
||||||
|
return make([]byte, length)
|
||||||
|
}
|
||||||
|
|
||||||
|
// shortcut which can be used if the requested buffer is guaranteed to be
|
||||||
|
// smaller than defaultBufSize
|
||||||
|
// Only one buffer (total) can be used at a time.
|
||||||
|
func (b *buffer) takeSmallBuffer(length int) []byte {
|
||||||
|
if b.length > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return b.buf[:length]
|
||||||
|
}
|
||||||
|
|
||||||
|
// takeCompleteBuffer returns the complete existing buffer.
|
||||||
|
// This can be used if the necessary buffer size is unknown.
|
||||||
|
// Only one buffer (total) can be used at a time.
|
||||||
|
func (b *buffer) takeCompleteBuffer() []byte {
|
||||||
|
if b.length > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return b.buf
|
||||||
|
}
|
251
vendor/github.com/go-sql-driver/mysql/collations.go
generated
vendored
Normal file
251
vendor/github.com/go-sql-driver/mysql/collations.go
generated
vendored
Normal file
@ -0,0 +1,251 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2014 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
const defaultCollation = "utf8_general_ci"
|
||||||
|
const binaryCollation = "binary"
|
||||||
|
|
||||||
|
// A list of available collations mapped to the internal ID.
|
||||||
|
// To update this map use the following MySQL query:
|
||||||
|
// SELECT COLLATION_NAME, ID FROM information_schema.COLLATIONS
|
||||||
|
var collations = map[string]byte{
|
||||||
|
"big5_chinese_ci": 1,
|
||||||
|
"latin2_czech_cs": 2,
|
||||||
|
"dec8_swedish_ci": 3,
|
||||||
|
"cp850_general_ci": 4,
|
||||||
|
"latin1_german1_ci": 5,
|
||||||
|
"hp8_english_ci": 6,
|
||||||
|
"koi8r_general_ci": 7,
|
||||||
|
"latin1_swedish_ci": 8,
|
||||||
|
"latin2_general_ci": 9,
|
||||||
|
"swe7_swedish_ci": 10,
|
||||||
|
"ascii_general_ci": 11,
|
||||||
|
"ujis_japanese_ci": 12,
|
||||||
|
"sjis_japanese_ci": 13,
|
||||||
|
"cp1251_bulgarian_ci": 14,
|
||||||
|
"latin1_danish_ci": 15,
|
||||||
|
"hebrew_general_ci": 16,
|
||||||
|
"tis620_thai_ci": 18,
|
||||||
|
"euckr_korean_ci": 19,
|
||||||
|
"latin7_estonian_cs": 20,
|
||||||
|
"latin2_hungarian_ci": 21,
|
||||||
|
"koi8u_general_ci": 22,
|
||||||
|
"cp1251_ukrainian_ci": 23,
|
||||||
|
"gb2312_chinese_ci": 24,
|
||||||
|
"greek_general_ci": 25,
|
||||||
|
"cp1250_general_ci": 26,
|
||||||
|
"latin2_croatian_ci": 27,
|
||||||
|
"gbk_chinese_ci": 28,
|
||||||
|
"cp1257_lithuanian_ci": 29,
|
||||||
|
"latin5_turkish_ci": 30,
|
||||||
|
"latin1_german2_ci": 31,
|
||||||
|
"armscii8_general_ci": 32,
|
||||||
|
"utf8_general_ci": 33,
|
||||||
|
"cp1250_czech_cs": 34,
|
||||||
|
"ucs2_general_ci": 35,
|
||||||
|
"cp866_general_ci": 36,
|
||||||
|
"keybcs2_general_ci": 37,
|
||||||
|
"macce_general_ci": 38,
|
||||||
|
"macroman_general_ci": 39,
|
||||||
|
"cp852_general_ci": 40,
|
||||||
|
"latin7_general_ci": 41,
|
||||||
|
"latin7_general_cs": 42,
|
||||||
|
"macce_bin": 43,
|
||||||
|
"cp1250_croatian_ci": 44,
|
||||||
|
"utf8mb4_general_ci": 45,
|
||||||
|
"utf8mb4_bin": 46,
|
||||||
|
"latin1_bin": 47,
|
||||||
|
"latin1_general_ci": 48,
|
||||||
|
"latin1_general_cs": 49,
|
||||||
|
"cp1251_bin": 50,
|
||||||
|
"cp1251_general_ci": 51,
|
||||||
|
"cp1251_general_cs": 52,
|
||||||
|
"macroman_bin": 53,
|
||||||
|
"utf16_general_ci": 54,
|
||||||
|
"utf16_bin": 55,
|
||||||
|
"utf16le_general_ci": 56,
|
||||||
|
"cp1256_general_ci": 57,
|
||||||
|
"cp1257_bin": 58,
|
||||||
|
"cp1257_general_ci": 59,
|
||||||
|
"utf32_general_ci": 60,
|
||||||
|
"utf32_bin": 61,
|
||||||
|
"utf16le_bin": 62,
|
||||||
|
"binary": 63,
|
||||||
|
"armscii8_bin": 64,
|
||||||
|
"ascii_bin": 65,
|
||||||
|
"cp1250_bin": 66,
|
||||||
|
"cp1256_bin": 67,
|
||||||
|
"cp866_bin": 68,
|
||||||
|
"dec8_bin": 69,
|
||||||
|
"greek_bin": 70,
|
||||||
|
"hebrew_bin": 71,
|
||||||
|
"hp8_bin": 72,
|
||||||
|
"keybcs2_bin": 73,
|
||||||
|
"koi8r_bin": 74,
|
||||||
|
"koi8u_bin": 75,
|
||||||
|
"latin2_bin": 77,
|
||||||
|
"latin5_bin": 78,
|
||||||
|
"latin7_bin": 79,
|
||||||
|
"cp850_bin": 80,
|
||||||
|
"cp852_bin": 81,
|
||||||
|
"swe7_bin": 82,
|
||||||
|
"utf8_bin": 83,
|
||||||
|
"big5_bin": 84,
|
||||||
|
"euckr_bin": 85,
|
||||||
|
"gb2312_bin": 86,
|
||||||
|
"gbk_bin": 87,
|
||||||
|
"sjis_bin": 88,
|
||||||
|
"tis620_bin": 89,
|
||||||
|
"ucs2_bin": 90,
|
||||||
|
"ujis_bin": 91,
|
||||||
|
"geostd8_general_ci": 92,
|
||||||
|
"geostd8_bin": 93,
|
||||||
|
"latin1_spanish_ci": 94,
|
||||||
|
"cp932_japanese_ci": 95,
|
||||||
|
"cp932_bin": 96,
|
||||||
|
"eucjpms_japanese_ci": 97,
|
||||||
|
"eucjpms_bin": 98,
|
||||||
|
"cp1250_polish_ci": 99,
|
||||||
|
"utf16_unicode_ci": 101,
|
||||||
|
"utf16_icelandic_ci": 102,
|
||||||
|
"utf16_latvian_ci": 103,
|
||||||
|
"utf16_romanian_ci": 104,
|
||||||
|
"utf16_slovenian_ci": 105,
|
||||||
|
"utf16_polish_ci": 106,
|
||||||
|
"utf16_estonian_ci": 107,
|
||||||
|
"utf16_spanish_ci": 108,
|
||||||
|
"utf16_swedish_ci": 109,
|
||||||
|
"utf16_turkish_ci": 110,
|
||||||
|
"utf16_czech_ci": 111,
|
||||||
|
"utf16_danish_ci": 112,
|
||||||
|
"utf16_lithuanian_ci": 113,
|
||||||
|
"utf16_slovak_ci": 114,
|
||||||
|
"utf16_spanish2_ci": 115,
|
||||||
|
"utf16_roman_ci": 116,
|
||||||
|
"utf16_persian_ci": 117,
|
||||||
|
"utf16_esperanto_ci": 118,
|
||||||
|
"utf16_hungarian_ci": 119,
|
||||||
|
"utf16_sinhala_ci": 120,
|
||||||
|
"utf16_german2_ci": 121,
|
||||||
|
"utf16_croatian_ci": 122,
|
||||||
|
"utf16_unicode_520_ci": 123,
|
||||||
|
"utf16_vietnamese_ci": 124,
|
||||||
|
"ucs2_unicode_ci": 128,
|
||||||
|
"ucs2_icelandic_ci": 129,
|
||||||
|
"ucs2_latvian_ci": 130,
|
||||||
|
"ucs2_romanian_ci": 131,
|
||||||
|
"ucs2_slovenian_ci": 132,
|
||||||
|
"ucs2_polish_ci": 133,
|
||||||
|
"ucs2_estonian_ci": 134,
|
||||||
|
"ucs2_spanish_ci": 135,
|
||||||
|
"ucs2_swedish_ci": 136,
|
||||||
|
"ucs2_turkish_ci": 137,
|
||||||
|
"ucs2_czech_ci": 138,
|
||||||
|
"ucs2_danish_ci": 139,
|
||||||
|
"ucs2_lithuanian_ci": 140,
|
||||||
|
"ucs2_slovak_ci": 141,
|
||||||
|
"ucs2_spanish2_ci": 142,
|
||||||
|
"ucs2_roman_ci": 143,
|
||||||
|
"ucs2_persian_ci": 144,
|
||||||
|
"ucs2_esperanto_ci": 145,
|
||||||
|
"ucs2_hungarian_ci": 146,
|
||||||
|
"ucs2_sinhala_ci": 147,
|
||||||
|
"ucs2_german2_ci": 148,
|
||||||
|
"ucs2_croatian_ci": 149,
|
||||||
|
"ucs2_unicode_520_ci": 150,
|
||||||
|
"ucs2_vietnamese_ci": 151,
|
||||||
|
"ucs2_general_mysql500_ci": 159,
|
||||||
|
"utf32_unicode_ci": 160,
|
||||||
|
"utf32_icelandic_ci": 161,
|
||||||
|
"utf32_latvian_ci": 162,
|
||||||
|
"utf32_romanian_ci": 163,
|
||||||
|
"utf32_slovenian_ci": 164,
|
||||||
|
"utf32_polish_ci": 165,
|
||||||
|
"utf32_estonian_ci": 166,
|
||||||
|
"utf32_spanish_ci": 167,
|
||||||
|
"utf32_swedish_ci": 168,
|
||||||
|
"utf32_turkish_ci": 169,
|
||||||
|
"utf32_czech_ci": 170,
|
||||||
|
"utf32_danish_ci": 171,
|
||||||
|
"utf32_lithuanian_ci": 172,
|
||||||
|
"utf32_slovak_ci": 173,
|
||||||
|
"utf32_spanish2_ci": 174,
|
||||||
|
"utf32_roman_ci": 175,
|
||||||
|
"utf32_persian_ci": 176,
|
||||||
|
"utf32_esperanto_ci": 177,
|
||||||
|
"utf32_hungarian_ci": 178,
|
||||||
|
"utf32_sinhala_ci": 179,
|
||||||
|
"utf32_german2_ci": 180,
|
||||||
|
"utf32_croatian_ci": 181,
|
||||||
|
"utf32_unicode_520_ci": 182,
|
||||||
|
"utf32_vietnamese_ci": 183,
|
||||||
|
"utf8_unicode_ci": 192,
|
||||||
|
"utf8_icelandic_ci": 193,
|
||||||
|
"utf8_latvian_ci": 194,
|
||||||
|
"utf8_romanian_ci": 195,
|
||||||
|
"utf8_slovenian_ci": 196,
|
||||||
|
"utf8_polish_ci": 197,
|
||||||
|
"utf8_estonian_ci": 198,
|
||||||
|
"utf8_spanish_ci": 199,
|
||||||
|
"utf8_swedish_ci": 200,
|
||||||
|
"utf8_turkish_ci": 201,
|
||||||
|
"utf8_czech_ci": 202,
|
||||||
|
"utf8_danish_ci": 203,
|
||||||
|
"utf8_lithuanian_ci": 204,
|
||||||
|
"utf8_slovak_ci": 205,
|
||||||
|
"utf8_spanish2_ci": 206,
|
||||||
|
"utf8_roman_ci": 207,
|
||||||
|
"utf8_persian_ci": 208,
|
||||||
|
"utf8_esperanto_ci": 209,
|
||||||
|
"utf8_hungarian_ci": 210,
|
||||||
|
"utf8_sinhala_ci": 211,
|
||||||
|
"utf8_german2_ci": 212,
|
||||||
|
"utf8_croatian_ci": 213,
|
||||||
|
"utf8_unicode_520_ci": 214,
|
||||||
|
"utf8_vietnamese_ci": 215,
|
||||||
|
"utf8_general_mysql500_ci": 223,
|
||||||
|
"utf8mb4_unicode_ci": 224,
|
||||||
|
"utf8mb4_icelandic_ci": 225,
|
||||||
|
"utf8mb4_latvian_ci": 226,
|
||||||
|
"utf8mb4_romanian_ci": 227,
|
||||||
|
"utf8mb4_slovenian_ci": 228,
|
||||||
|
"utf8mb4_polish_ci": 229,
|
||||||
|
"utf8mb4_estonian_ci": 230,
|
||||||
|
"utf8mb4_spanish_ci": 231,
|
||||||
|
"utf8mb4_swedish_ci": 232,
|
||||||
|
"utf8mb4_turkish_ci": 233,
|
||||||
|
"utf8mb4_czech_ci": 234,
|
||||||
|
"utf8mb4_danish_ci": 235,
|
||||||
|
"utf8mb4_lithuanian_ci": 236,
|
||||||
|
"utf8mb4_slovak_ci": 237,
|
||||||
|
"utf8mb4_spanish2_ci": 238,
|
||||||
|
"utf8mb4_roman_ci": 239,
|
||||||
|
"utf8mb4_persian_ci": 240,
|
||||||
|
"utf8mb4_esperanto_ci": 241,
|
||||||
|
"utf8mb4_hungarian_ci": 242,
|
||||||
|
"utf8mb4_sinhala_ci": 243,
|
||||||
|
"utf8mb4_german2_ci": 244,
|
||||||
|
"utf8mb4_croatian_ci": 245,
|
||||||
|
"utf8mb4_unicode_520_ci": 246,
|
||||||
|
"utf8mb4_vietnamese_ci": 247,
|
||||||
|
}
|
||||||
|
|
||||||
|
// A blacklist of collations which is unsafe to interpolate parameters.
|
||||||
|
// These multibyte encodings may contains 0x5c (`\`) in their trailing bytes.
|
||||||
|
var unsafeCollations = map[string]bool{
|
||||||
|
"big5_chinese_ci": true,
|
||||||
|
"sjis_japanese_ci": true,
|
||||||
|
"gbk_chinese_ci": true,
|
||||||
|
"big5_bin": true,
|
||||||
|
"gb2312_bin": true,
|
||||||
|
"gbk_bin": true,
|
||||||
|
"sjis_bin": true,
|
||||||
|
"cp932_japanese_ci": true,
|
||||||
|
"cp932_bin": true,
|
||||||
|
}
|
461
vendor/github.com/go-sql-driver/mysql/connection.go
generated
vendored
Normal file
461
vendor/github.com/go-sql-driver/mysql/connection.go
generated
vendored
Normal file
@ -0,0 +1,461 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// a copy of context.Context for Go 1.7 and earlier
|
||||||
|
type mysqlContext interface {
|
||||||
|
Done() <-chan struct{}
|
||||||
|
Err() error
|
||||||
|
|
||||||
|
// defined in context.Context, but not used in this driver:
|
||||||
|
// Deadline() (deadline time.Time, ok bool)
|
||||||
|
// Value(key interface{}) interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type mysqlConn struct {
|
||||||
|
buf buffer
|
||||||
|
netConn net.Conn
|
||||||
|
affectedRows uint64
|
||||||
|
insertId uint64
|
||||||
|
cfg *Config
|
||||||
|
maxAllowedPacket int
|
||||||
|
maxWriteSize int
|
||||||
|
writeTimeout time.Duration
|
||||||
|
flags clientFlag
|
||||||
|
status statusFlag
|
||||||
|
sequence uint8
|
||||||
|
parseTime bool
|
||||||
|
|
||||||
|
// for context support (Go 1.8+)
|
||||||
|
watching bool
|
||||||
|
watcher chan<- mysqlContext
|
||||||
|
closech chan struct{}
|
||||||
|
finished chan<- struct{}
|
||||||
|
canceled atomicError // set non-nil if conn is canceled
|
||||||
|
closed atomicBool // set when conn is closed, before closech is closed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles parameters set in DSN after the connection is established
|
||||||
|
func (mc *mysqlConn) handleParams() (err error) {
|
||||||
|
for param, val := range mc.cfg.Params {
|
||||||
|
switch param {
|
||||||
|
// Charset
|
||||||
|
case "charset":
|
||||||
|
charsets := strings.Split(val, ",")
|
||||||
|
for i := range charsets {
|
||||||
|
// ignore errors here - a charset may not exist
|
||||||
|
err = mc.exec("SET NAMES " + charsets[i])
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// System Vars
|
||||||
|
default:
|
||||||
|
err = mc.exec("SET " + param + "=" + val + "")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) markBadConn(err error) error {
|
||||||
|
if mc == nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err != errBadConnNoWrite {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return driver.ErrBadConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) Begin() (driver.Tx, error) {
|
||||||
|
return mc.begin(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) begin(readOnly bool) (driver.Tx, error) {
|
||||||
|
if mc.closed.IsSet() {
|
||||||
|
errLog.Print(ErrInvalidConn)
|
||||||
|
return nil, driver.ErrBadConn
|
||||||
|
}
|
||||||
|
var q string
|
||||||
|
if readOnly {
|
||||||
|
q = "START TRANSACTION READ ONLY"
|
||||||
|
} else {
|
||||||
|
q = "START TRANSACTION"
|
||||||
|
}
|
||||||
|
err := mc.exec(q)
|
||||||
|
if err == nil {
|
||||||
|
return &mysqlTx{mc}, err
|
||||||
|
}
|
||||||
|
return nil, mc.markBadConn(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) Close() (err error) {
|
||||||
|
// Makes Close idempotent
|
||||||
|
if !mc.closed.IsSet() {
|
||||||
|
err = mc.writeCommandPacket(comQuit)
|
||||||
|
}
|
||||||
|
|
||||||
|
mc.cleanup()
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Closes the network connection and unsets internal variables. Do not call this
|
||||||
|
// function after successfully authentication, call Close instead. This function
|
||||||
|
// is called before auth or on auth failure because MySQL will have already
|
||||||
|
// closed the network connection.
|
||||||
|
func (mc *mysqlConn) cleanup() {
|
||||||
|
if !mc.closed.TrySet(true) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Makes cleanup idempotent
|
||||||
|
close(mc.closech)
|
||||||
|
if mc.netConn == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := mc.netConn.Close(); err != nil {
|
||||||
|
errLog.Print(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) error() error {
|
||||||
|
if mc.closed.IsSet() {
|
||||||
|
if err := mc.canceled.Value(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return ErrInvalidConn
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
|
||||||
|
if mc.closed.IsSet() {
|
||||||
|
errLog.Print(ErrInvalidConn)
|
||||||
|
return nil, driver.ErrBadConn
|
||||||
|
}
|
||||||
|
// Send command
|
||||||
|
err := mc.writeCommandPacketStr(comStmtPrepare, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, mc.markBadConn(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt := &mysqlStmt{
|
||||||
|
mc: mc,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read Result
|
||||||
|
columnCount, err := stmt.readPrepareResultPacket()
|
||||||
|
if err == nil {
|
||||||
|
if stmt.paramCount > 0 {
|
||||||
|
if err = mc.readUntilEOF(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if columnCount > 0 {
|
||||||
|
err = mc.readUntilEOF()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stmt, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (string, error) {
|
||||||
|
// Number of ? should be same to len(args)
|
||||||
|
if strings.Count(query, "?") != len(args) {
|
||||||
|
return "", driver.ErrSkip
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := mc.buf.takeCompleteBuffer()
|
||||||
|
if buf == nil {
|
||||||
|
// can not take the buffer. Something must be wrong with the connection
|
||||||
|
errLog.Print(ErrBusyBuffer)
|
||||||
|
return "", ErrInvalidConn
|
||||||
|
}
|
||||||
|
buf = buf[:0]
|
||||||
|
argPos := 0
|
||||||
|
|
||||||
|
for i := 0; i < len(query); i++ {
|
||||||
|
q := strings.IndexByte(query[i:], '?')
|
||||||
|
if q == -1 {
|
||||||
|
buf = append(buf, query[i:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
buf = append(buf, query[i:i+q]...)
|
||||||
|
i += q
|
||||||
|
|
||||||
|
arg := args[argPos]
|
||||||
|
argPos++
|
||||||
|
|
||||||
|
if arg == nil {
|
||||||
|
buf = append(buf, "NULL"...)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch v := arg.(type) {
|
||||||
|
case int64:
|
||||||
|
buf = strconv.AppendInt(buf, v, 10)
|
||||||
|
case float64:
|
||||||
|
buf = strconv.AppendFloat(buf, v, 'g', -1, 64)
|
||||||
|
case bool:
|
||||||
|
if v {
|
||||||
|
buf = append(buf, '1')
|
||||||
|
} else {
|
||||||
|
buf = append(buf, '0')
|
||||||
|
}
|
||||||
|
case time.Time:
|
||||||
|
if v.IsZero() {
|
||||||
|
buf = append(buf, "'0000-00-00'"...)
|
||||||
|
} else {
|
||||||
|
v := v.In(mc.cfg.Loc)
|
||||||
|
v = v.Add(time.Nanosecond * 500) // To round under microsecond
|
||||||
|
year := v.Year()
|
||||||
|
year100 := year / 100
|
||||||
|
year1 := year % 100
|
||||||
|
month := v.Month()
|
||||||
|
day := v.Day()
|
||||||
|
hour := v.Hour()
|
||||||
|
minute := v.Minute()
|
||||||
|
second := v.Second()
|
||||||
|
micro := v.Nanosecond() / 1000
|
||||||
|
|
||||||
|
buf = append(buf, []byte{
|
||||||
|
'\'',
|
||||||
|
digits10[year100], digits01[year100],
|
||||||
|
digits10[year1], digits01[year1],
|
||||||
|
'-',
|
||||||
|
digits10[month], digits01[month],
|
||||||
|
'-',
|
||||||
|
digits10[day], digits01[day],
|
||||||
|
' ',
|
||||||
|
digits10[hour], digits01[hour],
|
||||||
|
':',
|
||||||
|
digits10[minute], digits01[minute],
|
||||||
|
':',
|
||||||
|
digits10[second], digits01[second],
|
||||||
|
}...)
|
||||||
|
|
||||||
|
if micro != 0 {
|
||||||
|
micro10000 := micro / 10000
|
||||||
|
micro100 := micro / 100 % 100
|
||||||
|
micro1 := micro % 100
|
||||||
|
buf = append(buf, []byte{
|
||||||
|
'.',
|
||||||
|
digits10[micro10000], digits01[micro10000],
|
||||||
|
digits10[micro100], digits01[micro100],
|
||||||
|
digits10[micro1], digits01[micro1],
|
||||||
|
}...)
|
||||||
|
}
|
||||||
|
buf = append(buf, '\'')
|
||||||
|
}
|
||||||
|
case []byte:
|
||||||
|
if v == nil {
|
||||||
|
buf = append(buf, "NULL"...)
|
||||||
|
} else {
|
||||||
|
buf = append(buf, "_binary'"...)
|
||||||
|
if mc.status&statusNoBackslashEscapes == 0 {
|
||||||
|
buf = escapeBytesBackslash(buf, v)
|
||||||
|
} else {
|
||||||
|
buf = escapeBytesQuotes(buf, v)
|
||||||
|
}
|
||||||
|
buf = append(buf, '\'')
|
||||||
|
}
|
||||||
|
case string:
|
||||||
|
buf = append(buf, '\'')
|
||||||
|
if mc.status&statusNoBackslashEscapes == 0 {
|
||||||
|
buf = escapeStringBackslash(buf, v)
|
||||||
|
} else {
|
||||||
|
buf = escapeStringQuotes(buf, v)
|
||||||
|
}
|
||||||
|
buf = append(buf, '\'')
|
||||||
|
default:
|
||||||
|
return "", driver.ErrSkip
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(buf)+4 > mc.maxAllowedPacket {
|
||||||
|
return "", driver.ErrSkip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if argPos != len(args) {
|
||||||
|
return "", driver.ErrSkip
|
||||||
|
}
|
||||||
|
return string(buf), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
||||||
|
if mc.closed.IsSet() {
|
||||||
|
errLog.Print(ErrInvalidConn)
|
||||||
|
return nil, driver.ErrBadConn
|
||||||
|
}
|
||||||
|
if len(args) != 0 {
|
||||||
|
if !mc.cfg.InterpolateParams {
|
||||||
|
return nil, driver.ErrSkip
|
||||||
|
}
|
||||||
|
// try to interpolate the parameters to save extra roundtrips for preparing and closing a statement
|
||||||
|
prepared, err := mc.interpolateParams(query, args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query = prepared
|
||||||
|
}
|
||||||
|
mc.affectedRows = 0
|
||||||
|
mc.insertId = 0
|
||||||
|
|
||||||
|
err := mc.exec(query)
|
||||||
|
if err == nil {
|
||||||
|
return &mysqlResult{
|
||||||
|
affectedRows: int64(mc.affectedRows),
|
||||||
|
insertId: int64(mc.insertId),
|
||||||
|
}, err
|
||||||
|
}
|
||||||
|
return nil, mc.markBadConn(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internal function to execute commands
|
||||||
|
func (mc *mysqlConn) exec(query string) error {
|
||||||
|
// Send command
|
||||||
|
if err := mc.writeCommandPacketStr(comQuery, query); err != nil {
|
||||||
|
return mc.markBadConn(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read Result
|
||||||
|
resLen, err := mc.readResultSetHeaderPacket()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resLen > 0 {
|
||||||
|
// columns
|
||||||
|
if err := mc.readUntilEOF(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// rows
|
||||||
|
if err := mc.readUntilEOF(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return mc.discardResults()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
||||||
|
return mc.query(query, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) query(query string, args []driver.Value) (*textRows, error) {
|
||||||
|
if mc.closed.IsSet() {
|
||||||
|
errLog.Print(ErrInvalidConn)
|
||||||
|
return nil, driver.ErrBadConn
|
||||||
|
}
|
||||||
|
if len(args) != 0 {
|
||||||
|
if !mc.cfg.InterpolateParams {
|
||||||
|
return nil, driver.ErrSkip
|
||||||
|
}
|
||||||
|
// try client-side prepare to reduce roundtrip
|
||||||
|
prepared, err := mc.interpolateParams(query, args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query = prepared
|
||||||
|
}
|
||||||
|
// Send command
|
||||||
|
err := mc.writeCommandPacketStr(comQuery, query)
|
||||||
|
if err == nil {
|
||||||
|
// Read Result
|
||||||
|
var resLen int
|
||||||
|
resLen, err = mc.readResultSetHeaderPacket()
|
||||||
|
if err == nil {
|
||||||
|
rows := new(textRows)
|
||||||
|
rows.mc = mc
|
||||||
|
|
||||||
|
if resLen == 0 {
|
||||||
|
rows.rs.done = true
|
||||||
|
|
||||||
|
switch err := rows.NextResultSet(); err {
|
||||||
|
case nil, io.EOF:
|
||||||
|
return rows, nil
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Columns
|
||||||
|
rows.rs.columns, err = mc.readColumns(resLen)
|
||||||
|
return rows, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, mc.markBadConn(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gets the value of the given MySQL System Variable
|
||||||
|
// The returned byte slice is only valid until the next read
|
||||||
|
func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
|
||||||
|
// Send command
|
||||||
|
if err := mc.writeCommandPacketStr(comQuery, "SELECT @@"+name); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read Result
|
||||||
|
resLen, err := mc.readResultSetHeaderPacket()
|
||||||
|
if err == nil {
|
||||||
|
rows := new(textRows)
|
||||||
|
rows.mc = mc
|
||||||
|
rows.rs.columns = []mysqlField{{fieldType: fieldTypeVarChar}}
|
||||||
|
|
||||||
|
if resLen > 0 {
|
||||||
|
// Columns
|
||||||
|
if err := mc.readUntilEOF(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dest := make([]driver.Value, resLen)
|
||||||
|
if err = rows.readRow(dest); err == nil {
|
||||||
|
return dest[0].([]byte), mc.readUntilEOF()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// finish is called when the query has canceled.
|
||||||
|
func (mc *mysqlConn) cancel(err error) {
|
||||||
|
mc.canceled.Set(err)
|
||||||
|
mc.cleanup()
|
||||||
|
}
|
||||||
|
|
||||||
|
// finish is called when the query has succeeded.
|
||||||
|
func (mc *mysqlConn) finish() {
|
||||||
|
if !mc.watching || mc.finished == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case mc.finished <- struct{}{}:
|
||||||
|
mc.watching = false
|
||||||
|
case <-mc.closech:
|
||||||
|
}
|
||||||
|
}
|
207
vendor/github.com/go-sql-driver/mysql/connection_go18.go
generated
vendored
Normal file
207
vendor/github.com/go-sql-driver/mysql/connection_go18.go
generated
vendored
Normal file
@ -0,0 +1,207 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
// +build go1.8
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"database/sql/driver"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Ping implements driver.Pinger interface
|
||||||
|
func (mc *mysqlConn) Ping(ctx context.Context) (err error) {
|
||||||
|
if mc.closed.IsSet() {
|
||||||
|
errLog.Print(ErrInvalidConn)
|
||||||
|
return driver.ErrBadConn
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = mc.watchCancel(ctx); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer mc.finish()
|
||||||
|
|
||||||
|
if err = mc.writeCommandPacket(comPing); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return mc.readResultOK()
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeginTx implements driver.ConnBeginTx interface
|
||||||
|
func (mc *mysqlConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
||||||
|
if err := mc.watchCancel(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer mc.finish()
|
||||||
|
|
||||||
|
if sql.IsolationLevel(opts.Isolation) != sql.LevelDefault {
|
||||||
|
level, err := mapIsolationLevel(opts.Isolation)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = mc.exec("SET TRANSACTION ISOLATION LEVEL " + level)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return mc.begin(opts.ReadOnly)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
||||||
|
dargs, err := namedValueToValue(args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := mc.watchCancel(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := mc.query(query, dargs)
|
||||||
|
if err != nil {
|
||||||
|
mc.finish()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rows.finish = mc.finish
|
||||||
|
return rows, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
||||||
|
dargs, err := namedValueToValue(args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := mc.watchCancel(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer mc.finish()
|
||||||
|
|
||||||
|
return mc.Exec(query, dargs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
|
||||||
|
if err := mc.watchCancel(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt, err := mc.Prepare(query)
|
||||||
|
mc.finish()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
default:
|
||||||
|
case <-ctx.Done():
|
||||||
|
stmt.Close()
|
||||||
|
return nil, ctx.Err()
|
||||||
|
}
|
||||||
|
return stmt, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stmt *mysqlStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
|
||||||
|
dargs, err := namedValueToValue(args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := stmt.mc.watchCancel(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := stmt.query(dargs)
|
||||||
|
if err != nil {
|
||||||
|
stmt.mc.finish()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rows.finish = stmt.mc.finish
|
||||||
|
return rows, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stmt *mysqlStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
|
||||||
|
dargs, err := namedValueToValue(args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := stmt.mc.watchCancel(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer stmt.mc.finish()
|
||||||
|
|
||||||
|
return stmt.Exec(dargs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) watchCancel(ctx context.Context) error {
|
||||||
|
if mc.watching {
|
||||||
|
// Reach here if canceled,
|
||||||
|
// so the connection is already invalid
|
||||||
|
mc.cleanup()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// When ctx is already cancelled, don't watch it.
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// When ctx is not cancellable, don't watch it.
|
||||||
|
if ctx.Done() == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// When watcher is not alive, can't watch it.
|
||||||
|
if mc.watcher == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mc.watching = true
|
||||||
|
mc.watcher <- ctx
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) startWatcher() {
|
||||||
|
watcher := make(chan mysqlContext, 1)
|
||||||
|
mc.watcher = watcher
|
||||||
|
finished := make(chan struct{})
|
||||||
|
mc.finished = finished
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
var ctx mysqlContext
|
||||||
|
select {
|
||||||
|
case ctx = <-watcher:
|
||||||
|
case <-mc.closech:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
mc.cancel(ctx.Err())
|
||||||
|
case <-finished:
|
||||||
|
case <-mc.closech:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) CheckNamedValue(nv *driver.NamedValue) (err error) {
|
||||||
|
nv.Value, err = converter{}.ConvertValue(nv.Value)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResetSession implements driver.SessionResetter.
|
||||||
|
// (From Go 1.10)
|
||||||
|
func (mc *mysqlConn) ResetSession(ctx context.Context) error {
|
||||||
|
if mc.closed.IsSet() {
|
||||||
|
return driver.ErrBadConn
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
174
vendor/github.com/go-sql-driver/mysql/const.go
generated
vendored
Normal file
174
vendor/github.com/go-sql-driver/mysql/const.go
generated
vendored
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultAuthPlugin = "mysql_native_password"
|
||||||
|
defaultMaxAllowedPacket = 4 << 20 // 4 MiB
|
||||||
|
minProtocolVersion = 10
|
||||||
|
maxPacketSize = 1<<24 - 1
|
||||||
|
timeFormat = "2006-01-02 15:04:05.999999"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MySQL constants documentation:
|
||||||
|
// http://dev.mysql.com/doc/internals/en/client-server-protocol.html
|
||||||
|
|
||||||
|
const (
|
||||||
|
iOK byte = 0x00
|
||||||
|
iAuthMoreData byte = 0x01
|
||||||
|
iLocalInFile byte = 0xfb
|
||||||
|
iEOF byte = 0xfe
|
||||||
|
iERR byte = 0xff
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://dev.mysql.com/doc/internals/en/capability-flags.html#packet-Protocol::CapabilityFlags
|
||||||
|
type clientFlag uint32
|
||||||
|
|
||||||
|
const (
|
||||||
|
clientLongPassword clientFlag = 1 << iota
|
||||||
|
clientFoundRows
|
||||||
|
clientLongFlag
|
||||||
|
clientConnectWithDB
|
||||||
|
clientNoSchema
|
||||||
|
clientCompress
|
||||||
|
clientODBC
|
||||||
|
clientLocalFiles
|
||||||
|
clientIgnoreSpace
|
||||||
|
clientProtocol41
|
||||||
|
clientInteractive
|
||||||
|
clientSSL
|
||||||
|
clientIgnoreSIGPIPE
|
||||||
|
clientTransactions
|
||||||
|
clientReserved
|
||||||
|
clientSecureConn
|
||||||
|
clientMultiStatements
|
||||||
|
clientMultiResults
|
||||||
|
clientPSMultiResults
|
||||||
|
clientPluginAuth
|
||||||
|
clientConnectAttrs
|
||||||
|
clientPluginAuthLenEncClientData
|
||||||
|
clientCanHandleExpiredPasswords
|
||||||
|
clientSessionTrack
|
||||||
|
clientDeprecateEOF
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
comQuit byte = iota + 1
|
||||||
|
comInitDB
|
||||||
|
comQuery
|
||||||
|
comFieldList
|
||||||
|
comCreateDB
|
||||||
|
comDropDB
|
||||||
|
comRefresh
|
||||||
|
comShutdown
|
||||||
|
comStatistics
|
||||||
|
comProcessInfo
|
||||||
|
comConnect
|
||||||
|
comProcessKill
|
||||||
|
comDebug
|
||||||
|
comPing
|
||||||
|
comTime
|
||||||
|
comDelayedInsert
|
||||||
|
comChangeUser
|
||||||
|
comBinlogDump
|
||||||
|
comTableDump
|
||||||
|
comConnectOut
|
||||||
|
comRegisterSlave
|
||||||
|
comStmtPrepare
|
||||||
|
comStmtExecute
|
||||||
|
comStmtSendLongData
|
||||||
|
comStmtClose
|
||||||
|
comStmtReset
|
||||||
|
comSetOption
|
||||||
|
comStmtFetch
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnType
|
||||||
|
type fieldType byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
fieldTypeDecimal fieldType = iota
|
||||||
|
fieldTypeTiny
|
||||||
|
fieldTypeShort
|
||||||
|
fieldTypeLong
|
||||||
|
fieldTypeFloat
|
||||||
|
fieldTypeDouble
|
||||||
|
fieldTypeNULL
|
||||||
|
fieldTypeTimestamp
|
||||||
|
fieldTypeLongLong
|
||||||
|
fieldTypeInt24
|
||||||
|
fieldTypeDate
|
||||||
|
fieldTypeTime
|
||||||
|
fieldTypeDateTime
|
||||||
|
fieldTypeYear
|
||||||
|
fieldTypeNewDate
|
||||||
|
fieldTypeVarChar
|
||||||
|
fieldTypeBit
|
||||||
|
)
|
||||||
|
const (
|
||||||
|
fieldTypeJSON fieldType = iota + 0xf5
|
||||||
|
fieldTypeNewDecimal
|
||||||
|
fieldTypeEnum
|
||||||
|
fieldTypeSet
|
||||||
|
fieldTypeTinyBLOB
|
||||||
|
fieldTypeMediumBLOB
|
||||||
|
fieldTypeLongBLOB
|
||||||
|
fieldTypeBLOB
|
||||||
|
fieldTypeVarString
|
||||||
|
fieldTypeString
|
||||||
|
fieldTypeGeometry
|
||||||
|
)
|
||||||
|
|
||||||
|
type fieldFlag uint16
|
||||||
|
|
||||||
|
const (
|
||||||
|
flagNotNULL fieldFlag = 1 << iota
|
||||||
|
flagPriKey
|
||||||
|
flagUniqueKey
|
||||||
|
flagMultipleKey
|
||||||
|
flagBLOB
|
||||||
|
flagUnsigned
|
||||||
|
flagZeroFill
|
||||||
|
flagBinary
|
||||||
|
flagEnum
|
||||||
|
flagAutoIncrement
|
||||||
|
flagTimestamp
|
||||||
|
flagSet
|
||||||
|
flagUnknown1
|
||||||
|
flagUnknown2
|
||||||
|
flagUnknown3
|
||||||
|
flagUnknown4
|
||||||
|
)
|
||||||
|
|
||||||
|
// http://dev.mysql.com/doc/internals/en/status-flags.html
|
||||||
|
type statusFlag uint16
|
||||||
|
|
||||||
|
const (
|
||||||
|
statusInTrans statusFlag = 1 << iota
|
||||||
|
statusInAutocommit
|
||||||
|
statusReserved // Not in documentation
|
||||||
|
statusMoreResultsExists
|
||||||
|
statusNoGoodIndexUsed
|
||||||
|
statusNoIndexUsed
|
||||||
|
statusCursorExists
|
||||||
|
statusLastRowSent
|
||||||
|
statusDbDropped
|
||||||
|
statusNoBackslashEscapes
|
||||||
|
statusMetadataChanged
|
||||||
|
statusQueryWasSlow
|
||||||
|
statusPsOutParams
|
||||||
|
statusInTransReadonly
|
||||||
|
statusSessionStateChanged
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
cachingSha2PasswordRequestPublicKey = 2
|
||||||
|
cachingSha2PasswordFastAuthSuccess = 3
|
||||||
|
cachingSha2PasswordPerformFullAuthentication = 4
|
||||||
|
)
|
172
vendor/github.com/go-sql-driver/mysql/driver.go
generated
vendored
Normal file
172
vendor/github.com/go-sql-driver/mysql/driver.go
generated
vendored
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
// Package mysql provides a MySQL driver for Go's database/sql package.
|
||||||
|
//
|
||||||
|
// The driver should be used via the database/sql package:
|
||||||
|
//
|
||||||
|
// import "database/sql"
|
||||||
|
// import _ "github.com/go-sql-driver/mysql"
|
||||||
|
//
|
||||||
|
// db, err := sql.Open("mysql", "user:password@/dbname")
|
||||||
|
//
|
||||||
|
// See https://github.com/go-sql-driver/mysql#usage for details
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"database/sql/driver"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// watcher interface is used for context support (From Go 1.8)
|
||||||
|
type watcher interface {
|
||||||
|
startWatcher()
|
||||||
|
}
|
||||||
|
|
||||||
|
// MySQLDriver is exported to make the driver directly accessible.
|
||||||
|
// In general the driver is used via the database/sql package.
|
||||||
|
type MySQLDriver struct{}
|
||||||
|
|
||||||
|
// DialFunc is a function which can be used to establish the network connection.
|
||||||
|
// Custom dial functions must be registered with RegisterDial
|
||||||
|
type DialFunc func(addr string) (net.Conn, error)
|
||||||
|
|
||||||
|
var (
|
||||||
|
dialsLock sync.RWMutex
|
||||||
|
dials map[string]DialFunc
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegisterDial registers a custom dial function. It can then be used by the
|
||||||
|
// network address mynet(addr), where mynet is the registered new network.
|
||||||
|
// addr is passed as a parameter to the dial function.
|
||||||
|
func RegisterDial(net string, dial DialFunc) {
|
||||||
|
dialsLock.Lock()
|
||||||
|
defer dialsLock.Unlock()
|
||||||
|
if dials == nil {
|
||||||
|
dials = make(map[string]DialFunc)
|
||||||
|
}
|
||||||
|
dials[net] = dial
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open new Connection.
|
||||||
|
// See https://github.com/go-sql-driver/mysql#dsn-data-source-name for how
|
||||||
|
// the DSN string is formated
|
||||||
|
func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// New mysqlConn
|
||||||
|
mc := &mysqlConn{
|
||||||
|
maxAllowedPacket: maxPacketSize,
|
||||||
|
maxWriteSize: maxPacketSize - 1,
|
||||||
|
closech: make(chan struct{}),
|
||||||
|
}
|
||||||
|
mc.cfg, err = ParseDSN(dsn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
mc.parseTime = mc.cfg.ParseTime
|
||||||
|
|
||||||
|
// Connect to Server
|
||||||
|
dialsLock.RLock()
|
||||||
|
dial, ok := dials[mc.cfg.Net]
|
||||||
|
dialsLock.RUnlock()
|
||||||
|
if ok {
|
||||||
|
mc.netConn, err = dial(mc.cfg.Addr)
|
||||||
|
} else {
|
||||||
|
nd := net.Dialer{Timeout: mc.cfg.Timeout}
|
||||||
|
mc.netConn, err = nd.Dial(mc.cfg.Net, mc.cfg.Addr)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable TCP Keepalives on TCP connections
|
||||||
|
if tc, ok := mc.netConn.(*net.TCPConn); ok {
|
||||||
|
if err := tc.SetKeepAlive(true); err != nil {
|
||||||
|
// Don't send COM_QUIT before handshake.
|
||||||
|
mc.netConn.Close()
|
||||||
|
mc.netConn = nil
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call startWatcher for context support (From Go 1.8)
|
||||||
|
if s, ok := interface{}(mc).(watcher); ok {
|
||||||
|
s.startWatcher()
|
||||||
|
}
|
||||||
|
|
||||||
|
mc.buf = newBuffer(mc.netConn)
|
||||||
|
|
||||||
|
// Set I/O timeouts
|
||||||
|
mc.buf.timeout = mc.cfg.ReadTimeout
|
||||||
|
mc.writeTimeout = mc.cfg.WriteTimeout
|
||||||
|
|
||||||
|
// Reading Handshake Initialization Packet
|
||||||
|
authData, plugin, err := mc.readHandshakePacket()
|
||||||
|
if err != nil {
|
||||||
|
mc.cleanup()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if plugin == "" {
|
||||||
|
plugin = defaultAuthPlugin
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send Client Authentication Packet
|
||||||
|
authResp, err := mc.auth(authData, plugin)
|
||||||
|
if err != nil {
|
||||||
|
// try the default auth plugin, if using the requested plugin failed
|
||||||
|
errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
|
||||||
|
plugin = defaultAuthPlugin
|
||||||
|
authResp, err = mc.auth(authData, plugin)
|
||||||
|
if err != nil {
|
||||||
|
mc.cleanup()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
|
||||||
|
mc.cleanup()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle response to auth packet, switch methods if possible
|
||||||
|
if err = mc.handleAuthResult(authData, plugin); err != nil {
|
||||||
|
// Authentication failed and MySQL has already closed the connection
|
||||||
|
// (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
|
||||||
|
// Do not send COM_QUIT, just cleanup and return the error.
|
||||||
|
mc.cleanup()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if mc.cfg.MaxAllowedPacket > 0 {
|
||||||
|
mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket
|
||||||
|
} else {
|
||||||
|
// Get max allowed packet size
|
||||||
|
maxap, err := mc.getSystemVar("max_allowed_packet")
|
||||||
|
if err != nil {
|
||||||
|
mc.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
mc.maxAllowedPacket = stringToInt(maxap) - 1
|
||||||
|
}
|
||||||
|
if mc.maxAllowedPacket < maxPacketSize {
|
||||||
|
mc.maxWriteSize = mc.maxAllowedPacket
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle DSN Params
|
||||||
|
err = mc.handleParams()
|
||||||
|
if err != nil {
|
||||||
|
mc.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mc, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
sql.Register("mysql", &MySQLDriver{})
|
||||||
|
}
|
611
vendor/github.com/go-sql-driver/mysql/dsn.go
generated
vendored
Normal file
611
vendor/github.com/go-sql-driver/mysql/dsn.go
generated
vendored
Normal file
@ -0,0 +1,611 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2016 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errInvalidDSNUnescaped = errors.New("invalid DSN: did you forget to escape a param value?")
|
||||||
|
errInvalidDSNAddr = errors.New("invalid DSN: network address not terminated (missing closing brace)")
|
||||||
|
errInvalidDSNNoSlash = errors.New("invalid DSN: missing the slash separating the database name")
|
||||||
|
errInvalidDSNUnsafeCollation = errors.New("invalid DSN: interpolateParams can not be used with unsafe collations")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config is a configuration parsed from a DSN string.
|
||||||
|
// If a new Config is created instead of being parsed from a DSN string,
|
||||||
|
// the NewConfig function should be used, which sets default values.
|
||||||
|
type Config struct {
|
||||||
|
User string // Username
|
||||||
|
Passwd string // Password (requires User)
|
||||||
|
Net string // Network type
|
||||||
|
Addr string // Network address (requires Net)
|
||||||
|
DBName string // Database name
|
||||||
|
Params map[string]string // Connection parameters
|
||||||
|
Collation string // Connection collation
|
||||||
|
Loc *time.Location // Location for time.Time values
|
||||||
|
MaxAllowedPacket int // Max packet size allowed
|
||||||
|
ServerPubKey string // Server public key name
|
||||||
|
pubKey *rsa.PublicKey // Server public key
|
||||||
|
TLSConfig string // TLS configuration name
|
||||||
|
tls *tls.Config // TLS configuration
|
||||||
|
Timeout time.Duration // Dial timeout
|
||||||
|
ReadTimeout time.Duration // I/O read timeout
|
||||||
|
WriteTimeout time.Duration // I/O write timeout
|
||||||
|
|
||||||
|
AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE
|
||||||
|
AllowCleartextPasswords bool // Allows the cleartext client side plugin
|
||||||
|
AllowNativePasswords bool // Allows the native password authentication method
|
||||||
|
AllowOldPasswords bool // Allows the old insecure password method
|
||||||
|
ClientFoundRows bool // Return number of matching rows instead of rows changed
|
||||||
|
ColumnsWithAlias bool // Prepend table alias to column names
|
||||||
|
InterpolateParams bool // Interpolate placeholders into query string
|
||||||
|
MultiStatements bool // Allow multiple statements in one query
|
||||||
|
ParseTime bool // Parse time values to time.Time
|
||||||
|
RejectReadOnly bool // Reject read-only connections
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewConfig creates a new Config and sets default values.
|
||||||
|
func NewConfig() *Config {
|
||||||
|
return &Config{
|
||||||
|
Collation: defaultCollation,
|
||||||
|
Loc: time.UTC,
|
||||||
|
MaxAllowedPacket: defaultMaxAllowedPacket,
|
||||||
|
AllowNativePasswords: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *Config) normalize() error {
|
||||||
|
if cfg.InterpolateParams && unsafeCollations[cfg.Collation] {
|
||||||
|
return errInvalidDSNUnsafeCollation
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set default network if empty
|
||||||
|
if cfg.Net == "" {
|
||||||
|
cfg.Net = "tcp"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set default address if empty
|
||||||
|
if cfg.Addr == "" {
|
||||||
|
switch cfg.Net {
|
||||||
|
case "tcp":
|
||||||
|
cfg.Addr = "127.0.0.1:3306"
|
||||||
|
case "unix":
|
||||||
|
cfg.Addr = "/tmp/mysql.sock"
|
||||||
|
default:
|
||||||
|
return errors.New("default addr for network '" + cfg.Net + "' unknown")
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if cfg.Net == "tcp" {
|
||||||
|
cfg.Addr = ensureHavePort(cfg.Addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.tls != nil {
|
||||||
|
if cfg.tls.ServerName == "" && !cfg.tls.InsecureSkipVerify {
|
||||||
|
host, _, err := net.SplitHostPort(cfg.Addr)
|
||||||
|
if err == nil {
|
||||||
|
cfg.tls.ServerName = host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatDSN formats the given Config into a DSN string which can be passed to
|
||||||
|
// the driver.
|
||||||
|
func (cfg *Config) FormatDSN() string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
// [username[:password]@]
|
||||||
|
if len(cfg.User) > 0 {
|
||||||
|
buf.WriteString(cfg.User)
|
||||||
|
if len(cfg.Passwd) > 0 {
|
||||||
|
buf.WriteByte(':')
|
||||||
|
buf.WriteString(cfg.Passwd)
|
||||||
|
}
|
||||||
|
buf.WriteByte('@')
|
||||||
|
}
|
||||||
|
|
||||||
|
// [protocol[(address)]]
|
||||||
|
if len(cfg.Net) > 0 {
|
||||||
|
buf.WriteString(cfg.Net)
|
||||||
|
if len(cfg.Addr) > 0 {
|
||||||
|
buf.WriteByte('(')
|
||||||
|
buf.WriteString(cfg.Addr)
|
||||||
|
buf.WriteByte(')')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// /dbname
|
||||||
|
buf.WriteByte('/')
|
||||||
|
buf.WriteString(cfg.DBName)
|
||||||
|
|
||||||
|
// [?param1=value1&...¶mN=valueN]
|
||||||
|
hasParam := false
|
||||||
|
|
||||||
|
if cfg.AllowAllFiles {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?allowAllFiles=true")
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.AllowCleartextPasswords {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&allowCleartextPasswords=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?allowCleartextPasswords=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !cfg.AllowNativePasswords {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&allowNativePasswords=false")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?allowNativePasswords=false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.AllowOldPasswords {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&allowOldPasswords=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?allowOldPasswords=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ClientFoundRows {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&clientFoundRows=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?clientFoundRows=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if col := cfg.Collation; col != defaultCollation && len(col) > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&collation=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?collation=")
|
||||||
|
}
|
||||||
|
buf.WriteString(col)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ColumnsWithAlias {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&columnsWithAlias=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?columnsWithAlias=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.InterpolateParams {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&interpolateParams=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?interpolateParams=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Loc != time.UTC && cfg.Loc != nil {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&loc=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?loc=")
|
||||||
|
}
|
||||||
|
buf.WriteString(url.QueryEscape(cfg.Loc.String()))
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.MultiStatements {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&multiStatements=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?multiStatements=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ParseTime {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&parseTime=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?parseTime=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ReadTimeout > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&readTimeout=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?readTimeout=")
|
||||||
|
}
|
||||||
|
buf.WriteString(cfg.ReadTimeout.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.RejectReadOnly {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&rejectReadOnly=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?rejectReadOnly=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cfg.ServerPubKey) > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&serverPubKey=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?serverPubKey=")
|
||||||
|
}
|
||||||
|
buf.WriteString(url.QueryEscape(cfg.ServerPubKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Timeout > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&timeout=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?timeout=")
|
||||||
|
}
|
||||||
|
buf.WriteString(cfg.Timeout.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cfg.TLSConfig) > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&tls=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?tls=")
|
||||||
|
}
|
||||||
|
buf.WriteString(url.QueryEscape(cfg.TLSConfig))
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.WriteTimeout > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&writeTimeout=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?writeTimeout=")
|
||||||
|
}
|
||||||
|
buf.WriteString(cfg.WriteTimeout.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.MaxAllowedPacket != defaultMaxAllowedPacket {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&maxAllowedPacket=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?maxAllowedPacket=")
|
||||||
|
}
|
||||||
|
buf.WriteString(strconv.Itoa(cfg.MaxAllowedPacket))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// other params
|
||||||
|
if cfg.Params != nil {
|
||||||
|
var params []string
|
||||||
|
for param := range cfg.Params {
|
||||||
|
params = append(params, param)
|
||||||
|
}
|
||||||
|
sort.Strings(params)
|
||||||
|
for _, param := range params {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteByte('&')
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteByte('?')
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.WriteString(param)
|
||||||
|
buf.WriteByte('=')
|
||||||
|
buf.WriteString(url.QueryEscape(cfg.Params[param]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseDSN parses the DSN string to a Config
|
||||||
|
func ParseDSN(dsn string) (cfg *Config, err error) {
|
||||||
|
// New config with some default values
|
||||||
|
cfg = NewConfig()
|
||||||
|
|
||||||
|
// [user[:password]@][net[(addr)]]/dbname[?param1=value1¶mN=valueN]
|
||||||
|
// Find the last '/' (since the password or the net addr might contain a '/')
|
||||||
|
foundSlash := false
|
||||||
|
for i := len(dsn) - 1; i >= 0; i-- {
|
||||||
|
if dsn[i] == '/' {
|
||||||
|
foundSlash = true
|
||||||
|
var j, k int
|
||||||
|
|
||||||
|
// left part is empty if i <= 0
|
||||||
|
if i > 0 {
|
||||||
|
// [username[:password]@][protocol[(address)]]
|
||||||
|
// Find the last '@' in dsn[:i]
|
||||||
|
for j = i; j >= 0; j-- {
|
||||||
|
if dsn[j] == '@' {
|
||||||
|
// username[:password]
|
||||||
|
// Find the first ':' in dsn[:j]
|
||||||
|
for k = 0; k < j; k++ {
|
||||||
|
if dsn[k] == ':' {
|
||||||
|
cfg.Passwd = dsn[k+1 : j]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cfg.User = dsn[:k]
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// [protocol[(address)]]
|
||||||
|
// Find the first '(' in dsn[j+1:i]
|
||||||
|
for k = j + 1; k < i; k++ {
|
||||||
|
if dsn[k] == '(' {
|
||||||
|
// dsn[i-1] must be == ')' if an address is specified
|
||||||
|
if dsn[i-1] != ')' {
|
||||||
|
if strings.ContainsRune(dsn[k+1:i], ')') {
|
||||||
|
return nil, errInvalidDSNUnescaped
|
||||||
|
}
|
||||||
|
return nil, errInvalidDSNAddr
|
||||||
|
}
|
||||||
|
cfg.Addr = dsn[k+1 : i-1]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cfg.Net = dsn[j+1 : k]
|
||||||
|
}
|
||||||
|
|
||||||
|
// dbname[?param1=value1&...¶mN=valueN]
|
||||||
|
// Find the first '?' in dsn[i+1:]
|
||||||
|
for j = i + 1; j < len(dsn); j++ {
|
||||||
|
if dsn[j] == '?' {
|
||||||
|
if err = parseDSNParams(cfg, dsn[j+1:]); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cfg.DBName = dsn[i+1 : j]
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !foundSlash && len(dsn) > 0 {
|
||||||
|
return nil, errInvalidDSNNoSlash
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = cfg.normalize(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseDSNParams parses the DSN "query string"
|
||||||
|
// Values must be url.QueryEscape'ed
|
||||||
|
func parseDSNParams(cfg *Config, params string) (err error) {
|
||||||
|
for _, v := range strings.Split(params, "&") {
|
||||||
|
param := strings.SplitN(v, "=", 2)
|
||||||
|
if len(param) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// cfg params
|
||||||
|
switch value := param[1]; param[0] {
|
||||||
|
// Disable INFILE whitelist / enable all files
|
||||||
|
case "allowAllFiles":
|
||||||
|
var isBool bool
|
||||||
|
cfg.AllowAllFiles, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use cleartext authentication mode (MySQL 5.5.10+)
|
||||||
|
case "allowCleartextPasswords":
|
||||||
|
var isBool bool
|
||||||
|
cfg.AllowCleartextPasswords, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use native password authentication
|
||||||
|
case "allowNativePasswords":
|
||||||
|
var isBool bool
|
||||||
|
cfg.AllowNativePasswords, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use old authentication mode (pre MySQL 4.1)
|
||||||
|
case "allowOldPasswords":
|
||||||
|
var isBool bool
|
||||||
|
cfg.AllowOldPasswords, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch "rowsAffected" mode
|
||||||
|
case "clientFoundRows":
|
||||||
|
var isBool bool
|
||||||
|
cfg.ClientFoundRows, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collation
|
||||||
|
case "collation":
|
||||||
|
cfg.Collation = value
|
||||||
|
break
|
||||||
|
|
||||||
|
case "columnsWithAlias":
|
||||||
|
var isBool bool
|
||||||
|
cfg.ColumnsWithAlias, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compression
|
||||||
|
case "compress":
|
||||||
|
return errors.New("compression not implemented yet")
|
||||||
|
|
||||||
|
// Enable client side placeholder substitution
|
||||||
|
case "interpolateParams":
|
||||||
|
var isBool bool
|
||||||
|
cfg.InterpolateParams, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Time Location
|
||||||
|
case "loc":
|
||||||
|
if value, err = url.QueryUnescape(value); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cfg.Loc, err = time.LoadLocation(value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// multiple statements in one query
|
||||||
|
case "multiStatements":
|
||||||
|
var isBool bool
|
||||||
|
cfg.MultiStatements, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// time.Time parsing
|
||||||
|
case "parseTime":
|
||||||
|
var isBool bool
|
||||||
|
cfg.ParseTime, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// I/O read Timeout
|
||||||
|
case "readTimeout":
|
||||||
|
cfg.ReadTimeout, err = time.ParseDuration(value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject read-only connections
|
||||||
|
case "rejectReadOnly":
|
||||||
|
var isBool bool
|
||||||
|
cfg.RejectReadOnly, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server public key
|
||||||
|
case "serverPubKey":
|
||||||
|
name, err := url.QueryUnescape(value)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid value for server pub key name: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if pubKey := getServerPubKey(name); pubKey != nil {
|
||||||
|
cfg.ServerPubKey = name
|
||||||
|
cfg.pubKey = pubKey
|
||||||
|
} else {
|
||||||
|
return errors.New("invalid value / unknown server pub key name: " + name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strict mode
|
||||||
|
case "strict":
|
||||||
|
panic("strict mode has been removed. See https://github.com/go-sql-driver/mysql/wiki/strict-mode")
|
||||||
|
|
||||||
|
// Dial Timeout
|
||||||
|
case "timeout":
|
||||||
|
cfg.Timeout, err = time.ParseDuration(value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TLS-Encryption
|
||||||
|
case "tls":
|
||||||
|
boolValue, isBool := readBool(value)
|
||||||
|
if isBool {
|
||||||
|
if boolValue {
|
||||||
|
cfg.TLSConfig = "true"
|
||||||
|
cfg.tls = &tls.Config{}
|
||||||
|
} else {
|
||||||
|
cfg.TLSConfig = "false"
|
||||||
|
}
|
||||||
|
} else if vl := strings.ToLower(value); vl == "skip-verify" {
|
||||||
|
cfg.TLSConfig = vl
|
||||||
|
cfg.tls = &tls.Config{InsecureSkipVerify: true}
|
||||||
|
} else {
|
||||||
|
name, err := url.QueryUnescape(value)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid value for TLS config name: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tlsConfig := getTLSConfigClone(name); tlsConfig != nil {
|
||||||
|
cfg.TLSConfig = name
|
||||||
|
cfg.tls = tlsConfig
|
||||||
|
} else {
|
||||||
|
return errors.New("invalid value / unknown config name: " + name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// I/O write Timeout
|
||||||
|
case "writeTimeout":
|
||||||
|
cfg.WriteTimeout, err = time.ParseDuration(value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "maxAllowedPacket":
|
||||||
|
cfg.MaxAllowedPacket, err = strconv.Atoi(value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
// lazy init
|
||||||
|
if cfg.Params == nil {
|
||||||
|
cfg.Params = make(map[string]string)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Params[param[0]], err = url.QueryUnescape(value); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureHavePort(addr string) string {
|
||||||
|
if _, _, err := net.SplitHostPort(addr); err != nil {
|
||||||
|
return net.JoinHostPort(addr, "3306")
|
||||||
|
}
|
||||||
|
return addr
|
||||||
|
}
|
65
vendor/github.com/go-sql-driver/mysql/errors.go
generated
vendored
Normal file
65
vendor/github.com/go-sql-driver/mysql/errors.go
generated
vendored
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Various errors the driver might return. Can change between driver versions.
|
||||||
|
var (
|
||||||
|
ErrInvalidConn = errors.New("invalid connection")
|
||||||
|
ErrMalformPkt = errors.New("malformed packet")
|
||||||
|
ErrNoTLS = errors.New("TLS requested but server does not support TLS")
|
||||||
|
ErrCleartextPassword = errors.New("this user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN")
|
||||||
|
ErrNativePassword = errors.New("this user requires mysql native password authentication.")
|
||||||
|
ErrOldPassword = errors.New("this user requires old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords")
|
||||||
|
ErrUnknownPlugin = errors.New("this authentication plugin is not supported")
|
||||||
|
ErrOldProtocol = errors.New("MySQL server does not support required protocol 41+")
|
||||||
|
ErrPktSync = errors.New("commands out of sync. You can't run this command now")
|
||||||
|
ErrPktSyncMul = errors.New("commands out of sync. Did you run multiple statements at once?")
|
||||||
|
ErrPktTooLarge = errors.New("packet for query is too large. Try adjusting the 'max_allowed_packet' variable on the server")
|
||||||
|
ErrBusyBuffer = errors.New("busy buffer")
|
||||||
|
|
||||||
|
// errBadConnNoWrite is used for connection errors where nothing was sent to the database yet.
|
||||||
|
// If this happens first in a function starting a database interaction, it should be replaced by driver.ErrBadConn
|
||||||
|
// to trigger a resend.
|
||||||
|
// See https://github.com/go-sql-driver/mysql/pull/302
|
||||||
|
errBadConnNoWrite = errors.New("bad connection")
|
||||||
|
)
|
||||||
|
|
||||||
|
var errLog = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
|
||||||
|
|
||||||
|
// Logger is used to log critical error messages.
|
||||||
|
type Logger interface {
|
||||||
|
Print(v ...interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLogger is used to set the logger for critical errors.
|
||||||
|
// The initial logger is os.Stderr.
|
||||||
|
func SetLogger(logger Logger) error {
|
||||||
|
if logger == nil {
|
||||||
|
return errors.New("logger is nil")
|
||||||
|
}
|
||||||
|
errLog = logger
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MySQLError is an error type which represents a single MySQL error
|
||||||
|
type MySQLError struct {
|
||||||
|
Number uint16
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (me *MySQLError) Error() string {
|
||||||
|
return fmt.Sprintf("Error %d: %s", me.Number, me.Message)
|
||||||
|
}
|
194
vendor/github.com/go-sql-driver/mysql/fields.go
generated
vendored
Normal file
194
vendor/github.com/go-sql-driver/mysql/fields.go
generated
vendored
Normal file
@ -0,0 +1,194 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (mf *mysqlField) typeDatabaseName() string {
|
||||||
|
switch mf.fieldType {
|
||||||
|
case fieldTypeBit:
|
||||||
|
return "BIT"
|
||||||
|
case fieldTypeBLOB:
|
||||||
|
if mf.charSet != collations[binaryCollation] {
|
||||||
|
return "TEXT"
|
||||||
|
}
|
||||||
|
return "BLOB"
|
||||||
|
case fieldTypeDate:
|
||||||
|
return "DATE"
|
||||||
|
case fieldTypeDateTime:
|
||||||
|
return "DATETIME"
|
||||||
|
case fieldTypeDecimal:
|
||||||
|
return "DECIMAL"
|
||||||
|
case fieldTypeDouble:
|
||||||
|
return "DOUBLE"
|
||||||
|
case fieldTypeEnum:
|
||||||
|
return "ENUM"
|
||||||
|
case fieldTypeFloat:
|
||||||
|
return "FLOAT"
|
||||||
|
case fieldTypeGeometry:
|
||||||
|
return "GEOMETRY"
|
||||||
|
case fieldTypeInt24:
|
||||||
|
return "MEDIUMINT"
|
||||||
|
case fieldTypeJSON:
|
||||||
|
return "JSON"
|
||||||
|
case fieldTypeLong:
|
||||||
|
return "INT"
|
||||||
|
case fieldTypeLongBLOB:
|
||||||
|
if mf.charSet != collations[binaryCollation] {
|
||||||
|
return "LONGTEXT"
|
||||||
|
}
|
||||||
|
return "LONGBLOB"
|
||||||
|
case fieldTypeLongLong:
|
||||||
|
return "BIGINT"
|
||||||
|
case fieldTypeMediumBLOB:
|
||||||
|
if mf.charSet != collations[binaryCollation] {
|
||||||
|
return "MEDIUMTEXT"
|
||||||
|
}
|
||||||
|
return "MEDIUMBLOB"
|
||||||
|
case fieldTypeNewDate:
|
||||||
|
return "DATE"
|
||||||
|
case fieldTypeNewDecimal:
|
||||||
|
return "DECIMAL"
|
||||||
|
case fieldTypeNULL:
|
||||||
|
return "NULL"
|
||||||
|
case fieldTypeSet:
|
||||||
|
return "SET"
|
||||||
|
case fieldTypeShort:
|
||||||
|
return "SMALLINT"
|
||||||
|
case fieldTypeString:
|
||||||
|
if mf.charSet == collations[binaryCollation] {
|
||||||
|
return "BINARY"
|
||||||
|
}
|
||||||
|
return "CHAR"
|
||||||
|
case fieldTypeTime:
|
||||||
|
return "TIME"
|
||||||
|
case fieldTypeTimestamp:
|
||||||
|
return "TIMESTAMP"
|
||||||
|
case fieldTypeTiny:
|
||||||
|
return "TINYINT"
|
||||||
|
case fieldTypeTinyBLOB:
|
||||||
|
if mf.charSet != collations[binaryCollation] {
|
||||||
|
return "TINYTEXT"
|
||||||
|
}
|
||||||
|
return "TINYBLOB"
|
||||||
|
case fieldTypeVarChar:
|
||||||
|
if mf.charSet == collations[binaryCollation] {
|
||||||
|
return "VARBINARY"
|
||||||
|
}
|
||||||
|
return "VARCHAR"
|
||||||
|
case fieldTypeVarString:
|
||||||
|
if mf.charSet == collations[binaryCollation] {
|
||||||
|
return "VARBINARY"
|
||||||
|
}
|
||||||
|
return "VARCHAR"
|
||||||
|
case fieldTypeYear:
|
||||||
|
return "YEAR"
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
scanTypeFloat32 = reflect.TypeOf(float32(0))
|
||||||
|
scanTypeFloat64 = reflect.TypeOf(float64(0))
|
||||||
|
scanTypeInt8 = reflect.TypeOf(int8(0))
|
||||||
|
scanTypeInt16 = reflect.TypeOf(int16(0))
|
||||||
|
scanTypeInt32 = reflect.TypeOf(int32(0))
|
||||||
|
scanTypeInt64 = reflect.TypeOf(int64(0))
|
||||||
|
scanTypeNullFloat = reflect.TypeOf(sql.NullFloat64{})
|
||||||
|
scanTypeNullInt = reflect.TypeOf(sql.NullInt64{})
|
||||||
|
scanTypeNullTime = reflect.TypeOf(NullTime{})
|
||||||
|
scanTypeUint8 = reflect.TypeOf(uint8(0))
|
||||||
|
scanTypeUint16 = reflect.TypeOf(uint16(0))
|
||||||
|
scanTypeUint32 = reflect.TypeOf(uint32(0))
|
||||||
|
scanTypeUint64 = reflect.TypeOf(uint64(0))
|
||||||
|
scanTypeRawBytes = reflect.TypeOf(sql.RawBytes{})
|
||||||
|
scanTypeUnknown = reflect.TypeOf(new(interface{}))
|
||||||
|
)
|
||||||
|
|
||||||
|
type mysqlField struct {
|
||||||
|
tableName string
|
||||||
|
name string
|
||||||
|
length uint32
|
||||||
|
flags fieldFlag
|
||||||
|
fieldType fieldType
|
||||||
|
decimals byte
|
||||||
|
charSet uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mf *mysqlField) scanType() reflect.Type {
|
||||||
|
switch mf.fieldType {
|
||||||
|
case fieldTypeTiny:
|
||||||
|
if mf.flags&flagNotNULL != 0 {
|
||||||
|
if mf.flags&flagUnsigned != 0 {
|
||||||
|
return scanTypeUint8
|
||||||
|
}
|
||||||
|
return scanTypeInt8
|
||||||
|
}
|
||||||
|
return scanTypeNullInt
|
||||||
|
|
||||||
|
case fieldTypeShort, fieldTypeYear:
|
||||||
|
if mf.flags&flagNotNULL != 0 {
|
||||||
|
if mf.flags&flagUnsigned != 0 {
|
||||||
|
return scanTypeUint16
|
||||||
|
}
|
||||||
|
return scanTypeInt16
|
||||||
|
}
|
||||||
|
return scanTypeNullInt
|
||||||
|
|
||||||
|
case fieldTypeInt24, fieldTypeLong:
|
||||||
|
if mf.flags&flagNotNULL != 0 {
|
||||||
|
if mf.flags&flagUnsigned != 0 {
|
||||||
|
return scanTypeUint32
|
||||||
|
}
|
||||||
|
return scanTypeInt32
|
||||||
|
}
|
||||||
|
return scanTypeNullInt
|
||||||
|
|
||||||
|
case fieldTypeLongLong:
|
||||||
|
if mf.flags&flagNotNULL != 0 {
|
||||||
|
if mf.flags&flagUnsigned != 0 {
|
||||||
|
return scanTypeUint64
|
||||||
|
}
|
||||||
|
return scanTypeInt64
|
||||||
|
}
|
||||||
|
return scanTypeNullInt
|
||||||
|
|
||||||
|
case fieldTypeFloat:
|
||||||
|
if mf.flags&flagNotNULL != 0 {
|
||||||
|
return scanTypeFloat32
|
||||||
|
}
|
||||||
|
return scanTypeNullFloat
|
||||||
|
|
||||||
|
case fieldTypeDouble:
|
||||||
|
if mf.flags&flagNotNULL != 0 {
|
||||||
|
return scanTypeFloat64
|
||||||
|
}
|
||||||
|
return scanTypeNullFloat
|
||||||
|
|
||||||
|
case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar,
|
||||||
|
fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB,
|
||||||
|
fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB,
|
||||||
|
fieldTypeVarString, fieldTypeString, fieldTypeGeometry, fieldTypeJSON,
|
||||||
|
fieldTypeTime:
|
||||||
|
return scanTypeRawBytes
|
||||||
|
|
||||||
|
case fieldTypeDate, fieldTypeNewDate,
|
||||||
|
fieldTypeTimestamp, fieldTypeDateTime:
|
||||||
|
// NullTime is always returned for more consistent behavior as it can
|
||||||
|
// handle both cases of parseTime regardless if the field is nullable.
|
||||||
|
return scanTypeNullTime
|
||||||
|
|
||||||
|
default:
|
||||||
|
return scanTypeUnknown
|
||||||
|
}
|
||||||
|
}
|
182
vendor/github.com/go-sql-driver/mysql/infile.go
generated
vendored
Normal file
182
vendor/github.com/go-sql-driver/mysql/infile.go
generated
vendored
Normal file
@ -0,0 +1,182 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
fileRegister map[string]bool
|
||||||
|
fileRegisterLock sync.RWMutex
|
||||||
|
readerRegister map[string]func() io.Reader
|
||||||
|
readerRegisterLock sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegisterLocalFile adds the given file to the file whitelist,
|
||||||
|
// so that it can be used by "LOAD DATA LOCAL INFILE <filepath>".
|
||||||
|
// Alternatively you can allow the use of all local files with
|
||||||
|
// the DSN parameter 'allowAllFiles=true'
|
||||||
|
//
|
||||||
|
// filePath := "/home/gopher/data.csv"
|
||||||
|
// mysql.RegisterLocalFile(filePath)
|
||||||
|
// err := db.Exec("LOAD DATA LOCAL INFILE '" + filePath + "' INTO TABLE foo")
|
||||||
|
// if err != nil {
|
||||||
|
// ...
|
||||||
|
//
|
||||||
|
func RegisterLocalFile(filePath string) {
|
||||||
|
fileRegisterLock.Lock()
|
||||||
|
// lazy map init
|
||||||
|
if fileRegister == nil {
|
||||||
|
fileRegister = make(map[string]bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
fileRegister[strings.Trim(filePath, `"`)] = true
|
||||||
|
fileRegisterLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeregisterLocalFile removes the given filepath from the whitelist.
|
||||||
|
func DeregisterLocalFile(filePath string) {
|
||||||
|
fileRegisterLock.Lock()
|
||||||
|
delete(fileRegister, strings.Trim(filePath, `"`))
|
||||||
|
fileRegisterLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterReaderHandler registers a handler function which is used
|
||||||
|
// to receive a io.Reader.
|
||||||
|
// The Reader can be used by "LOAD DATA LOCAL INFILE Reader::<name>".
|
||||||
|
// If the handler returns a io.ReadCloser Close() is called when the
|
||||||
|
// request is finished.
|
||||||
|
//
|
||||||
|
// mysql.RegisterReaderHandler("data", func() io.Reader {
|
||||||
|
// var csvReader io.Reader // Some Reader that returns CSV data
|
||||||
|
// ... // Open Reader here
|
||||||
|
// return csvReader
|
||||||
|
// })
|
||||||
|
// err := db.Exec("LOAD DATA LOCAL INFILE 'Reader::data' INTO TABLE foo")
|
||||||
|
// if err != nil {
|
||||||
|
// ...
|
||||||
|
//
|
||||||
|
func RegisterReaderHandler(name string, handler func() io.Reader) {
|
||||||
|
readerRegisterLock.Lock()
|
||||||
|
// lazy map init
|
||||||
|
if readerRegister == nil {
|
||||||
|
readerRegister = make(map[string]func() io.Reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
readerRegister[name] = handler
|
||||||
|
readerRegisterLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeregisterReaderHandler removes the ReaderHandler function with
|
||||||
|
// the given name from the registry.
|
||||||
|
func DeregisterReaderHandler(name string) {
|
||||||
|
readerRegisterLock.Lock()
|
||||||
|
delete(readerRegister, name)
|
||||||
|
readerRegisterLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func deferredClose(err *error, closer io.Closer) {
|
||||||
|
closeErr := closer.Close()
|
||||||
|
if *err == nil {
|
||||||
|
*err = closeErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
|
||||||
|
var rdr io.Reader
|
||||||
|
var data []byte
|
||||||
|
packetSize := 16 * 1024 // 16KB is small enough for disk readahead and large enough for TCP
|
||||||
|
if mc.maxWriteSize < packetSize {
|
||||||
|
packetSize = mc.maxWriteSize
|
||||||
|
}
|
||||||
|
|
||||||
|
if idx := strings.Index(name, "Reader::"); idx == 0 || (idx > 0 && name[idx-1] == '/') { // io.Reader
|
||||||
|
// The server might return an an absolute path. See issue #355.
|
||||||
|
name = name[idx+8:]
|
||||||
|
|
||||||
|
readerRegisterLock.RLock()
|
||||||
|
handler, inMap := readerRegister[name]
|
||||||
|
readerRegisterLock.RUnlock()
|
||||||
|
|
||||||
|
if inMap {
|
||||||
|
rdr = handler()
|
||||||
|
if rdr != nil {
|
||||||
|
if cl, ok := rdr.(io.Closer); ok {
|
||||||
|
defer deferredClose(&err, cl)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("Reader '%s' is <nil>", name)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("Reader '%s' is not registered", name)
|
||||||
|
}
|
||||||
|
} else { // File
|
||||||
|
name = strings.Trim(name, `"`)
|
||||||
|
fileRegisterLock.RLock()
|
||||||
|
fr := fileRegister[name]
|
||||||
|
fileRegisterLock.RUnlock()
|
||||||
|
if mc.cfg.AllowAllFiles || fr {
|
||||||
|
var file *os.File
|
||||||
|
var fi os.FileInfo
|
||||||
|
|
||||||
|
if file, err = os.Open(name); err == nil {
|
||||||
|
defer deferredClose(&err, file)
|
||||||
|
|
||||||
|
// get file size
|
||||||
|
if fi, err = file.Stat(); err == nil {
|
||||||
|
rdr = file
|
||||||
|
if fileSize := int(fi.Size()); fileSize < packetSize {
|
||||||
|
packetSize = fileSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("local file '%s' is not registered", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// send content packets
|
||||||
|
// if packetSize == 0, the Reader contains no data
|
||||||
|
if err == nil && packetSize > 0 {
|
||||||
|
data := make([]byte, 4+packetSize)
|
||||||
|
var n int
|
||||||
|
for err == nil {
|
||||||
|
n, err = rdr.Read(data[4:])
|
||||||
|
if n > 0 {
|
||||||
|
if ioErr := mc.writePacket(data[:4+n]); ioErr != nil {
|
||||||
|
return ioErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err == io.EOF {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// send empty packet (termination)
|
||||||
|
if data == nil {
|
||||||
|
data = make([]byte, 4)
|
||||||
|
}
|
||||||
|
if ioErr := mc.writePacket(data[:4]); ioErr != nil {
|
||||||
|
return ioErr
|
||||||
|
}
|
||||||
|
|
||||||
|
// read OK packet
|
||||||
|
if err == nil {
|
||||||
|
return mc.readResultOK()
|
||||||
|
}
|
||||||
|
|
||||||
|
mc.readPacket()
|
||||||
|
return err
|
||||||
|
}
|
1286
vendor/github.com/go-sql-driver/mysql/packets.go
generated
vendored
Normal file
1286
vendor/github.com/go-sql-driver/mysql/packets.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
22
vendor/github.com/go-sql-driver/mysql/result.go
generated
vendored
Normal file
22
vendor/github.com/go-sql-driver/mysql/result.go
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
type mysqlResult struct {
|
||||||
|
affectedRows int64
|
||||||
|
insertId int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (res *mysqlResult) LastInsertId() (int64, error) {
|
||||||
|
return res.insertId, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (res *mysqlResult) RowsAffected() (int64, error) {
|
||||||
|
return res.affectedRows, nil
|
||||||
|
}
|
216
vendor/github.com/go-sql-driver/mysql/rows.go
generated
vendored
Normal file
216
vendor/github.com/go-sql-driver/mysql/rows.go
generated
vendored
Normal file
@ -0,0 +1,216 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
"io"
|
||||||
|
"math"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
type resultSet struct {
|
||||||
|
columns []mysqlField
|
||||||
|
columnNames []string
|
||||||
|
done bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type mysqlRows struct {
|
||||||
|
mc *mysqlConn
|
||||||
|
rs resultSet
|
||||||
|
finish func()
|
||||||
|
}
|
||||||
|
|
||||||
|
type binaryRows struct {
|
||||||
|
mysqlRows
|
||||||
|
}
|
||||||
|
|
||||||
|
type textRows struct {
|
||||||
|
mysqlRows
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *mysqlRows) Columns() []string {
|
||||||
|
if rows.rs.columnNames != nil {
|
||||||
|
return rows.rs.columnNames
|
||||||
|
}
|
||||||
|
|
||||||
|
columns := make([]string, len(rows.rs.columns))
|
||||||
|
if rows.mc != nil && rows.mc.cfg.ColumnsWithAlias {
|
||||||
|
for i := range columns {
|
||||||
|
if tableName := rows.rs.columns[i].tableName; len(tableName) > 0 {
|
||||||
|
columns[i] = tableName + "." + rows.rs.columns[i].name
|
||||||
|
} else {
|
||||||
|
columns[i] = rows.rs.columns[i].name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for i := range columns {
|
||||||
|
columns[i] = rows.rs.columns[i].name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rows.rs.columnNames = columns
|
||||||
|
return columns
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *mysqlRows) ColumnTypeDatabaseTypeName(i int) string {
|
||||||
|
return rows.rs.columns[i].typeDatabaseName()
|
||||||
|
}
|
||||||
|
|
||||||
|
// func (rows *mysqlRows) ColumnTypeLength(i int) (length int64, ok bool) {
|
||||||
|
// return int64(rows.rs.columns[i].length), true
|
||||||
|
// }
|
||||||
|
|
||||||
|
func (rows *mysqlRows) ColumnTypeNullable(i int) (nullable, ok bool) {
|
||||||
|
return rows.rs.columns[i].flags&flagNotNULL == 0, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *mysqlRows) ColumnTypePrecisionScale(i int) (int64, int64, bool) {
|
||||||
|
column := rows.rs.columns[i]
|
||||||
|
decimals := int64(column.decimals)
|
||||||
|
|
||||||
|
switch column.fieldType {
|
||||||
|
case fieldTypeDecimal, fieldTypeNewDecimal:
|
||||||
|
if decimals > 0 {
|
||||||
|
return int64(column.length) - 2, decimals, true
|
||||||
|
}
|
||||||
|
return int64(column.length) - 1, decimals, true
|
||||||
|
case fieldTypeTimestamp, fieldTypeDateTime, fieldTypeTime:
|
||||||
|
return decimals, decimals, true
|
||||||
|
case fieldTypeFloat, fieldTypeDouble:
|
||||||
|
if decimals == 0x1f {
|
||||||
|
return math.MaxInt64, math.MaxInt64, true
|
||||||
|
}
|
||||||
|
return math.MaxInt64, decimals, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *mysqlRows) ColumnTypeScanType(i int) reflect.Type {
|
||||||
|
return rows.rs.columns[i].scanType()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *mysqlRows) Close() (err error) {
|
||||||
|
if f := rows.finish; f != nil {
|
||||||
|
f()
|
||||||
|
rows.finish = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mc := rows.mc
|
||||||
|
if mc == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := mc.error(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove unread packets from stream
|
||||||
|
if !rows.rs.done {
|
||||||
|
err = mc.readUntilEOF()
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
if err = mc.discardResults(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rows.mc = nil
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *mysqlRows) HasNextResultSet() (b bool) {
|
||||||
|
if rows.mc == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return rows.mc.status&statusMoreResultsExists != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *mysqlRows) nextResultSet() (int, error) {
|
||||||
|
if rows.mc == nil {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
if err := rows.mc.error(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove unread packets from stream
|
||||||
|
if !rows.rs.done {
|
||||||
|
if err := rows.mc.readUntilEOF(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
rows.rs.done = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if !rows.HasNextResultSet() {
|
||||||
|
rows.mc = nil
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
rows.rs = resultSet{}
|
||||||
|
return rows.mc.readResultSetHeaderPacket()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *mysqlRows) nextNotEmptyResultSet() (int, error) {
|
||||||
|
for {
|
||||||
|
resLen, err := rows.nextResultSet()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resLen > 0 {
|
||||||
|
return resLen, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rows.rs.done = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *binaryRows) NextResultSet() error {
|
||||||
|
resLen, err := rows.nextNotEmptyResultSet()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows.rs.columns, err = rows.mc.readColumns(resLen)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *binaryRows) Next(dest []driver.Value) error {
|
||||||
|
if mc := rows.mc; mc != nil {
|
||||||
|
if err := mc.error(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch next row from stream
|
||||||
|
return rows.readRow(dest)
|
||||||
|
}
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *textRows) NextResultSet() (err error) {
|
||||||
|
resLen, err := rows.nextNotEmptyResultSet()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows.rs.columns, err = rows.mc.readColumns(resLen)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rows *textRows) Next(dest []driver.Value) error {
|
||||||
|
if mc := rows.mc; mc != nil {
|
||||||
|
if err := mc.error(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch next row from stream
|
||||||
|
return rows.readRow(dest)
|
||||||
|
}
|
||||||
|
return io.EOF
|
||||||
|
}
|
211
vendor/github.com/go-sql-driver/mysql/statement.go
generated
vendored
Normal file
211
vendor/github.com/go-sql-driver/mysql/statement.go
generated
vendored
Normal file
@ -0,0 +1,211 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mysqlStmt struct {
|
||||||
|
mc *mysqlConn
|
||||||
|
id uint32
|
||||||
|
paramCount int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stmt *mysqlStmt) Close() error {
|
||||||
|
if stmt.mc == nil || stmt.mc.closed.IsSet() {
|
||||||
|
// driver.Stmt.Close can be called more than once, thus this function
|
||||||
|
// has to be idempotent.
|
||||||
|
// See also Issue #450 and golang/go#16019.
|
||||||
|
//errLog.Print(ErrInvalidConn)
|
||||||
|
return driver.ErrBadConn
|
||||||
|
}
|
||||||
|
|
||||||
|
err := stmt.mc.writeCommandPacketUint32(comStmtClose, stmt.id)
|
||||||
|
stmt.mc = nil
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stmt *mysqlStmt) NumInput() int {
|
||||||
|
return stmt.paramCount
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stmt *mysqlStmt) ColumnConverter(idx int) driver.ValueConverter {
|
||||||
|
return converter{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
|
||||||
|
if stmt.mc.closed.IsSet() {
|
||||||
|
errLog.Print(ErrInvalidConn)
|
||||||
|
return nil, driver.ErrBadConn
|
||||||
|
}
|
||||||
|
// Send command
|
||||||
|
err := stmt.writeExecutePacket(args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, stmt.mc.markBadConn(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mc := stmt.mc
|
||||||
|
|
||||||
|
mc.affectedRows = 0
|
||||||
|
mc.insertId = 0
|
||||||
|
|
||||||
|
// Read Result
|
||||||
|
resLen, err := mc.readResultSetHeaderPacket()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resLen > 0 {
|
||||||
|
// Columns
|
||||||
|
if err = mc.readUntilEOF(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rows
|
||||||
|
if err := mc.readUntilEOF(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := mc.discardResults(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &mysqlResult{
|
||||||
|
affectedRows: int64(mc.affectedRows),
|
||||||
|
insertId: int64(mc.insertId),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
|
||||||
|
return stmt.query(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stmt *mysqlStmt) query(args []driver.Value) (*binaryRows, error) {
|
||||||
|
if stmt.mc.closed.IsSet() {
|
||||||
|
errLog.Print(ErrInvalidConn)
|
||||||
|
return nil, driver.ErrBadConn
|
||||||
|
}
|
||||||
|
// Send command
|
||||||
|
err := stmt.writeExecutePacket(args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, stmt.mc.markBadConn(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mc := stmt.mc
|
||||||
|
|
||||||
|
// Read Result
|
||||||
|
resLen, err := mc.readResultSetHeaderPacket()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rows := new(binaryRows)
|
||||||
|
|
||||||
|
if resLen > 0 {
|
||||||
|
rows.mc = mc
|
||||||
|
rows.rs.columns, err = mc.readColumns(resLen)
|
||||||
|
} else {
|
||||||
|
rows.rs.done = true
|
||||||
|
|
||||||
|
switch err := rows.NextResultSet(); err {
|
||||||
|
case nil, io.EOF:
|
||||||
|
return rows, nil
|
||||||
|
default:
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rows, err
|
||||||
|
}
|
||||||
|
|
||||||
|
type converter struct{}
|
||||||
|
|
||||||
|
// ConvertValue mirrors the reference/default converter in database/sql/driver
|
||||||
|
// with _one_ exception. We support uint64 with their high bit and the default
|
||||||
|
// implementation does not. This function should be kept in sync with
|
||||||
|
// database/sql/driver defaultConverter.ConvertValue() except for that
|
||||||
|
// deliberate difference.
|
||||||
|
func (c converter) ConvertValue(v interface{}) (driver.Value, error) {
|
||||||
|
if driver.IsValue(v) {
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if vr, ok := v.(driver.Valuer); ok {
|
||||||
|
sv, err := callValuerValue(vr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !driver.IsValue(sv) {
|
||||||
|
return nil, fmt.Errorf("non-Value type %T returned from Value", sv)
|
||||||
|
}
|
||||||
|
return sv, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rv := reflect.ValueOf(v)
|
||||||
|
switch rv.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
// indirect pointers
|
||||||
|
if rv.IsNil() {
|
||||||
|
return nil, nil
|
||||||
|
} else {
|
||||||
|
return c.ConvertValue(rv.Elem().Interface())
|
||||||
|
}
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
return rv.Int(), nil
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
||||||
|
return int64(rv.Uint()), nil
|
||||||
|
case reflect.Uint64:
|
||||||
|
u64 := rv.Uint()
|
||||||
|
if u64 >= 1<<63 {
|
||||||
|
return strconv.FormatUint(u64, 10), nil
|
||||||
|
}
|
||||||
|
return int64(u64), nil
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
return rv.Float(), nil
|
||||||
|
case reflect.Bool:
|
||||||
|
return rv.Bool(), nil
|
||||||
|
case reflect.Slice:
|
||||||
|
ek := rv.Type().Elem().Kind()
|
||||||
|
if ek == reflect.Uint8 {
|
||||||
|
return rv.Bytes(), nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("unsupported type %T, a slice of %s", v, ek)
|
||||||
|
case reflect.String:
|
||||||
|
return rv.String(), nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("unsupported type %T, a %s", v, rv.Kind())
|
||||||
|
}
|
||||||
|
|
||||||
|
var valuerReflectType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
|
||||||
|
|
||||||
|
// callValuerValue returns vr.Value(), with one exception:
|
||||||
|
// If vr.Value is an auto-generated method on a pointer type and the
|
||||||
|
// pointer is nil, it would panic at runtime in the panicwrap
|
||||||
|
// method. Treat it like nil instead.
|
||||||
|
//
|
||||||
|
// This is so people can implement driver.Value on value types and
|
||||||
|
// still use nil pointers to those types to mean nil/NULL, just like
|
||||||
|
// string/*string.
|
||||||
|
//
|
||||||
|
// This is an exact copy of the same-named unexported function from the
|
||||||
|
// database/sql package.
|
||||||
|
func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
|
||||||
|
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Ptr &&
|
||||||
|
rv.IsNil() &&
|
||||||
|
rv.Type().Elem().Implements(valuerReflectType) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return vr.Value()
|
||||||
|
}
|
31
vendor/github.com/go-sql-driver/mysql/transaction.go
generated
vendored
Normal file
31
vendor/github.com/go-sql-driver/mysql/transaction.go
generated
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
type mysqlTx struct {
|
||||||
|
mc *mysqlConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *mysqlTx) Commit() (err error) {
|
||||||
|
if tx.mc == nil || tx.mc.closed.IsSet() {
|
||||||
|
return ErrInvalidConn
|
||||||
|
}
|
||||||
|
err = tx.mc.exec("COMMIT")
|
||||||
|
tx.mc = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *mysqlTx) Rollback() (err error) {
|
||||||
|
if tx.mc == nil || tx.mc.closed.IsSet() {
|
||||||
|
return ErrInvalidConn
|
||||||
|
}
|
||||||
|
err = tx.mc.exec("ROLLBACK")
|
||||||
|
tx.mc = nil
|
||||||
|
return
|
||||||
|
}
|
726
vendor/github.com/go-sql-driver/mysql/utils.go
generated
vendored
Normal file
726
vendor/github.com/go-sql-driver/mysql/utils.go
generated
vendored
Normal file
@ -0,0 +1,726 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"database/sql/driver"
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Registry for custom tls.Configs
|
||||||
|
var (
|
||||||
|
tlsConfigLock sync.RWMutex
|
||||||
|
tlsConfigRegistry map[string]*tls.Config
|
||||||
|
)
|
||||||
|
|
||||||
|
// RegisterTLSConfig registers a custom tls.Config to be used with sql.Open.
|
||||||
|
// Use the key as a value in the DSN where tls=value.
|
||||||
|
//
|
||||||
|
// Note: The provided tls.Config is exclusively owned by the driver after
|
||||||
|
// registering it.
|
||||||
|
//
|
||||||
|
// rootCertPool := x509.NewCertPool()
|
||||||
|
// pem, err := ioutil.ReadFile("/path/ca-cert.pem")
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatal(err)
|
||||||
|
// }
|
||||||
|
// if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
|
||||||
|
// log.Fatal("Failed to append PEM.")
|
||||||
|
// }
|
||||||
|
// clientCert := make([]tls.Certificate, 0, 1)
|
||||||
|
// certs, err := tls.LoadX509KeyPair("/path/client-cert.pem", "/path/client-key.pem")
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatal(err)
|
||||||
|
// }
|
||||||
|
// clientCert = append(clientCert, certs)
|
||||||
|
// mysql.RegisterTLSConfig("custom", &tls.Config{
|
||||||
|
// RootCAs: rootCertPool,
|
||||||
|
// Certificates: clientCert,
|
||||||
|
// })
|
||||||
|
// db, err := sql.Open("mysql", "user@tcp(localhost:3306)/test?tls=custom")
|
||||||
|
//
|
||||||
|
func RegisterTLSConfig(key string, config *tls.Config) error {
|
||||||
|
if _, isBool := readBool(key); isBool || strings.ToLower(key) == "skip-verify" {
|
||||||
|
return fmt.Errorf("key '%s' is reserved", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsConfigLock.Lock()
|
||||||
|
if tlsConfigRegistry == nil {
|
||||||
|
tlsConfigRegistry = make(map[string]*tls.Config)
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsConfigRegistry[key] = config
|
||||||
|
tlsConfigLock.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeregisterTLSConfig removes the tls.Config associated with key.
|
||||||
|
func DeregisterTLSConfig(key string) {
|
||||||
|
tlsConfigLock.Lock()
|
||||||
|
if tlsConfigRegistry != nil {
|
||||||
|
delete(tlsConfigRegistry, key)
|
||||||
|
}
|
||||||
|
tlsConfigLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTLSConfigClone(key string) (config *tls.Config) {
|
||||||
|
tlsConfigLock.RLock()
|
||||||
|
if v, ok := tlsConfigRegistry[key]; ok {
|
||||||
|
config = cloneTLSConfig(v)
|
||||||
|
}
|
||||||
|
tlsConfigLock.RUnlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the bool value of the input.
|
||||||
|
// The 2nd return value indicates if the input was a valid bool value
|
||||||
|
func readBool(input string) (value bool, valid bool) {
|
||||||
|
switch input {
|
||||||
|
case "1", "true", "TRUE", "True":
|
||||||
|
return true, true
|
||||||
|
case "0", "false", "FALSE", "False":
|
||||||
|
return false, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not a valid bool value
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Time related utils *
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
// NullTime represents a time.Time that may be NULL.
|
||||||
|
// NullTime implements the Scanner interface so
|
||||||
|
// it can be used as a scan destination:
|
||||||
|
//
|
||||||
|
// var nt NullTime
|
||||||
|
// err := db.QueryRow("SELECT time FROM foo WHERE id=?", id).Scan(&nt)
|
||||||
|
// ...
|
||||||
|
// if nt.Valid {
|
||||||
|
// // use nt.Time
|
||||||
|
// } else {
|
||||||
|
// // NULL value
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// This NullTime implementation is not driver-specific
|
||||||
|
type NullTime struct {
|
||||||
|
Time time.Time
|
||||||
|
Valid bool // Valid is true if Time is not NULL
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan implements the Scanner interface.
|
||||||
|
// The value type must be time.Time or string / []byte (formatted time-string),
|
||||||
|
// otherwise Scan fails.
|
||||||
|
func (nt *NullTime) Scan(value interface{}) (err error) {
|
||||||
|
if value == nil {
|
||||||
|
nt.Time, nt.Valid = time.Time{}, false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch v := value.(type) {
|
||||||
|
case time.Time:
|
||||||
|
nt.Time, nt.Valid = v, true
|
||||||
|
return
|
||||||
|
case []byte:
|
||||||
|
nt.Time, err = parseDateTime(string(v), time.UTC)
|
||||||
|
nt.Valid = (err == nil)
|
||||||
|
return
|
||||||
|
case string:
|
||||||
|
nt.Time, err = parseDateTime(v, time.UTC)
|
||||||
|
nt.Valid = (err == nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
nt.Valid = false
|
||||||
|
return fmt.Errorf("Can't convert %T to time.Time", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value implements the driver Valuer interface.
|
||||||
|
func (nt NullTime) Value() (driver.Value, error) {
|
||||||
|
if !nt.Valid {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return nt.Time, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseDateTime(str string, loc *time.Location) (t time.Time, err error) {
|
||||||
|
base := "0000-00-00 00:00:00.0000000"
|
||||||
|
switch len(str) {
|
||||||
|
case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
|
||||||
|
if str == base[:len(str)] {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t, err = time.Parse(timeFormat[:len(str)], str)
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("invalid time string: %s", str)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjust location
|
||||||
|
if err == nil && loc != time.UTC {
|
||||||
|
y, mo, d := t.Date()
|
||||||
|
h, mi, s := t.Clock()
|
||||||
|
t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Value, error) {
|
||||||
|
switch num {
|
||||||
|
case 0:
|
||||||
|
return time.Time{}, nil
|
||||||
|
case 4:
|
||||||
|
return time.Date(
|
||||||
|
int(binary.LittleEndian.Uint16(data[:2])), // year
|
||||||
|
time.Month(data[2]), // month
|
||||||
|
int(data[3]), // day
|
||||||
|
0, 0, 0, 0,
|
||||||
|
loc,
|
||||||
|
), nil
|
||||||
|
case 7:
|
||||||
|
return time.Date(
|
||||||
|
int(binary.LittleEndian.Uint16(data[:2])), // year
|
||||||
|
time.Month(data[2]), // month
|
||||||
|
int(data[3]), // day
|
||||||
|
int(data[4]), // hour
|
||||||
|
int(data[5]), // minutes
|
||||||
|
int(data[6]), // seconds
|
||||||
|
0,
|
||||||
|
loc,
|
||||||
|
), nil
|
||||||
|
case 11:
|
||||||
|
return time.Date(
|
||||||
|
int(binary.LittleEndian.Uint16(data[:2])), // year
|
||||||
|
time.Month(data[2]), // month
|
||||||
|
int(data[3]), // day
|
||||||
|
int(data[4]), // hour
|
||||||
|
int(data[5]), // minutes
|
||||||
|
int(data[6]), // seconds
|
||||||
|
int(binary.LittleEndian.Uint32(data[7:11]))*1000, // nanoseconds
|
||||||
|
loc,
|
||||||
|
), nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("invalid DATETIME packet length %d", num)
|
||||||
|
}
|
||||||
|
|
||||||
|
// zeroDateTime is used in formatBinaryDateTime to avoid an allocation
|
||||||
|
// if the DATE or DATETIME has the zero value.
|
||||||
|
// It must never be changed.
|
||||||
|
// The current behavior depends on database/sql copying the result.
|
||||||
|
var zeroDateTime = []byte("0000-00-00 00:00:00.000000")
|
||||||
|
|
||||||
|
const digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
||||||
|
const digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
|
||||||
|
|
||||||
|
func appendMicrosecs(dst, src []byte, decimals int) []byte {
|
||||||
|
if decimals <= 0 {
|
||||||
|
return dst
|
||||||
|
}
|
||||||
|
if len(src) == 0 {
|
||||||
|
return append(dst, ".000000"[:decimals+1]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
microsecs := binary.LittleEndian.Uint32(src[:4])
|
||||||
|
p1 := byte(microsecs / 10000)
|
||||||
|
microsecs -= 10000 * uint32(p1)
|
||||||
|
p2 := byte(microsecs / 100)
|
||||||
|
microsecs -= 100 * uint32(p2)
|
||||||
|
p3 := byte(microsecs)
|
||||||
|
|
||||||
|
switch decimals {
|
||||||
|
default:
|
||||||
|
return append(dst, '.',
|
||||||
|
digits10[p1], digits01[p1],
|
||||||
|
digits10[p2], digits01[p2],
|
||||||
|
digits10[p3], digits01[p3],
|
||||||
|
)
|
||||||
|
case 1:
|
||||||
|
return append(dst, '.',
|
||||||
|
digits10[p1],
|
||||||
|
)
|
||||||
|
case 2:
|
||||||
|
return append(dst, '.',
|
||||||
|
digits10[p1], digits01[p1],
|
||||||
|
)
|
||||||
|
case 3:
|
||||||
|
return append(dst, '.',
|
||||||
|
digits10[p1], digits01[p1],
|
||||||
|
digits10[p2],
|
||||||
|
)
|
||||||
|
case 4:
|
||||||
|
return append(dst, '.',
|
||||||
|
digits10[p1], digits01[p1],
|
||||||
|
digits10[p2], digits01[p2],
|
||||||
|
)
|
||||||
|
case 5:
|
||||||
|
return append(dst, '.',
|
||||||
|
digits10[p1], digits01[p1],
|
||||||
|
digits10[p2], digits01[p2],
|
||||||
|
digits10[p3],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatBinaryDateTime(src []byte, length uint8) (driver.Value, error) {
|
||||||
|
// length expects the deterministic length of the zero value,
|
||||||
|
// negative time and 100+ hours are automatically added if needed
|
||||||
|
if len(src) == 0 {
|
||||||
|
return zeroDateTime[:length], nil
|
||||||
|
}
|
||||||
|
var dst []byte // return value
|
||||||
|
var p1, p2, p3 byte // current digit pair
|
||||||
|
|
||||||
|
switch length {
|
||||||
|
case 10, 19, 21, 22, 23, 24, 25, 26:
|
||||||
|
default:
|
||||||
|
t := "DATE"
|
||||||
|
if length > 10 {
|
||||||
|
t += "TIME"
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("illegal %s length %d", t, length)
|
||||||
|
}
|
||||||
|
switch len(src) {
|
||||||
|
case 4, 7, 11:
|
||||||
|
default:
|
||||||
|
t := "DATE"
|
||||||
|
if length > 10 {
|
||||||
|
t += "TIME"
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("illegal %s packet length %d", t, len(src))
|
||||||
|
}
|
||||||
|
dst = make([]byte, 0, length)
|
||||||
|
// start with the date
|
||||||
|
year := binary.LittleEndian.Uint16(src[:2])
|
||||||
|
pt := year / 100
|
||||||
|
p1 = byte(year - 100*uint16(pt))
|
||||||
|
p2, p3 = src[2], src[3]
|
||||||
|
dst = append(dst,
|
||||||
|
digits10[pt], digits01[pt],
|
||||||
|
digits10[p1], digits01[p1], '-',
|
||||||
|
digits10[p2], digits01[p2], '-',
|
||||||
|
digits10[p3], digits01[p3],
|
||||||
|
)
|
||||||
|
if length == 10 {
|
||||||
|
return dst, nil
|
||||||
|
}
|
||||||
|
if len(src) == 4 {
|
||||||
|
return append(dst, zeroDateTime[10:length]...), nil
|
||||||
|
}
|
||||||
|
dst = append(dst, ' ')
|
||||||
|
p1 = src[4] // hour
|
||||||
|
src = src[5:]
|
||||||
|
|
||||||
|
// p1 is 2-digit hour, src is after hour
|
||||||
|
p2, p3 = src[0], src[1]
|
||||||
|
dst = append(dst,
|
||||||
|
digits10[p1], digits01[p1], ':',
|
||||||
|
digits10[p2], digits01[p2], ':',
|
||||||
|
digits10[p3], digits01[p3],
|
||||||
|
)
|
||||||
|
return appendMicrosecs(dst, src[2:], int(length)-20), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatBinaryTime(src []byte, length uint8) (driver.Value, error) {
|
||||||
|
// length expects the deterministic length of the zero value,
|
||||||
|
// negative time and 100+ hours are automatically added if needed
|
||||||
|
if len(src) == 0 {
|
||||||
|
return zeroDateTime[11 : 11+length], nil
|
||||||
|
}
|
||||||
|
var dst []byte // return value
|
||||||
|
|
||||||
|
switch length {
|
||||||
|
case
|
||||||
|
8, // time (can be up to 10 when negative and 100+ hours)
|
||||||
|
10, 11, 12, 13, 14, 15: // time with fractional seconds
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("illegal TIME length %d", length)
|
||||||
|
}
|
||||||
|
switch len(src) {
|
||||||
|
case 8, 12:
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("invalid TIME packet length %d", len(src))
|
||||||
|
}
|
||||||
|
// +2 to enable negative time and 100+ hours
|
||||||
|
dst = make([]byte, 0, length+2)
|
||||||
|
if src[0] == 1 {
|
||||||
|
dst = append(dst, '-')
|
||||||
|
}
|
||||||
|
days := binary.LittleEndian.Uint32(src[1:5])
|
||||||
|
hours := int64(days)*24 + int64(src[5])
|
||||||
|
|
||||||
|
if hours >= 100 {
|
||||||
|
dst = strconv.AppendInt(dst, hours, 10)
|
||||||
|
} else {
|
||||||
|
dst = append(dst, digits10[hours], digits01[hours])
|
||||||
|
}
|
||||||
|
|
||||||
|
min, sec := src[6], src[7]
|
||||||
|
dst = append(dst, ':',
|
||||||
|
digits10[min], digits01[min], ':',
|
||||||
|
digits10[sec], digits01[sec],
|
||||||
|
)
|
||||||
|
return appendMicrosecs(dst, src[8:], int(length)-9), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Convert from and to bytes *
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
func uint64ToBytes(n uint64) []byte {
|
||||||
|
return []byte{
|
||||||
|
byte(n),
|
||||||
|
byte(n >> 8),
|
||||||
|
byte(n >> 16),
|
||||||
|
byte(n >> 24),
|
||||||
|
byte(n >> 32),
|
||||||
|
byte(n >> 40),
|
||||||
|
byte(n >> 48),
|
||||||
|
byte(n >> 56),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func uint64ToString(n uint64) []byte {
|
||||||
|
var a [20]byte
|
||||||
|
i := 20
|
||||||
|
|
||||||
|
// U+0030 = 0
|
||||||
|
// ...
|
||||||
|
// U+0039 = 9
|
||||||
|
|
||||||
|
var q uint64
|
||||||
|
for n >= 10 {
|
||||||
|
i--
|
||||||
|
q = n / 10
|
||||||
|
a[i] = uint8(n-q*10) + 0x30
|
||||||
|
n = q
|
||||||
|
}
|
||||||
|
|
||||||
|
i--
|
||||||
|
a[i] = uint8(n) + 0x30
|
||||||
|
|
||||||
|
return a[i:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// treats string value as unsigned integer representation
|
||||||
|
func stringToInt(b []byte) int {
|
||||||
|
val := 0
|
||||||
|
for i := range b {
|
||||||
|
val *= 10
|
||||||
|
val += int(b[i] - 0x30)
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns the string read as a bytes slice, wheter the value is NULL,
|
||||||
|
// the number of bytes read and an error, in case the string is longer than
|
||||||
|
// the input slice
|
||||||
|
func readLengthEncodedString(b []byte) ([]byte, bool, int, error) {
|
||||||
|
// Get length
|
||||||
|
num, isNull, n := readLengthEncodedInteger(b)
|
||||||
|
if num < 1 {
|
||||||
|
return b[n:n], isNull, n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
n += int(num)
|
||||||
|
|
||||||
|
// Check data length
|
||||||
|
if len(b) >= n {
|
||||||
|
return b[n-int(num) : n : n], false, n, nil
|
||||||
|
}
|
||||||
|
return nil, false, n, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns the number of bytes skipped and an error, in case the string is
|
||||||
|
// longer than the input slice
|
||||||
|
func skipLengthEncodedString(b []byte) (int, error) {
|
||||||
|
// Get length
|
||||||
|
num, _, n := readLengthEncodedInteger(b)
|
||||||
|
if num < 1 {
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
n += int(num)
|
||||||
|
|
||||||
|
// Check data length
|
||||||
|
if len(b) >= n {
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
return n, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns the number read, whether the value is NULL and the number of bytes read
|
||||||
|
func readLengthEncodedInteger(b []byte) (uint64, bool, int) {
|
||||||
|
// See issue #349
|
||||||
|
if len(b) == 0 {
|
||||||
|
return 0, true, 1
|
||||||
|
}
|
||||||
|
|
||||||
|
switch b[0] {
|
||||||
|
// 251: NULL
|
||||||
|
case 0xfb:
|
||||||
|
return 0, true, 1
|
||||||
|
|
||||||
|
// 252: value of following 2
|
||||||
|
case 0xfc:
|
||||||
|
return uint64(b[1]) | uint64(b[2])<<8, false, 3
|
||||||
|
|
||||||
|
// 253: value of following 3
|
||||||
|
case 0xfd:
|
||||||
|
return uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16, false, 4
|
||||||
|
|
||||||
|
// 254: value of following 8
|
||||||
|
case 0xfe:
|
||||||
|
return uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16 |
|
||||||
|
uint64(b[4])<<24 | uint64(b[5])<<32 | uint64(b[6])<<40 |
|
||||||
|
uint64(b[7])<<48 | uint64(b[8])<<56,
|
||||||
|
false, 9
|
||||||
|
}
|
||||||
|
|
||||||
|
// 0-250: value of first byte
|
||||||
|
return uint64(b[0]), false, 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// encodes a uint64 value and appends it to the given bytes slice
|
||||||
|
func appendLengthEncodedInteger(b []byte, n uint64) []byte {
|
||||||
|
switch {
|
||||||
|
case n <= 250:
|
||||||
|
return append(b, byte(n))
|
||||||
|
|
||||||
|
case n <= 0xffff:
|
||||||
|
return append(b, 0xfc, byte(n), byte(n>>8))
|
||||||
|
|
||||||
|
case n <= 0xffffff:
|
||||||
|
return append(b, 0xfd, byte(n), byte(n>>8), byte(n>>16))
|
||||||
|
}
|
||||||
|
return append(b, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
|
||||||
|
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
|
||||||
|
}
|
||||||
|
|
||||||
|
// reserveBuffer checks cap(buf) and expand buffer to len(buf) + appendSize.
|
||||||
|
// If cap(buf) is not enough, reallocate new buffer.
|
||||||
|
func reserveBuffer(buf []byte, appendSize int) []byte {
|
||||||
|
newSize := len(buf) + appendSize
|
||||||
|
if cap(buf) < newSize {
|
||||||
|
// Grow buffer exponentially
|
||||||
|
newBuf := make([]byte, len(buf)*2+appendSize)
|
||||||
|
copy(newBuf, buf)
|
||||||
|
buf = newBuf
|
||||||
|
}
|
||||||
|
return buf[:newSize]
|
||||||
|
}
|
||||||
|
|
||||||
|
// escapeBytesBackslash escapes []byte with backslashes (\)
|
||||||
|
// This escapes the contents of a string (provided as []byte) by adding backslashes before special
|
||||||
|
// characters, and turning others into specific escape sequences, such as
|
||||||
|
// turning newlines into \n and null bytes into \0.
|
||||||
|
// https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L823-L932
|
||||||
|
func escapeBytesBackslash(buf, v []byte) []byte {
|
||||||
|
pos := len(buf)
|
||||||
|
buf = reserveBuffer(buf, len(v)*2)
|
||||||
|
|
||||||
|
for _, c := range v {
|
||||||
|
switch c {
|
||||||
|
case '\x00':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '0'
|
||||||
|
pos += 2
|
||||||
|
case '\n':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'n'
|
||||||
|
pos += 2
|
||||||
|
case '\r':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'r'
|
||||||
|
pos += 2
|
||||||
|
case '\x1a':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'Z'
|
||||||
|
pos += 2
|
||||||
|
case '\'':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '\''
|
||||||
|
pos += 2
|
||||||
|
case '"':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '"'
|
||||||
|
pos += 2
|
||||||
|
case '\\':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '\\'
|
||||||
|
pos += 2
|
||||||
|
default:
|
||||||
|
buf[pos] = c
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf[:pos]
|
||||||
|
}
|
||||||
|
|
||||||
|
// escapeStringBackslash is similar to escapeBytesBackslash but for string.
|
||||||
|
func escapeStringBackslash(buf []byte, v string) []byte {
|
||||||
|
pos := len(buf)
|
||||||
|
buf = reserveBuffer(buf, len(v)*2)
|
||||||
|
|
||||||
|
for i := 0; i < len(v); i++ {
|
||||||
|
c := v[i]
|
||||||
|
switch c {
|
||||||
|
case '\x00':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '0'
|
||||||
|
pos += 2
|
||||||
|
case '\n':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'n'
|
||||||
|
pos += 2
|
||||||
|
case '\r':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'r'
|
||||||
|
pos += 2
|
||||||
|
case '\x1a':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'Z'
|
||||||
|
pos += 2
|
||||||
|
case '\'':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '\''
|
||||||
|
pos += 2
|
||||||
|
case '"':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '"'
|
||||||
|
pos += 2
|
||||||
|
case '\\':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '\\'
|
||||||
|
pos += 2
|
||||||
|
default:
|
||||||
|
buf[pos] = c
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf[:pos]
|
||||||
|
}
|
||||||
|
|
||||||
|
// escapeBytesQuotes escapes apostrophes in []byte by doubling them up.
|
||||||
|
// This escapes the contents of a string by doubling up any apostrophes that
|
||||||
|
// it contains. This is used when the NO_BACKSLASH_ESCAPES SQL_MODE is in
|
||||||
|
// effect on the server.
|
||||||
|
// https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L963-L1038
|
||||||
|
func escapeBytesQuotes(buf, v []byte) []byte {
|
||||||
|
pos := len(buf)
|
||||||
|
buf = reserveBuffer(buf, len(v)*2)
|
||||||
|
|
||||||
|
for _, c := range v {
|
||||||
|
if c == '\'' {
|
||||||
|
buf[pos] = '\''
|
||||||
|
buf[pos+1] = '\''
|
||||||
|
pos += 2
|
||||||
|
} else {
|
||||||
|
buf[pos] = c
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf[:pos]
|
||||||
|
}
|
||||||
|
|
||||||
|
// escapeStringQuotes is similar to escapeBytesQuotes but for string.
|
||||||
|
func escapeStringQuotes(buf []byte, v string) []byte {
|
||||||
|
pos := len(buf)
|
||||||
|
buf = reserveBuffer(buf, len(v)*2)
|
||||||
|
|
||||||
|
for i := 0; i < len(v); i++ {
|
||||||
|
c := v[i]
|
||||||
|
if c == '\'' {
|
||||||
|
buf[pos] = '\''
|
||||||
|
buf[pos+1] = '\''
|
||||||
|
pos += 2
|
||||||
|
} else {
|
||||||
|
buf[pos] = c
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf[:pos]
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Sync utils *
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
// noCopy may be embedded into structs which must not be copied
|
||||||
|
// after the first use.
|
||||||
|
//
|
||||||
|
// See https://github.com/golang/go/issues/8005#issuecomment-190753527
|
||||||
|
// for details.
|
||||||
|
type noCopy struct{}
|
||||||
|
|
||||||
|
// Lock is a no-op used by -copylocks checker from `go vet`.
|
||||||
|
func (*noCopy) Lock() {}
|
||||||
|
|
||||||
|
// atomicBool is a wrapper around uint32 for usage as a boolean value with
|
||||||
|
// atomic access.
|
||||||
|
type atomicBool struct {
|
||||||
|
_noCopy noCopy
|
||||||
|
value uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns wether the current boolean value is true
|
||||||
|
func (ab *atomicBool) IsSet() bool {
|
||||||
|
return atomic.LoadUint32(&ab.value) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set sets the value of the bool regardless of the previous value
|
||||||
|
func (ab *atomicBool) Set(value bool) {
|
||||||
|
if value {
|
||||||
|
atomic.StoreUint32(&ab.value, 1)
|
||||||
|
} else {
|
||||||
|
atomic.StoreUint32(&ab.value, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TrySet sets the value of the bool and returns wether the value changed
|
||||||
|
func (ab *atomicBool) TrySet(value bool) bool {
|
||||||
|
if value {
|
||||||
|
return atomic.SwapUint32(&ab.value, 1) == 0
|
||||||
|
}
|
||||||
|
return atomic.SwapUint32(&ab.value, 0) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// atomicError is a wrapper for atomically accessed error values
|
||||||
|
type atomicError struct {
|
||||||
|
_noCopy noCopy
|
||||||
|
value atomic.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set sets the error value regardless of the previous value.
|
||||||
|
// The value must not be nil
|
||||||
|
func (ae *atomicError) Set(value error) {
|
||||||
|
ae.value.Store(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns the current error value
|
||||||
|
func (ae *atomicError) Value() error {
|
||||||
|
if v := ae.value.Load(); v != nil {
|
||||||
|
// this will panic if the value doesn't implement the error interface
|
||||||
|
return v.(error)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
40
vendor/github.com/go-sql-driver/mysql/utils_go17.go
generated
vendored
Normal file
40
vendor/github.com/go-sql-driver/mysql/utils_go17.go
generated
vendored
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
// +build go1.7
|
||||||
|
// +build !go1.8
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import "crypto/tls"
|
||||||
|
|
||||||
|
func cloneTLSConfig(c *tls.Config) *tls.Config {
|
||||||
|
return &tls.Config{
|
||||||
|
Rand: c.Rand,
|
||||||
|
Time: c.Time,
|
||||||
|
Certificates: c.Certificates,
|
||||||
|
NameToCertificate: c.NameToCertificate,
|
||||||
|
GetCertificate: c.GetCertificate,
|
||||||
|
RootCAs: c.RootCAs,
|
||||||
|
NextProtos: c.NextProtos,
|
||||||
|
ServerName: c.ServerName,
|
||||||
|
ClientAuth: c.ClientAuth,
|
||||||
|
ClientCAs: c.ClientCAs,
|
||||||
|
InsecureSkipVerify: c.InsecureSkipVerify,
|
||||||
|
CipherSuites: c.CipherSuites,
|
||||||
|
PreferServerCipherSuites: c.PreferServerCipherSuites,
|
||||||
|
SessionTicketsDisabled: c.SessionTicketsDisabled,
|
||||||
|
SessionTicketKey: c.SessionTicketKey,
|
||||||
|
ClientSessionCache: c.ClientSessionCache,
|
||||||
|
MinVersion: c.MinVersion,
|
||||||
|
MaxVersion: c.MaxVersion,
|
||||||
|
CurvePreferences: c.CurvePreferences,
|
||||||
|
DynamicRecordSizingDisabled: c.DynamicRecordSizingDisabled,
|
||||||
|
Renegotiation: c.Renegotiation,
|
||||||
|
}
|
||||||
|
}
|
50
vendor/github.com/go-sql-driver/mysql/utils_go18.go
generated
vendored
Normal file
50
vendor/github.com/go-sql-driver/mysql/utils_go18.go
generated
vendored
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
// +build go1.8
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"database/sql"
|
||||||
|
"database/sql/driver"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func cloneTLSConfig(c *tls.Config) *tls.Config {
|
||||||
|
return c.Clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
|
||||||
|
dargs := make([]driver.Value, len(named))
|
||||||
|
for n, param := range named {
|
||||||
|
if len(param.Name) > 0 {
|
||||||
|
// TODO: support the use of Named Parameters #561
|
||||||
|
return nil, errors.New("mysql: driver does not support the use of Named Parameters")
|
||||||
|
}
|
||||||
|
dargs[n] = param.Value
|
||||||
|
}
|
||||||
|
return dargs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapIsolationLevel(level driver.IsolationLevel) (string, error) {
|
||||||
|
switch sql.IsolationLevel(level) {
|
||||||
|
case sql.LevelRepeatableRead:
|
||||||
|
return "REPEATABLE READ", nil
|
||||||
|
case sql.LevelReadCommitted:
|
||||||
|
return "READ COMMITTED", nil
|
||||||
|
case sql.LevelReadUncommitted:
|
||||||
|
return "READ UNCOMMITTED", nil
|
||||||
|
case sql.LevelSerializable:
|
||||||
|
return "SERIALIZABLE", nil
|
||||||
|
default:
|
||||||
|
return "", fmt.Errorf("mysql: unsupported isolation level: %v", level)
|
||||||
|
}
|
||||||
|
}
|
24
vendor/github.com/jmoiron/sqlx/.gitignore
generated
vendored
Normal file
24
vendor/github.com/jmoiron/sqlx/.gitignore
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Folders
|
||||||
|
_obj
|
||||||
|
_test
|
||||||
|
|
||||||
|
# Architecture specific extensions/prefixes
|
||||||
|
*.[568vq]
|
||||||
|
[568vq].out
|
||||||
|
|
||||||
|
*.cgo1.go
|
||||||
|
*.cgo2.c
|
||||||
|
_cgo_defun.c
|
||||||
|
_cgo_gotypes.go
|
||||||
|
_cgo_export.*
|
||||||
|
|
||||||
|
_testmain.go
|
||||||
|
|
||||||
|
*.exe
|
||||||
|
tags
|
||||||
|
environ
|
27
vendor/github.com/jmoiron/sqlx/.travis.yml
generated
vendored
Normal file
27
vendor/github.com/jmoiron/sqlx/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# vim: ft=yaml sw=2 ts=2
|
||||||
|
|
||||||
|
language: go
|
||||||
|
|
||||||
|
# enable database services
|
||||||
|
services:
|
||||||
|
- mysql
|
||||||
|
- postgresql
|
||||||
|
|
||||||
|
# create test database
|
||||||
|
before_install:
|
||||||
|
- mysql -e 'CREATE DATABASE IF NOT EXISTS sqlxtest;'
|
||||||
|
- psql -c 'create database sqlxtest;' -U postgres
|
||||||
|
- go get github.com/mattn/goveralls
|
||||||
|
- export SQLX_MYSQL_DSN="travis:@/sqlxtest?parseTime=true"
|
||||||
|
- export SQLX_POSTGRES_DSN="postgres://postgres:@localhost/sqlxtest?sslmode=disable"
|
||||||
|
- export SQLX_SQLITE_DSN="$HOME/sqlxtest.db"
|
||||||
|
|
||||||
|
# go versions to test
|
||||||
|
go:
|
||||||
|
- "1.8"
|
||||||
|
- "1.9"
|
||||||
|
- "1.10.x"
|
||||||
|
|
||||||
|
# run tests w/ coverage
|
||||||
|
script:
|
||||||
|
- travis_retry $GOPATH/bin/goveralls -service=travis-ci
|
23
vendor/github.com/jmoiron/sqlx/LICENSE
generated
vendored
Normal file
23
vendor/github.com/jmoiron/sqlx/LICENSE
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
Copyright (c) 2013, Jason Moiron
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person
|
||||||
|
obtaining a copy of this software and associated documentation
|
||||||
|
files (the "Software"), to deal in the Software without
|
||||||
|
restriction, including without limitation the rights to use,
|
||||||
|
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the
|
||||||
|
Software is furnished to do so, subject to the following
|
||||||
|
conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
187
vendor/github.com/jmoiron/sqlx/README.md
generated
vendored
Normal file
187
vendor/github.com/jmoiron/sqlx/README.md
generated
vendored
Normal file
@ -0,0 +1,187 @@
|
|||||||
|
# sqlx
|
||||||
|
|
||||||
|
[![Build Status](https://travis-ci.org/jmoiron/sqlx.svg?branch=master)](https://travis-ci.org/jmoiron/sqlx) [![Coverage Status](https://coveralls.io/repos/github/jmoiron/sqlx/badge.svg?branch=master)](https://coveralls.io/github/jmoiron/sqlx?branch=master) [![Godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/jmoiron/sqlx) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/jmoiron/sqlx/master/LICENSE)
|
||||||
|
|
||||||
|
sqlx is a library which provides a set of extensions on go's standard
|
||||||
|
`database/sql` library. The sqlx versions of `sql.DB`, `sql.TX`, `sql.Stmt`,
|
||||||
|
et al. all leave the underlying interfaces untouched, so that their interfaces
|
||||||
|
are a superset on the standard ones. This makes it relatively painless to
|
||||||
|
integrate existing codebases using database/sql with sqlx.
|
||||||
|
|
||||||
|
Major additional concepts are:
|
||||||
|
|
||||||
|
* Marshal rows into structs (with embedded struct support), maps, and slices
|
||||||
|
* Named parameter support including prepared statements
|
||||||
|
* `Get` and `Select` to go quickly from query to struct/slice
|
||||||
|
|
||||||
|
In addition to the [godoc API documentation](http://godoc.org/github.com/jmoiron/sqlx),
|
||||||
|
there is also some [standard documentation](http://jmoiron.github.io/sqlx/) that
|
||||||
|
explains how to use `database/sql` along with sqlx.
|
||||||
|
|
||||||
|
## Recent Changes
|
||||||
|
|
||||||
|
* The [introduction](https://github.com/jmoiron/sqlx/pull/387) of `sql.ColumnType` sets the required minimum Go version to 1.8.
|
||||||
|
|
||||||
|
* sqlx/types.JsonText has been renamed to JSONText to follow Go naming conventions.
|
||||||
|
|
||||||
|
This breaks backwards compatibility, but it's in a way that is trivially fixable
|
||||||
|
(`s/JsonText/JSONText/g`). The `types` package is both experimental and not in
|
||||||
|
active development currently.
|
||||||
|
|
||||||
|
* Using Go 1.6 and below with `types.JSONText` and `types.GzippedText` can be _potentially unsafe_, **especially** when used with common auto-scan sqlx idioms like `Select` and `Get`. See [golang bug #13905](https://github.com/golang/go/issues/13905).
|
||||||
|
|
||||||
|
### Backwards Compatibility
|
||||||
|
|
||||||
|
There is no Go1-like promise of absolute stability, but I take the issue seriously
|
||||||
|
and will maintain the library in a compatible state unless vital bugs prevent me
|
||||||
|
from doing so. Since [#59](https://github.com/jmoiron/sqlx/issues/59) and
|
||||||
|
[#60](https://github.com/jmoiron/sqlx/issues/60) necessitated breaking behavior,
|
||||||
|
a wider API cleanup was done at the time of fixing. It's possible this will happen
|
||||||
|
in future; if it does, a git tag will be provided for users requiring the old
|
||||||
|
behavior to continue to use it until such a time as they can migrate.
|
||||||
|
|
||||||
|
## install
|
||||||
|
|
||||||
|
go get github.com/jmoiron/sqlx
|
||||||
|
|
||||||
|
## issues
|
||||||
|
|
||||||
|
Row headers can be ambiguous (`SELECT 1 AS a, 2 AS a`), and the result of
|
||||||
|
`Columns()` does not fully qualify column names in queries like:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT a.id, a.name, b.id, b.name FROM foos AS a JOIN foos AS b ON a.parent = b.id;
|
||||||
|
```
|
||||||
|
|
||||||
|
making a struct or map destination ambiguous. Use `AS` in your queries
|
||||||
|
to give columns distinct names, `rows.Scan` to scan them manually, or
|
||||||
|
`SliceScan` to get a slice of results.
|
||||||
|
|
||||||
|
## usage
|
||||||
|
|
||||||
|
Below is an example which shows some common use cases for sqlx. Check
|
||||||
|
[sqlx_test.go](https://github.com/jmoiron/sqlx/blob/master/sqlx_test.go) for more
|
||||||
|
usage.
|
||||||
|
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
)
|
||||||
|
|
||||||
|
var schema = `
|
||||||
|
CREATE TABLE person (
|
||||||
|
first_name text,
|
||||||
|
last_name text,
|
||||||
|
email text
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE place (
|
||||||
|
country text,
|
||||||
|
city text NULL,
|
||||||
|
telcode integer
|
||||||
|
)`
|
||||||
|
|
||||||
|
type Person struct {
|
||||||
|
FirstName string `db:"first_name"`
|
||||||
|
LastName string `db:"last_name"`
|
||||||
|
Email string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Place struct {
|
||||||
|
Country string
|
||||||
|
City sql.NullString
|
||||||
|
TelCode int
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// this Pings the database trying to connect, panics on error
|
||||||
|
// use sqlx.Open() for sql.Open() semantics
|
||||||
|
db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// exec the schema or fail; multi-statement Exec behavior varies between
|
||||||
|
// database drivers; pq will exec them all, sqlite3 won't, ymmv
|
||||||
|
db.MustExec(schema)
|
||||||
|
|
||||||
|
tx := db.MustBegin()
|
||||||
|
tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Jason", "Moiron", "jmoiron@jmoiron.net")
|
||||||
|
tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "John", "Doe", "johndoeDNE@gmail.net")
|
||||||
|
tx.MustExec("INSERT INTO place (country, city, telcode) VALUES ($1, $2, $3)", "United States", "New York", "1")
|
||||||
|
tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Hong Kong", "852")
|
||||||
|
tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Singapore", "65")
|
||||||
|
// Named queries can use structs, so if you have an existing struct (i.e. person := &Person{}) that you have populated, you can pass it in as &person
|
||||||
|
tx.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Jane", "Citizen", "jane.citzen@example.com"})
|
||||||
|
tx.Commit()
|
||||||
|
|
||||||
|
// Query the database, storing results in a []Person (wrapped in []interface{})
|
||||||
|
people := []Person{}
|
||||||
|
db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
|
||||||
|
jason, john := people[0], people[1]
|
||||||
|
|
||||||
|
fmt.Printf("%#v\n%#v", jason, john)
|
||||||
|
// Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}
|
||||||
|
// Person{FirstName:"John", LastName:"Doe", Email:"johndoeDNE@gmail.net"}
|
||||||
|
|
||||||
|
// You can also get a single result, a la QueryRow
|
||||||
|
jason = Person{}
|
||||||
|
err = db.Get(&jason, "SELECT * FROM person WHERE first_name=$1", "Jason")
|
||||||
|
fmt.Printf("%#v\n", jason)
|
||||||
|
// Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}
|
||||||
|
|
||||||
|
// if you have null fields and use SELECT *, you must use sql.Null* in your struct
|
||||||
|
places := []Place{}
|
||||||
|
err = db.Select(&places, "SELECT * FROM place ORDER BY telcode ASC")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
usa, singsing, honkers := places[0], places[1], places[2]
|
||||||
|
|
||||||
|
fmt.Printf("%#v\n%#v\n%#v\n", usa, singsing, honkers)
|
||||||
|
// Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
|
||||||
|
// Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}
|
||||||
|
// Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}
|
||||||
|
|
||||||
|
// Loop through rows using only one struct
|
||||||
|
place := Place{}
|
||||||
|
rows, err := db.Queryx("SELECT * FROM place")
|
||||||
|
for rows.Next() {
|
||||||
|
err := rows.StructScan(&place)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("%#v\n", place)
|
||||||
|
}
|
||||||
|
// Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
|
||||||
|
// Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}
|
||||||
|
// Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}
|
||||||
|
|
||||||
|
// Named queries, using `:name` as the bindvar. Automatic bindvar support
|
||||||
|
// which takes into account the dbtype based on the driverName on sqlx.Open/Connect
|
||||||
|
_, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`,
|
||||||
|
map[string]interface{}{
|
||||||
|
"first": "Bin",
|
||||||
|
"last": "Smuth",
|
||||||
|
"email": "bensmith@allblacks.nz",
|
||||||
|
})
|
||||||
|
|
||||||
|
// Selects Mr. Smith from the database
|
||||||
|
rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]interface{}{"fn": "Bin"})
|
||||||
|
|
||||||
|
// Named queries can also use structs. Their bind names follow the same rules
|
||||||
|
// as the name -> db mapping, so struct fields are lowercased and the `db` tag
|
||||||
|
// is taken into consideration.
|
||||||
|
rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
217
vendor/github.com/jmoiron/sqlx/bind.go
generated
vendored
Normal file
217
vendor/github.com/jmoiron/sqlx/bind.go
generated
vendored
Normal file
@ -0,0 +1,217 @@
|
|||||||
|
package sqlx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"database/sql/driver"
|
||||||
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jmoiron/sqlx/reflectx"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Bindvar types supported by Rebind, BindMap and BindStruct.
|
||||||
|
const (
|
||||||
|
UNKNOWN = iota
|
||||||
|
QUESTION
|
||||||
|
DOLLAR
|
||||||
|
NAMED
|
||||||
|
AT
|
||||||
|
)
|
||||||
|
|
||||||
|
// BindType returns the bindtype for a given database given a drivername.
|
||||||
|
func BindType(driverName string) int {
|
||||||
|
switch driverName {
|
||||||
|
case "postgres", "pgx", "pq-timeouts", "cloudsqlpostgres":
|
||||||
|
return DOLLAR
|
||||||
|
case "mysql":
|
||||||
|
return QUESTION
|
||||||
|
case "sqlite3":
|
||||||
|
return QUESTION
|
||||||
|
case "oci8", "ora", "goracle":
|
||||||
|
return NAMED
|
||||||
|
case "sqlserver":
|
||||||
|
return AT
|
||||||
|
}
|
||||||
|
return UNKNOWN
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: this should be able to be tolerant of escaped ?'s in queries without
|
||||||
|
// losing much speed, and should be to avoid confusion.
|
||||||
|
|
||||||
|
// Rebind a query from the default bindtype (QUESTION) to the target bindtype.
|
||||||
|
func Rebind(bindType int, query string) string {
|
||||||
|
switch bindType {
|
||||||
|
case QUESTION, UNKNOWN:
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add space enough for 10 params before we have to allocate
|
||||||
|
rqb := make([]byte, 0, len(query)+10)
|
||||||
|
|
||||||
|
var i, j int
|
||||||
|
|
||||||
|
for i = strings.Index(query, "?"); i != -1; i = strings.Index(query, "?") {
|
||||||
|
rqb = append(rqb, query[:i]...)
|
||||||
|
|
||||||
|
switch bindType {
|
||||||
|
case DOLLAR:
|
||||||
|
rqb = append(rqb, '$')
|
||||||
|
case NAMED:
|
||||||
|
rqb = append(rqb, ':', 'a', 'r', 'g')
|
||||||
|
case AT:
|
||||||
|
rqb = append(rqb, '@', 'p')
|
||||||
|
}
|
||||||
|
|
||||||
|
j++
|
||||||
|
rqb = strconv.AppendInt(rqb, int64(j), 10)
|
||||||
|
|
||||||
|
query = query[i+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(append(rqb, query...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Experimental implementation of Rebind which uses a bytes.Buffer. The code is
|
||||||
|
// much simpler and should be more resistant to odd unicode, but it is twice as
|
||||||
|
// slow. Kept here for benchmarking purposes and to possibly replace Rebind if
|
||||||
|
// problems arise with its somewhat naive handling of unicode.
|
||||||
|
func rebindBuff(bindType int, query string) string {
|
||||||
|
if bindType != DOLLAR {
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
|
||||||
|
b := make([]byte, 0, len(query))
|
||||||
|
rqb := bytes.NewBuffer(b)
|
||||||
|
j := 1
|
||||||
|
for _, r := range query {
|
||||||
|
if r == '?' {
|
||||||
|
rqb.WriteRune('$')
|
||||||
|
rqb.WriteString(strconv.Itoa(j))
|
||||||
|
j++
|
||||||
|
} else {
|
||||||
|
rqb.WriteRune(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rqb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// In expands slice values in args, returning the modified query string
|
||||||
|
// and a new arg list that can be executed by a database. The `query` should
|
||||||
|
// use the `?` bindVar. The return value uses the `?` bindVar.
|
||||||
|
func In(query string, args ...interface{}) (string, []interface{}, error) {
|
||||||
|
// argMeta stores reflect.Value and length for slices and
|
||||||
|
// the value itself for non-slice arguments
|
||||||
|
type argMeta struct {
|
||||||
|
v reflect.Value
|
||||||
|
i interface{}
|
||||||
|
length int
|
||||||
|
}
|
||||||
|
|
||||||
|
var flatArgsCount int
|
||||||
|
var anySlices bool
|
||||||
|
|
||||||
|
meta := make([]argMeta, len(args))
|
||||||
|
|
||||||
|
for i, arg := range args {
|
||||||
|
if a, ok := arg.(driver.Valuer); ok {
|
||||||
|
arg, _ = a.Value()
|
||||||
|
}
|
||||||
|
v := reflect.ValueOf(arg)
|
||||||
|
t := reflectx.Deref(v.Type())
|
||||||
|
|
||||||
|
// []byte is a driver.Value type so it should not be expanded
|
||||||
|
if t.Kind() == reflect.Slice && t != reflect.TypeOf([]byte{}) {
|
||||||
|
meta[i].length = v.Len()
|
||||||
|
meta[i].v = v
|
||||||
|
|
||||||
|
anySlices = true
|
||||||
|
flatArgsCount += meta[i].length
|
||||||
|
|
||||||
|
if meta[i].length == 0 {
|
||||||
|
return "", nil, errors.New("empty slice passed to 'in' query")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
meta[i].i = arg
|
||||||
|
flatArgsCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// don't do any parsing if there aren't any slices; note that this means
|
||||||
|
// some errors that we might have caught below will not be returned.
|
||||||
|
if !anySlices {
|
||||||
|
return query, args, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
newArgs := make([]interface{}, 0, flatArgsCount)
|
||||||
|
buf := make([]byte, 0, len(query)+len(", ?")*flatArgsCount)
|
||||||
|
|
||||||
|
var arg, offset int
|
||||||
|
|
||||||
|
for i := strings.IndexByte(query[offset:], '?'); i != -1; i = strings.IndexByte(query[offset:], '?') {
|
||||||
|
if arg >= len(meta) {
|
||||||
|
// if an argument wasn't passed, lets return an error; this is
|
||||||
|
// not actually how database/sql Exec/Query works, but since we are
|
||||||
|
// creating an argument list programmatically, we want to be able
|
||||||
|
// to catch these programmer errors earlier.
|
||||||
|
return "", nil, errors.New("number of bindVars exceeds arguments")
|
||||||
|
}
|
||||||
|
|
||||||
|
argMeta := meta[arg]
|
||||||
|
arg++
|
||||||
|
|
||||||
|
// not a slice, continue.
|
||||||
|
// our questionmark will either be written before the next expansion
|
||||||
|
// of a slice or after the loop when writing the rest of the query
|
||||||
|
if argMeta.length == 0 {
|
||||||
|
offset = offset + i + 1
|
||||||
|
newArgs = append(newArgs, argMeta.i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// write everything up to and including our ? character
|
||||||
|
buf = append(buf, query[:offset+i+1]...)
|
||||||
|
|
||||||
|
for si := 1; si < argMeta.length; si++ {
|
||||||
|
buf = append(buf, ", ?"...)
|
||||||
|
}
|
||||||
|
|
||||||
|
newArgs = appendReflectSlice(newArgs, argMeta.v, argMeta.length)
|
||||||
|
|
||||||
|
// slice the query and reset the offset. this avoids some bookkeeping for
|
||||||
|
// the write after the loop
|
||||||
|
query = query[offset+i+1:]
|
||||||
|
offset = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
buf = append(buf, query...)
|
||||||
|
|
||||||
|
if arg < len(meta) {
|
||||||
|
return "", nil, errors.New("number of bindVars less than number arguments")
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(buf), newArgs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interface{} {
|
||||||
|
switch val := v.Interface().(type) {
|
||||||
|
case []interface{}:
|
||||||
|
args = append(args, val...)
|
||||||
|
case []int:
|
||||||
|
for i := range val {
|
||||||
|
args = append(args, val[i])
|
||||||
|
}
|
||||||
|
case []string:
|
||||||
|
for i := range val {
|
||||||
|
args = append(args, val[i])
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
for si := 0; si < vlen; si++ {
|
||||||
|
args = append(args, v.Index(si).Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return args
|
||||||
|
}
|
12
vendor/github.com/jmoiron/sqlx/doc.go
generated
vendored
Normal file
12
vendor/github.com/jmoiron/sqlx/doc.go
generated
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// Package sqlx provides general purpose extensions to database/sql.
|
||||||
|
//
|
||||||
|
// It is intended to seamlessly wrap database/sql and provide convenience
|
||||||
|
// methods which are useful in the development of database driven applications.
|
||||||
|
// None of the underlying database/sql methods are changed. Instead all extended
|
||||||
|
// behavior is implemented through new methods defined on wrapper types.
|
||||||
|
//
|
||||||
|
// Additions include scanning into structs, named query support, rebinding
|
||||||
|
// queries for different drivers, convenient shorthands for common error handling
|
||||||
|
// and more.
|
||||||
|
//
|
||||||
|
package sqlx
|
7
vendor/github.com/jmoiron/sqlx/go.mod
generated
vendored
Normal file
7
vendor/github.com/jmoiron/sqlx/go.mod
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
module github.com/jmoiron/sqlx
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/go-sql-driver/mysql v1.4.0
|
||||||
|
github.com/lib/pq v1.0.0
|
||||||
|
github.com/mattn/go-sqlite3 v1.9.0
|
||||||
|
)
|
6
vendor/github.com/jmoiron/sqlx/go.sum
generated
vendored
Normal file
6
vendor/github.com/jmoiron/sqlx/go.sum
generated
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
|
||||||
|
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||||
|
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
|
||||||
|
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||||
|
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
|
||||||
|
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
356
vendor/github.com/jmoiron/sqlx/named.go
generated
vendored
Normal file
356
vendor/github.com/jmoiron/sqlx/named.go
generated
vendored
Normal file
@ -0,0 +1,356 @@
|
|||||||
|
package sqlx
|
||||||
|
|
||||||
|
// Named Query Support
|
||||||
|
//
|
||||||
|
// * BindMap - bind query bindvars to map/struct args
|
||||||
|
// * NamedExec, NamedQuery - named query w/ struct or map
|
||||||
|
// * NamedStmt - a pre-compiled named query which is a prepared statement
|
||||||
|
//
|
||||||
|
// Internal Interfaces:
|
||||||
|
//
|
||||||
|
// * compileNamedQuery - rebind a named query, returning a query and list of names
|
||||||
|
// * bindArgs, bindMapArgs, bindAnyArgs - given a list of names, return an arglist
|
||||||
|
//
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
|
"github.com/jmoiron/sqlx/reflectx"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NamedStmt is a prepared statement that executes named queries. Prepare it
|
||||||
|
// how you would execute a NamedQuery, but pass in a struct or map when executing.
|
||||||
|
type NamedStmt struct {
|
||||||
|
Params []string
|
||||||
|
QueryString string
|
||||||
|
Stmt *Stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes the named statement.
|
||||||
|
func (n *NamedStmt) Close() error {
|
||||||
|
return n.Stmt.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exec executes a named statement using the struct passed.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) Exec(arg interface{}) (sql.Result, error) {
|
||||||
|
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
|
||||||
|
if err != nil {
|
||||||
|
return *new(sql.Result), err
|
||||||
|
}
|
||||||
|
return n.Stmt.Exec(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query executes a named statement using the struct argument, returning rows.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) Query(arg interface{}) (*sql.Rows, error) {
|
||||||
|
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return n.Stmt.Query(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRow executes a named statement against the database. Because sqlx cannot
|
||||||
|
// create a *sql.Row with an error condition pre-set for binding errors, sqlx
|
||||||
|
// returns a *sqlx.Row instead.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) QueryRow(arg interface{}) *Row {
|
||||||
|
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
|
||||||
|
if err != nil {
|
||||||
|
return &Row{err: err}
|
||||||
|
}
|
||||||
|
return n.Stmt.QueryRowx(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustExec execs a NamedStmt, panicing on error
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) MustExec(arg interface{}) sql.Result {
|
||||||
|
res, err := n.Exec(arg)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// Queryx using this NamedStmt
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) Queryx(arg interface{}) (*Rows, error) {
|
||||||
|
r, err := n.Query(arg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Rows{Rows: r, Mapper: n.Stmt.Mapper, unsafe: isUnsafe(n)}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRowx this NamedStmt. Because of limitations with QueryRow, this is
|
||||||
|
// an alias for QueryRow.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) QueryRowx(arg interface{}) *Row {
|
||||||
|
return n.QueryRow(arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select using this NamedStmt
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) Select(dest interface{}, arg interface{}) error {
|
||||||
|
rows, err := n.Queryx(arg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// if something happens here, we want to make sure the rows are Closed
|
||||||
|
defer rows.Close()
|
||||||
|
return scanAll(rows, dest, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get using this NamedStmt
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) Get(dest interface{}, arg interface{}) error {
|
||||||
|
r := n.QueryRowx(arg)
|
||||||
|
return r.scanAny(dest, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsafe creates an unsafe version of the NamedStmt
|
||||||
|
func (n *NamedStmt) Unsafe() *NamedStmt {
|
||||||
|
r := &NamedStmt{Params: n.Params, Stmt: n.Stmt, QueryString: n.QueryString}
|
||||||
|
r.Stmt.unsafe = true
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// A union interface of preparer and binder, required to be able to prepare
|
||||||
|
// named statements (as the bindtype must be determined).
|
||||||
|
type namedPreparer interface {
|
||||||
|
Preparer
|
||||||
|
binder
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareNamed(p namedPreparer, query string) (*NamedStmt, error) {
|
||||||
|
bindType := BindType(p.DriverName())
|
||||||
|
q, args, err := compileNamedQuery([]byte(query), bindType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
stmt, err := Preparex(p, q)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &NamedStmt{
|
||||||
|
QueryString: q,
|
||||||
|
Params: args,
|
||||||
|
Stmt: stmt,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func bindAnyArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{}, error) {
|
||||||
|
if maparg, ok := arg.(map[string]interface{}); ok {
|
||||||
|
return bindMapArgs(names, maparg)
|
||||||
|
}
|
||||||
|
return bindArgs(names, arg, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// private interface to generate a list of interfaces from a given struct
|
||||||
|
// type, given a list of names to pull out of the struct. Used by public
|
||||||
|
// BindStruct interface.
|
||||||
|
func bindArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{}, error) {
|
||||||
|
arglist := make([]interface{}, 0, len(names))
|
||||||
|
|
||||||
|
// grab the indirected value of arg
|
||||||
|
v := reflect.ValueOf(arg)
|
||||||
|
for v = reflect.ValueOf(arg); v.Kind() == reflect.Ptr; {
|
||||||
|
v = v.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
err := m.TraversalsByNameFunc(v.Type(), names, func(i int, t []int) error {
|
||||||
|
if len(t) == 0 {
|
||||||
|
return fmt.Errorf("could not find name %s in %#v", names[i], arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
val := reflectx.FieldByIndexesReadOnly(v, t)
|
||||||
|
arglist = append(arglist, val.Interface())
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return arglist, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// like bindArgs, but for maps.
|
||||||
|
func bindMapArgs(names []string, arg map[string]interface{}) ([]interface{}, error) {
|
||||||
|
arglist := make([]interface{}, 0, len(names))
|
||||||
|
|
||||||
|
for _, name := range names {
|
||||||
|
val, ok := arg[name]
|
||||||
|
if !ok {
|
||||||
|
return arglist, fmt.Errorf("could not find name %s in %#v", name, arg)
|
||||||
|
}
|
||||||
|
arglist = append(arglist, val)
|
||||||
|
}
|
||||||
|
return arglist, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// bindStruct binds a named parameter query with fields from a struct argument.
|
||||||
|
// The rules for binding field names to parameter names follow the same
|
||||||
|
// conventions as for StructScan, including obeying the `db` struct tags.
|
||||||
|
func bindStruct(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
|
||||||
|
bound, names, err := compileNamedQuery([]byte(query), bindType)
|
||||||
|
if err != nil {
|
||||||
|
return "", []interface{}{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
arglist, err := bindArgs(names, arg, m)
|
||||||
|
if err != nil {
|
||||||
|
return "", []interface{}{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return bound, arglist, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// bindMap binds a named parameter query with a map of arguments.
|
||||||
|
func bindMap(bindType int, query string, args map[string]interface{}) (string, []interface{}, error) {
|
||||||
|
bound, names, err := compileNamedQuery([]byte(query), bindType)
|
||||||
|
if err != nil {
|
||||||
|
return "", []interface{}{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
arglist, err := bindMapArgs(names, args)
|
||||||
|
return bound, arglist, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- Compilation of Named Queries
|
||||||
|
|
||||||
|
// Allow digits and letters in bind params; additionally runes are
|
||||||
|
// checked against underscores, meaning that bind params can have be
|
||||||
|
// alphanumeric with underscores. Mind the difference between unicode
|
||||||
|
// digits and numbers, where '5' is a digit but '五' is not.
|
||||||
|
var allowedBindRunes = []*unicode.RangeTable{unicode.Letter, unicode.Digit}
|
||||||
|
|
||||||
|
// FIXME: this function isn't safe for unicode named params, as a failing test
|
||||||
|
// can testify. This is not a regression but a failure of the original code
|
||||||
|
// as well. It should be modified to range over runes in a string rather than
|
||||||
|
// bytes, even though this is less convenient and slower. Hopefully the
|
||||||
|
// addition of the prepared NamedStmt (which will only do this once) will make
|
||||||
|
// up for the slightly slower ad-hoc NamedExec/NamedQuery.
|
||||||
|
|
||||||
|
// compile a NamedQuery into an unbound query (using the '?' bindvar) and
|
||||||
|
// a list of names.
|
||||||
|
func compileNamedQuery(qs []byte, bindType int) (query string, names []string, err error) {
|
||||||
|
names = make([]string, 0, 10)
|
||||||
|
rebound := make([]byte, 0, len(qs))
|
||||||
|
|
||||||
|
inName := false
|
||||||
|
last := len(qs) - 1
|
||||||
|
currentVar := 1
|
||||||
|
name := make([]byte, 0, 10)
|
||||||
|
|
||||||
|
for i, b := range qs {
|
||||||
|
// a ':' while we're in a name is an error
|
||||||
|
if b == ':' {
|
||||||
|
// if this is the second ':' in a '::' escape sequence, append a ':'
|
||||||
|
if inName && i > 0 && qs[i-1] == ':' {
|
||||||
|
rebound = append(rebound, ':')
|
||||||
|
inName = false
|
||||||
|
continue
|
||||||
|
} else if inName {
|
||||||
|
err = errors.New("unexpected `:` while reading named param at " + strconv.Itoa(i))
|
||||||
|
return query, names, err
|
||||||
|
}
|
||||||
|
inName = true
|
||||||
|
name = []byte{}
|
||||||
|
} else if inName && i > 0 && b == '=' {
|
||||||
|
rebound = append(rebound, ':', '=')
|
||||||
|
inName = false
|
||||||
|
continue
|
||||||
|
// if we're in a name, and this is an allowed character, continue
|
||||||
|
} else if inName && (unicode.IsOneOf(allowedBindRunes, rune(b)) || b == '_' || b == '.') && i != last {
|
||||||
|
// append the byte to the name if we are in a name and not on the last byte
|
||||||
|
name = append(name, b)
|
||||||
|
// if we're in a name and it's not an allowed character, the name is done
|
||||||
|
} else if inName {
|
||||||
|
inName = false
|
||||||
|
// if this is the final byte of the string and it is part of the name, then
|
||||||
|
// make sure to add it to the name
|
||||||
|
if i == last && unicode.IsOneOf(allowedBindRunes, rune(b)) {
|
||||||
|
name = append(name, b)
|
||||||
|
}
|
||||||
|
// add the string representation to the names list
|
||||||
|
names = append(names, string(name))
|
||||||
|
// add a proper bindvar for the bindType
|
||||||
|
switch bindType {
|
||||||
|
// oracle only supports named type bind vars even for positional
|
||||||
|
case NAMED:
|
||||||
|
rebound = append(rebound, ':')
|
||||||
|
rebound = append(rebound, name...)
|
||||||
|
case QUESTION, UNKNOWN:
|
||||||
|
rebound = append(rebound, '?')
|
||||||
|
case DOLLAR:
|
||||||
|
rebound = append(rebound, '$')
|
||||||
|
for _, b := range strconv.Itoa(currentVar) {
|
||||||
|
rebound = append(rebound, byte(b))
|
||||||
|
}
|
||||||
|
currentVar++
|
||||||
|
case AT:
|
||||||
|
rebound = append(rebound, '@', 'p')
|
||||||
|
for _, b := range strconv.Itoa(currentVar) {
|
||||||
|
rebound = append(rebound, byte(b))
|
||||||
|
}
|
||||||
|
currentVar++
|
||||||
|
}
|
||||||
|
// add this byte to string unless it was not part of the name
|
||||||
|
if i != last {
|
||||||
|
rebound = append(rebound, b)
|
||||||
|
} else if !unicode.IsOneOf(allowedBindRunes, rune(b)) {
|
||||||
|
rebound = append(rebound, b)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// this is a normal byte and should just go onto the rebound query
|
||||||
|
rebound = append(rebound, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(rebound), names, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// BindNamed binds a struct or a map to a query with named parameters.
|
||||||
|
// DEPRECATED: use sqlx.Named` instead of this, it may be removed in future.
|
||||||
|
func BindNamed(bindType int, query string, arg interface{}) (string, []interface{}, error) {
|
||||||
|
return bindNamedMapper(bindType, query, arg, mapper())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Named takes a query using named parameters and an argument and
|
||||||
|
// returns a new query with a list of args that can be executed by
|
||||||
|
// a database. The return value uses the `?` bindvar.
|
||||||
|
func Named(query string, arg interface{}) (string, []interface{}, error) {
|
||||||
|
return bindNamedMapper(QUESTION, query, arg, mapper())
|
||||||
|
}
|
||||||
|
|
||||||
|
func bindNamedMapper(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
|
||||||
|
if maparg, ok := arg.(map[string]interface{}); ok {
|
||||||
|
return bindMap(bindType, query, maparg)
|
||||||
|
}
|
||||||
|
return bindStruct(bindType, query, arg, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NamedQuery binds a named query and then runs Query on the result using the
|
||||||
|
// provided Ext (sqlx.Tx, sqlx.Db). It works with both structs and with
|
||||||
|
// map[string]interface{} types.
|
||||||
|
func NamedQuery(e Ext, query string, arg interface{}) (*Rows, error) {
|
||||||
|
q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return e.Queryx(q, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NamedExec uses BindStruct to get a query executable by the driver and
|
||||||
|
// then runs Exec on the result. Returns an error from the binding
|
||||||
|
// or the query excution itself.
|
||||||
|
func NamedExec(e Ext, query string, arg interface{}) (sql.Result, error) {
|
||||||
|
q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return e.Exec(q, args...)
|
||||||
|
}
|
132
vendor/github.com/jmoiron/sqlx/named_context.go
generated
vendored
Normal file
132
vendor/github.com/jmoiron/sqlx/named_context.go
generated
vendored
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
// +build go1.8
|
||||||
|
|
||||||
|
package sqlx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A union interface of contextPreparer and binder, required to be able to
|
||||||
|
// prepare named statements with context (as the bindtype must be determined).
|
||||||
|
type namedPreparerContext interface {
|
||||||
|
PreparerContext
|
||||||
|
binder
|
||||||
|
}
|
||||||
|
|
||||||
|
func prepareNamedContext(ctx context.Context, p namedPreparerContext, query string) (*NamedStmt, error) {
|
||||||
|
bindType := BindType(p.DriverName())
|
||||||
|
q, args, err := compileNamedQuery([]byte(query), bindType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
stmt, err := PreparexContext(ctx, p, q)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &NamedStmt{
|
||||||
|
QueryString: q,
|
||||||
|
Params: args,
|
||||||
|
Stmt: stmt,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExecContext executes a named statement using the struct passed.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) ExecContext(ctx context.Context, arg interface{}) (sql.Result, error) {
|
||||||
|
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
|
||||||
|
if err != nil {
|
||||||
|
return *new(sql.Result), err
|
||||||
|
}
|
||||||
|
return n.Stmt.ExecContext(ctx, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryContext executes a named statement using the struct argument, returning rows.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) QueryContext(ctx context.Context, arg interface{}) (*sql.Rows, error) {
|
||||||
|
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return n.Stmt.QueryContext(ctx, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRowContext executes a named statement against the database. Because sqlx cannot
|
||||||
|
// create a *sql.Row with an error condition pre-set for binding errors, sqlx
|
||||||
|
// returns a *sqlx.Row instead.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) QueryRowContext(ctx context.Context, arg interface{}) *Row {
|
||||||
|
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
|
||||||
|
if err != nil {
|
||||||
|
return &Row{err: err}
|
||||||
|
}
|
||||||
|
return n.Stmt.QueryRowxContext(ctx, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustExecContext execs a NamedStmt, panicing on error
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) MustExecContext(ctx context.Context, arg interface{}) sql.Result {
|
||||||
|
res, err := n.ExecContext(ctx, arg)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryxContext using this NamedStmt
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) QueryxContext(ctx context.Context, arg interface{}) (*Rows, error) {
|
||||||
|
r, err := n.QueryContext(ctx, arg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Rows{Rows: r, Mapper: n.Stmt.Mapper, unsafe: isUnsafe(n)}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRowxContext this NamedStmt. Because of limitations with QueryRow, this is
|
||||||
|
// an alias for QueryRow.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) QueryRowxContext(ctx context.Context, arg interface{}) *Row {
|
||||||
|
return n.QueryRowContext(ctx, arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectContext using this NamedStmt
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) SelectContext(ctx context.Context, dest interface{}, arg interface{}) error {
|
||||||
|
rows, err := n.QueryxContext(ctx, arg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// if something happens here, we want to make sure the rows are Closed
|
||||||
|
defer rows.Close()
|
||||||
|
return scanAll(rows, dest, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContext using this NamedStmt
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (n *NamedStmt) GetContext(ctx context.Context, dest interface{}, arg interface{}) error {
|
||||||
|
r := n.QueryRowxContext(ctx, arg)
|
||||||
|
return r.scanAny(dest, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NamedQueryContext binds a named query and then runs Query on the result using the
|
||||||
|
// provided Ext (sqlx.Tx, sqlx.Db). It works with both structs and with
|
||||||
|
// map[string]interface{} types.
|
||||||
|
func NamedQueryContext(ctx context.Context, e ExtContext, query string, arg interface{}) (*Rows, error) {
|
||||||
|
q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return e.QueryxContext(ctx, q, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NamedExecContext uses BindStruct to get a query executable by the driver and
|
||||||
|
// then runs Exec on the result. Returns an error from the binding
|
||||||
|
// or the query excution itself.
|
||||||
|
func NamedExecContext(ctx context.Context, e ExtContext, query string, arg interface{}) (sql.Result, error) {
|
||||||
|
q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return e.ExecContext(ctx, q, args...)
|
||||||
|
}
|
17
vendor/github.com/jmoiron/sqlx/reflectx/README.md
generated
vendored
Normal file
17
vendor/github.com/jmoiron/sqlx/reflectx/README.md
generated
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# reflectx
|
||||||
|
|
||||||
|
The sqlx package has special reflect needs. In particular, it needs to:
|
||||||
|
|
||||||
|
* be able to map a name to a field
|
||||||
|
* understand embedded structs
|
||||||
|
* understand mapping names to fields by a particular tag
|
||||||
|
* user specified name -> field mapping functions
|
||||||
|
|
||||||
|
These behaviors mimic the behaviors by the standard library marshallers and also the
|
||||||
|
behavior of standard Go accessors.
|
||||||
|
|
||||||
|
The first two are amply taken care of by `Reflect.Value.FieldByName`, and the third is
|
||||||
|
addressed by `Reflect.Value.FieldByNameFunc`, but these don't quite understand struct
|
||||||
|
tags in the ways that are vital to most marshallers, and they are slow.
|
||||||
|
|
||||||
|
This reflectx package extends reflect to achieve these goals.
|
441
vendor/github.com/jmoiron/sqlx/reflectx/reflect.go
generated
vendored
Normal file
441
vendor/github.com/jmoiron/sqlx/reflectx/reflect.go
generated
vendored
Normal file
@ -0,0 +1,441 @@
|
|||||||
|
// Package reflectx implements extensions to the standard reflect lib suitable
|
||||||
|
// for implementing marshalling and unmarshalling packages. The main Mapper type
|
||||||
|
// allows for Go-compatible named attribute access, including accessing embedded
|
||||||
|
// struct attributes and the ability to use functions and struct tags to
|
||||||
|
// customize field names.
|
||||||
|
//
|
||||||
|
package reflectx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A FieldInfo is metadata for a struct field.
|
||||||
|
type FieldInfo struct {
|
||||||
|
Index []int
|
||||||
|
Path string
|
||||||
|
Field reflect.StructField
|
||||||
|
Zero reflect.Value
|
||||||
|
Name string
|
||||||
|
Options map[string]string
|
||||||
|
Embedded bool
|
||||||
|
Children []*FieldInfo
|
||||||
|
Parent *FieldInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
// A StructMap is an index of field metadata for a struct.
|
||||||
|
type StructMap struct {
|
||||||
|
Tree *FieldInfo
|
||||||
|
Index []*FieldInfo
|
||||||
|
Paths map[string]*FieldInfo
|
||||||
|
Names map[string]*FieldInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetByPath returns a *FieldInfo for a given string path.
|
||||||
|
func (f StructMap) GetByPath(path string) *FieldInfo {
|
||||||
|
return f.Paths[path]
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetByTraversal returns a *FieldInfo for a given integer path. It is
|
||||||
|
// analogous to reflect.FieldByIndex, but using the cached traversal
|
||||||
|
// rather than re-executing the reflect machinery each time.
|
||||||
|
func (f StructMap) GetByTraversal(index []int) *FieldInfo {
|
||||||
|
if len(index) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tree := f.Tree
|
||||||
|
for _, i := range index {
|
||||||
|
if i >= len(tree.Children) || tree.Children[i] == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
tree = tree.Children[i]
|
||||||
|
}
|
||||||
|
return tree
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mapper is a general purpose mapper of names to struct fields. A Mapper
|
||||||
|
// behaves like most marshallers in the standard library, obeying a field tag
|
||||||
|
// for name mapping but also providing a basic transform function.
|
||||||
|
type Mapper struct {
|
||||||
|
cache map[reflect.Type]*StructMap
|
||||||
|
tagName string
|
||||||
|
tagMapFunc func(string) string
|
||||||
|
mapFunc func(string) string
|
||||||
|
mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMapper returns a new mapper using the tagName as its struct field tag.
|
||||||
|
// If tagName is the empty string, it is ignored.
|
||||||
|
func NewMapper(tagName string) *Mapper {
|
||||||
|
return &Mapper{
|
||||||
|
cache: make(map[reflect.Type]*StructMap),
|
||||||
|
tagName: tagName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMapperTagFunc returns a new mapper which contains a mapper for field names
|
||||||
|
// AND a mapper for tag values. This is useful for tags like json which can
|
||||||
|
// have values like "name,omitempty".
|
||||||
|
func NewMapperTagFunc(tagName string, mapFunc, tagMapFunc func(string) string) *Mapper {
|
||||||
|
return &Mapper{
|
||||||
|
cache: make(map[reflect.Type]*StructMap),
|
||||||
|
tagName: tagName,
|
||||||
|
mapFunc: mapFunc,
|
||||||
|
tagMapFunc: tagMapFunc,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMapperFunc returns a new mapper which optionally obeys a field tag and
|
||||||
|
// a struct field name mapper func given by f. Tags will take precedence, but
|
||||||
|
// for any other field, the mapped name will be f(field.Name)
|
||||||
|
func NewMapperFunc(tagName string, f func(string) string) *Mapper {
|
||||||
|
return &Mapper{
|
||||||
|
cache: make(map[reflect.Type]*StructMap),
|
||||||
|
tagName: tagName,
|
||||||
|
mapFunc: f,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TypeMap returns a mapping of field strings to int slices representing
|
||||||
|
// the traversal down the struct to reach the field.
|
||||||
|
func (m *Mapper) TypeMap(t reflect.Type) *StructMap {
|
||||||
|
m.mutex.Lock()
|
||||||
|
mapping, ok := m.cache[t]
|
||||||
|
if !ok {
|
||||||
|
mapping = getMapping(t, m.tagName, m.mapFunc, m.tagMapFunc)
|
||||||
|
m.cache[t] = mapping
|
||||||
|
}
|
||||||
|
m.mutex.Unlock()
|
||||||
|
return mapping
|
||||||
|
}
|
||||||
|
|
||||||
|
// FieldMap returns the mapper's mapping of field names to reflect values. Panics
|
||||||
|
// if v's Kind is not Struct, or v is not Indirectable to a struct kind.
|
||||||
|
func (m *Mapper) FieldMap(v reflect.Value) map[string]reflect.Value {
|
||||||
|
v = reflect.Indirect(v)
|
||||||
|
mustBe(v, reflect.Struct)
|
||||||
|
|
||||||
|
r := map[string]reflect.Value{}
|
||||||
|
tm := m.TypeMap(v.Type())
|
||||||
|
for tagName, fi := range tm.Names {
|
||||||
|
r[tagName] = FieldByIndexes(v, fi.Index)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// FieldByName returns a field by its mapped name as a reflect.Value.
|
||||||
|
// Panics if v's Kind is not Struct or v is not Indirectable to a struct Kind.
|
||||||
|
// Returns zero Value if the name is not found.
|
||||||
|
func (m *Mapper) FieldByName(v reflect.Value, name string) reflect.Value {
|
||||||
|
v = reflect.Indirect(v)
|
||||||
|
mustBe(v, reflect.Struct)
|
||||||
|
|
||||||
|
tm := m.TypeMap(v.Type())
|
||||||
|
fi, ok := tm.Names[name]
|
||||||
|
if !ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return FieldByIndexes(v, fi.Index)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FieldsByName returns a slice of values corresponding to the slice of names
|
||||||
|
// for the value. Panics if v's Kind is not Struct or v is not Indirectable
|
||||||
|
// to a struct Kind. Returns zero Value for each name not found.
|
||||||
|
func (m *Mapper) FieldsByName(v reflect.Value, names []string) []reflect.Value {
|
||||||
|
v = reflect.Indirect(v)
|
||||||
|
mustBe(v, reflect.Struct)
|
||||||
|
|
||||||
|
tm := m.TypeMap(v.Type())
|
||||||
|
vals := make([]reflect.Value, 0, len(names))
|
||||||
|
for _, name := range names {
|
||||||
|
fi, ok := tm.Names[name]
|
||||||
|
if !ok {
|
||||||
|
vals = append(vals, *new(reflect.Value))
|
||||||
|
} else {
|
||||||
|
vals = append(vals, FieldByIndexes(v, fi.Index))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
}
|
||||||
|
|
||||||
|
// TraversalsByName returns a slice of int slices which represent the struct
|
||||||
|
// traversals for each mapped name. Panics if t is not a struct or Indirectable
|
||||||
|
// to a struct. Returns empty int slice for each name not found.
|
||||||
|
func (m *Mapper) TraversalsByName(t reflect.Type, names []string) [][]int {
|
||||||
|
r := make([][]int, 0, len(names))
|
||||||
|
m.TraversalsByNameFunc(t, names, func(_ int, i []int) error {
|
||||||
|
if i == nil {
|
||||||
|
r = append(r, []int{})
|
||||||
|
} else {
|
||||||
|
r = append(r, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// TraversalsByNameFunc traverses the mapped names and calls fn with the index of
|
||||||
|
// each name and the struct traversal represented by that name. Panics if t is not
|
||||||
|
// a struct or Indirectable to a struct. Returns the first error returned by fn or nil.
|
||||||
|
func (m *Mapper) TraversalsByNameFunc(t reflect.Type, names []string, fn func(int, []int) error) error {
|
||||||
|
t = Deref(t)
|
||||||
|
mustBe(t, reflect.Struct)
|
||||||
|
tm := m.TypeMap(t)
|
||||||
|
for i, name := range names {
|
||||||
|
fi, ok := tm.Names[name]
|
||||||
|
if !ok {
|
||||||
|
if err := fn(i, nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := fn(i, fi.Index); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FieldByIndexes returns a value for the field given by the struct traversal
|
||||||
|
// for the given value.
|
||||||
|
func FieldByIndexes(v reflect.Value, indexes []int) reflect.Value {
|
||||||
|
for _, i := range indexes {
|
||||||
|
v = reflect.Indirect(v).Field(i)
|
||||||
|
// if this is a pointer and it's nil, allocate a new value and set it
|
||||||
|
if v.Kind() == reflect.Ptr && v.IsNil() {
|
||||||
|
alloc := reflect.New(Deref(v.Type()))
|
||||||
|
v.Set(alloc)
|
||||||
|
}
|
||||||
|
if v.Kind() == reflect.Map && v.IsNil() {
|
||||||
|
v.Set(reflect.MakeMap(v.Type()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// FieldByIndexesReadOnly returns a value for a particular struct traversal,
|
||||||
|
// but is not concerned with allocating nil pointers because the value is
|
||||||
|
// going to be used for reading and not setting.
|
||||||
|
func FieldByIndexesReadOnly(v reflect.Value, indexes []int) reflect.Value {
|
||||||
|
for _, i := range indexes {
|
||||||
|
v = reflect.Indirect(v).Field(i)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deref is Indirect for reflect.Types
|
||||||
|
func Deref(t reflect.Type) reflect.Type {
|
||||||
|
if t.Kind() == reflect.Ptr {
|
||||||
|
t = t.Elem()
|
||||||
|
}
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- helpers & utilities --
|
||||||
|
|
||||||
|
type kinder interface {
|
||||||
|
Kind() reflect.Kind
|
||||||
|
}
|
||||||
|
|
||||||
|
// mustBe checks a value against a kind, panicing with a reflect.ValueError
|
||||||
|
// if the kind isn't that which is required.
|
||||||
|
func mustBe(v kinder, expected reflect.Kind) {
|
||||||
|
if k := v.Kind(); k != expected {
|
||||||
|
panic(&reflect.ValueError{Method: methodName(), Kind: k})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// methodName returns the caller of the function calling methodName
|
||||||
|
func methodName() string {
|
||||||
|
pc, _, _, _ := runtime.Caller(2)
|
||||||
|
f := runtime.FuncForPC(pc)
|
||||||
|
if f == nil {
|
||||||
|
return "unknown method"
|
||||||
|
}
|
||||||
|
return f.Name()
|
||||||
|
}
|
||||||
|
|
||||||
|
type typeQueue struct {
|
||||||
|
t reflect.Type
|
||||||
|
fi *FieldInfo
|
||||||
|
pp string // Parent path
|
||||||
|
}
|
||||||
|
|
||||||
|
// A copying append that creates a new slice each time.
|
||||||
|
func apnd(is []int, i int) []int {
|
||||||
|
x := make([]int, len(is)+1)
|
||||||
|
for p, n := range is {
|
||||||
|
x[p] = n
|
||||||
|
}
|
||||||
|
x[len(x)-1] = i
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
type mapf func(string) string
|
||||||
|
|
||||||
|
// parseName parses the tag and the target name for the given field using
|
||||||
|
// the tagName (eg 'json' for `json:"foo"` tags), mapFunc for mapping the
|
||||||
|
// field's name to a target name, and tagMapFunc for mapping the tag to
|
||||||
|
// a target name.
|
||||||
|
func parseName(field reflect.StructField, tagName string, mapFunc, tagMapFunc mapf) (tag, fieldName string) {
|
||||||
|
// first, set the fieldName to the field's name
|
||||||
|
fieldName = field.Name
|
||||||
|
// if a mapFunc is set, use that to override the fieldName
|
||||||
|
if mapFunc != nil {
|
||||||
|
fieldName = mapFunc(fieldName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if there's no tag to look for, return the field name
|
||||||
|
if tagName == "" {
|
||||||
|
return "", fieldName
|
||||||
|
}
|
||||||
|
|
||||||
|
// if this tag is not set using the normal convention in the tag,
|
||||||
|
// then return the fieldname.. this check is done because according
|
||||||
|
// to the reflect documentation:
|
||||||
|
// If the tag does not have the conventional format,
|
||||||
|
// the value returned by Get is unspecified.
|
||||||
|
// which doesn't sound great.
|
||||||
|
if !strings.Contains(string(field.Tag), tagName+":") {
|
||||||
|
return "", fieldName
|
||||||
|
}
|
||||||
|
|
||||||
|
// at this point we're fairly sure that we have a tag, so lets pull it out
|
||||||
|
tag = field.Tag.Get(tagName)
|
||||||
|
|
||||||
|
// if we have a mapper function, call it on the whole tag
|
||||||
|
// XXX: this is a change from the old version, which pulled out the name
|
||||||
|
// before the tagMapFunc could be run, but I think this is the right way
|
||||||
|
if tagMapFunc != nil {
|
||||||
|
tag = tagMapFunc(tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
// finally, split the options from the name
|
||||||
|
parts := strings.Split(tag, ",")
|
||||||
|
fieldName = parts[0]
|
||||||
|
|
||||||
|
return tag, fieldName
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseOptions parses options out of a tag string, skipping the name
|
||||||
|
func parseOptions(tag string) map[string]string {
|
||||||
|
parts := strings.Split(tag, ",")
|
||||||
|
options := make(map[string]string, len(parts))
|
||||||
|
if len(parts) > 1 {
|
||||||
|
for _, opt := range parts[1:] {
|
||||||
|
// short circuit potentially expensive split op
|
||||||
|
if strings.Contains(opt, "=") {
|
||||||
|
kv := strings.Split(opt, "=")
|
||||||
|
options[kv[0]] = kv[1]
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
options[opt] = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
// getMapping returns a mapping for the t type, using the tagName, mapFunc and
|
||||||
|
// tagMapFunc to determine the canonical names of fields.
|
||||||
|
func getMapping(t reflect.Type, tagName string, mapFunc, tagMapFunc mapf) *StructMap {
|
||||||
|
m := []*FieldInfo{}
|
||||||
|
|
||||||
|
root := &FieldInfo{}
|
||||||
|
queue := []typeQueue{}
|
||||||
|
queue = append(queue, typeQueue{Deref(t), root, ""})
|
||||||
|
|
||||||
|
QueueLoop:
|
||||||
|
for len(queue) != 0 {
|
||||||
|
// pop the first item off of the queue
|
||||||
|
tq := queue[0]
|
||||||
|
queue = queue[1:]
|
||||||
|
|
||||||
|
// ignore recursive field
|
||||||
|
for p := tq.fi.Parent; p != nil; p = p.Parent {
|
||||||
|
if tq.fi.Field.Type == p.Field.Type {
|
||||||
|
continue QueueLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nChildren := 0
|
||||||
|
if tq.t.Kind() == reflect.Struct {
|
||||||
|
nChildren = tq.t.NumField()
|
||||||
|
}
|
||||||
|
tq.fi.Children = make([]*FieldInfo, nChildren)
|
||||||
|
|
||||||
|
// iterate through all of its fields
|
||||||
|
for fieldPos := 0; fieldPos < nChildren; fieldPos++ {
|
||||||
|
|
||||||
|
f := tq.t.Field(fieldPos)
|
||||||
|
|
||||||
|
// parse the tag and the target name using the mapping options for this field
|
||||||
|
tag, name := parseName(f, tagName, mapFunc, tagMapFunc)
|
||||||
|
|
||||||
|
// if the name is "-", disabled via a tag, skip it
|
||||||
|
if name == "-" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fi := FieldInfo{
|
||||||
|
Field: f,
|
||||||
|
Name: name,
|
||||||
|
Zero: reflect.New(f.Type).Elem(),
|
||||||
|
Options: parseOptions(tag),
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the path is empty this path is just the name
|
||||||
|
if tq.pp == "" {
|
||||||
|
fi.Path = fi.Name
|
||||||
|
} else {
|
||||||
|
fi.Path = tq.pp + "." + fi.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip unexported fields
|
||||||
|
if len(f.PkgPath) != 0 && !f.Anonymous {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// bfs search of anonymous embedded structs
|
||||||
|
if f.Anonymous {
|
||||||
|
pp := tq.pp
|
||||||
|
if tag != "" {
|
||||||
|
pp = fi.Path
|
||||||
|
}
|
||||||
|
|
||||||
|
fi.Embedded = true
|
||||||
|
fi.Index = apnd(tq.fi.Index, fieldPos)
|
||||||
|
nChildren := 0
|
||||||
|
ft := Deref(f.Type)
|
||||||
|
if ft.Kind() == reflect.Struct {
|
||||||
|
nChildren = ft.NumField()
|
||||||
|
}
|
||||||
|
fi.Children = make([]*FieldInfo, nChildren)
|
||||||
|
queue = append(queue, typeQueue{Deref(f.Type), &fi, pp})
|
||||||
|
} else if fi.Zero.Kind() == reflect.Struct || (fi.Zero.Kind() == reflect.Ptr && fi.Zero.Type().Elem().Kind() == reflect.Struct) {
|
||||||
|
fi.Index = apnd(tq.fi.Index, fieldPos)
|
||||||
|
fi.Children = make([]*FieldInfo, Deref(f.Type).NumField())
|
||||||
|
queue = append(queue, typeQueue{Deref(f.Type), &fi, fi.Path})
|
||||||
|
}
|
||||||
|
|
||||||
|
fi.Index = apnd(tq.fi.Index, fieldPos)
|
||||||
|
fi.Parent = tq.fi
|
||||||
|
tq.fi.Children[fieldPos] = &fi
|
||||||
|
m = append(m, &fi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flds := &StructMap{Index: m, Tree: root, Paths: map[string]*FieldInfo{}, Names: map[string]*FieldInfo{}}
|
||||||
|
for _, fi := range flds.Index {
|
||||||
|
flds.Paths[fi.Path] = fi
|
||||||
|
if fi.Name != "" && !fi.Embedded {
|
||||||
|
flds.Names[fi.Path] = fi
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return flds
|
||||||
|
}
|
1045
vendor/github.com/jmoiron/sqlx/sqlx.go
generated
vendored
Normal file
1045
vendor/github.com/jmoiron/sqlx/sqlx.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
346
vendor/github.com/jmoiron/sqlx/sqlx_context.go
generated
vendored
Normal file
346
vendor/github.com/jmoiron/sqlx/sqlx_context.go
generated
vendored
Normal file
@ -0,0 +1,346 @@
|
|||||||
|
// +build go1.8
|
||||||
|
|
||||||
|
package sqlx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ConnectContext to a database and verify with a ping.
|
||||||
|
func ConnectContext(ctx context.Context, driverName, dataSourceName string) (*DB, error) {
|
||||||
|
db, err := Open(driverName, dataSourceName)
|
||||||
|
if err != nil {
|
||||||
|
return db, err
|
||||||
|
}
|
||||||
|
err = db.PingContext(ctx)
|
||||||
|
return db, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryerContext is an interface used by GetContext and SelectContext
|
||||||
|
type QueryerContext interface {
|
||||||
|
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
|
||||||
|
QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
|
||||||
|
QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row
|
||||||
|
}
|
||||||
|
|
||||||
|
// PreparerContext is an interface used by PreparexContext.
|
||||||
|
type PreparerContext interface {
|
||||||
|
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExecerContext is an interface used by MustExecContext and LoadFileContext
|
||||||
|
type ExecerContext interface {
|
||||||
|
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExtContext is a union interface which can bind, query, and exec, with Context
|
||||||
|
// used by NamedQueryContext and NamedExecContext.
|
||||||
|
type ExtContext interface {
|
||||||
|
binder
|
||||||
|
QueryerContext
|
||||||
|
ExecerContext
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectContext executes a query using the provided Queryer, and StructScans
|
||||||
|
// each row into dest, which must be a slice. If the slice elements are
|
||||||
|
// scannable, then the result set must have only one column. Otherwise,
|
||||||
|
// StructScan is used. The *sql.Rows are closed automatically.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func SelectContext(ctx context.Context, q QueryerContext, dest interface{}, query string, args ...interface{}) error {
|
||||||
|
rows, err := q.QueryxContext(ctx, query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// if something happens here, we want to make sure the rows are Closed
|
||||||
|
defer rows.Close()
|
||||||
|
return scanAll(rows, dest, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PreparexContext prepares a statement.
|
||||||
|
//
|
||||||
|
// The provided context is used for the preparation of the statement, not for
|
||||||
|
// the execution of the statement.
|
||||||
|
func PreparexContext(ctx context.Context, p PreparerContext, query string) (*Stmt, error) {
|
||||||
|
s, err := p.PrepareContext(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Stmt{Stmt: s, unsafe: isUnsafe(p), Mapper: mapperFor(p)}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContext does a QueryRow using the provided Queryer, and scans the
|
||||||
|
// resulting row to dest. If dest is scannable, the result must only have one
|
||||||
|
// column. Otherwise, StructScan is used. Get will return sql.ErrNoRows like
|
||||||
|
// row.Scan would. Any placeholder parameters are replaced with supplied args.
|
||||||
|
// An error is returned if the result set is empty.
|
||||||
|
func GetContext(ctx context.Context, q QueryerContext, dest interface{}, query string, args ...interface{}) error {
|
||||||
|
r := q.QueryRowxContext(ctx, query, args...)
|
||||||
|
return r.scanAny(dest, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadFileContext exec's every statement in a file (as a single call to Exec).
|
||||||
|
// LoadFileContext may return a nil *sql.Result if errors are encountered
|
||||||
|
// locating or reading the file at path. LoadFile reads the entire file into
|
||||||
|
// memory, so it is not suitable for loading large data dumps, but can be useful
|
||||||
|
// for initializing schemas or loading indexes.
|
||||||
|
//
|
||||||
|
// FIXME: this does not really work with multi-statement files for mattn/go-sqlite3
|
||||||
|
// or the go-mysql-driver/mysql drivers; pq seems to be an exception here. Detecting
|
||||||
|
// this by requiring something with DriverName() and then attempting to split the
|
||||||
|
// queries will be difficult to get right, and its current driver-specific behavior
|
||||||
|
// is deemed at least not complex in its incorrectness.
|
||||||
|
func LoadFileContext(ctx context.Context, e ExecerContext, path string) (*sql.Result, error) {
|
||||||
|
realpath, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
contents, err := ioutil.ReadFile(realpath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res, err := e.ExecContext(ctx, string(contents))
|
||||||
|
return &res, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustExecContext execs the query using e and panics if there was an error.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func MustExecContext(ctx context.Context, e ExecerContext, query string, args ...interface{}) sql.Result {
|
||||||
|
res, err := e.ExecContext(ctx, query, args...)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrepareNamedContext returns an sqlx.NamedStmt
|
||||||
|
func (db *DB) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error) {
|
||||||
|
return prepareNamedContext(ctx, db, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NamedQueryContext using this DB.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (db *DB) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*Rows, error) {
|
||||||
|
return NamedQueryContext(ctx, db, query, arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NamedExecContext using this DB.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (db *DB) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
|
||||||
|
return NamedExecContext(ctx, db, query, arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectContext using this DB.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (db *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
|
||||||
|
return SelectContext(ctx, db, dest, query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContext using this DB.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
// An error is returned if the result set is empty.
|
||||||
|
func (db *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
|
||||||
|
return GetContext(ctx, db, dest, query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.
|
||||||
|
//
|
||||||
|
// The provided context is used for the preparation of the statement, not for
|
||||||
|
// the execution of the statement.
|
||||||
|
func (db *DB) PreparexContext(ctx context.Context, query string) (*Stmt, error) {
|
||||||
|
return PreparexContext(ctx, db, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryxContext queries the database and returns an *sqlx.Rows.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (db *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
|
||||||
|
r, err := db.DB.QueryContext(ctx, query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Rows{Rows: r, unsafe: db.unsafe, Mapper: db.Mapper}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRowxContext queries the database and returns an *sqlx.Row.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (db *DB) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row {
|
||||||
|
rows, err := db.DB.QueryContext(ctx, query, args...)
|
||||||
|
return &Row{rows: rows, err: err, unsafe: db.unsafe, Mapper: db.Mapper}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustBeginTx starts a transaction, and panics on error. Returns an *sqlx.Tx instead
|
||||||
|
// of an *sql.Tx.
|
||||||
|
//
|
||||||
|
// The provided context is used until the transaction is committed or rolled
|
||||||
|
// back. If the context is canceled, the sql package will roll back the
|
||||||
|
// transaction. Tx.Commit will return an error if the context provided to
|
||||||
|
// MustBeginContext is canceled.
|
||||||
|
func (db *DB) MustBeginTx(ctx context.Context, opts *sql.TxOptions) *Tx {
|
||||||
|
tx, err := db.BeginTxx(ctx, opts)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return tx
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustExecContext (panic) runs MustExec using this database.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (db *DB) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result {
|
||||||
|
return MustExecContext(ctx, db, query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeginTxx begins a transaction and returns an *sqlx.Tx instead of an
|
||||||
|
// *sql.Tx.
|
||||||
|
//
|
||||||
|
// The provided context is used until the transaction is committed or rolled
|
||||||
|
// back. If the context is canceled, the sql package will roll back the
|
||||||
|
// transaction. Tx.Commit will return an error if the context provided to
|
||||||
|
// BeginxContext is canceled.
|
||||||
|
func (db *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) {
|
||||||
|
tx, err := db.DB.BeginTx(ctx, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Tx{Tx: tx, driverName: db.driverName, unsafe: db.unsafe, Mapper: db.Mapper}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// StmtxContext returns a version of the prepared statement which runs within a
|
||||||
|
// transaction. Provided stmt can be either *sql.Stmt or *sqlx.Stmt.
|
||||||
|
func (tx *Tx) StmtxContext(ctx context.Context, stmt interface{}) *Stmt {
|
||||||
|
var s *sql.Stmt
|
||||||
|
switch v := stmt.(type) {
|
||||||
|
case Stmt:
|
||||||
|
s = v.Stmt
|
||||||
|
case *Stmt:
|
||||||
|
s = v.Stmt
|
||||||
|
case *sql.Stmt:
|
||||||
|
s = v
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("non-statement type %v passed to Stmtx", reflect.ValueOf(stmt).Type()))
|
||||||
|
}
|
||||||
|
return &Stmt{Stmt: tx.StmtContext(ctx, s), Mapper: tx.Mapper}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NamedStmtContext returns a version of the prepared statement which runs
|
||||||
|
// within a transaction.
|
||||||
|
func (tx *Tx) NamedStmtContext(ctx context.Context, stmt *NamedStmt) *NamedStmt {
|
||||||
|
return &NamedStmt{
|
||||||
|
QueryString: stmt.QueryString,
|
||||||
|
Params: stmt.Params,
|
||||||
|
Stmt: tx.StmtxContext(ctx, stmt.Stmt),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.
|
||||||
|
//
|
||||||
|
// The provided context is used for the preparation of the statement, not for
|
||||||
|
// the execution of the statement.
|
||||||
|
func (tx *Tx) PreparexContext(ctx context.Context, query string) (*Stmt, error) {
|
||||||
|
return PreparexContext(ctx, tx, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrepareNamedContext returns an sqlx.NamedStmt
|
||||||
|
func (tx *Tx) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error) {
|
||||||
|
return prepareNamedContext(ctx, tx, query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustExecContext runs MustExecContext within a transaction.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (tx *Tx) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result {
|
||||||
|
return MustExecContext(ctx, tx, query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryxContext within a transaction and context.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (tx *Tx) QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
|
||||||
|
r, err := tx.Tx.QueryContext(ctx, query, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Rows{Rows: r, unsafe: tx.unsafe, Mapper: tx.Mapper}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectContext within a transaction and context.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (tx *Tx) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
|
||||||
|
return SelectContext(ctx, tx, dest, query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContext within a transaction and context.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
// An error is returned if the result set is empty.
|
||||||
|
func (tx *Tx) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
|
||||||
|
return GetContext(ctx, tx, dest, query, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRowxContext within a transaction and context.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (tx *Tx) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row {
|
||||||
|
rows, err := tx.Tx.QueryContext(ctx, query, args...)
|
||||||
|
return &Row{rows: rows, err: err, unsafe: tx.unsafe, Mapper: tx.Mapper}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NamedExecContext using this Tx.
|
||||||
|
// Any named placeholder parameters are replaced with fields from arg.
|
||||||
|
func (tx *Tx) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
|
||||||
|
return NamedExecContext(ctx, tx, query, arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectContext using the prepared statement.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (s *Stmt) SelectContext(ctx context.Context, dest interface{}, args ...interface{}) error {
|
||||||
|
return SelectContext(ctx, &qStmt{s}, dest, "", args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetContext using the prepared statement.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
// An error is returned if the result set is empty.
|
||||||
|
func (s *Stmt) GetContext(ctx context.Context, dest interface{}, args ...interface{}) error {
|
||||||
|
return GetContext(ctx, &qStmt{s}, dest, "", args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustExecContext (panic) using this statement. Note that the query portion of
|
||||||
|
// the error output will be blank, as Stmt does not expose its query.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (s *Stmt) MustExecContext(ctx context.Context, args ...interface{}) sql.Result {
|
||||||
|
return MustExecContext(ctx, &qStmt{s}, "", args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRowxContext using this statement.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (s *Stmt) QueryRowxContext(ctx context.Context, args ...interface{}) *Row {
|
||||||
|
qs := &qStmt{s}
|
||||||
|
return qs.QueryRowxContext(ctx, "", args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryxContext using this statement.
|
||||||
|
// Any placeholder parameters are replaced with supplied args.
|
||||||
|
func (s *Stmt) QueryxContext(ctx context.Context, args ...interface{}) (*Rows, error) {
|
||||||
|
qs := &qStmt{s}
|
||||||
|
return qs.QueryxContext(ctx, "", args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *qStmt) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
|
||||||
|
return q.Stmt.QueryContext(ctx, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *qStmt) QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
|
||||||
|
r, err := q.Stmt.QueryContext(ctx, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Rows{Rows: r, unsafe: q.Stmt.unsafe, Mapper: q.Stmt.Mapper}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *qStmt) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row {
|
||||||
|
rows, err := q.Stmt.QueryContext(ctx, args...)
|
||||||
|
return &Row{rows: rows, err: err, unsafe: q.Stmt.unsafe, Mapper: q.Stmt.Mapper}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *qStmt) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
|
||||||
|
return q.Stmt.ExecContext(ctx, args...)
|
||||||
|
}
|
3
vendor/golang.org/x/net/AUTHORS
generated
vendored
Normal file
3
vendor/golang.org/x/net/AUTHORS
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# This source code refers to The Go Authors for copyright purposes.
|
||||||
|
# The master list of authors is in the main Go distribution,
|
||||||
|
# visible at http://tip.golang.org/AUTHORS.
|
3
vendor/golang.org/x/net/CONTRIBUTORS
generated
vendored
Normal file
3
vendor/golang.org/x/net/CONTRIBUTORS
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# This source code was written by the Go contributors.
|
||||||
|
# The master list of contributors is in the main Go distribution,
|
||||||
|
# visible at http://tip.golang.org/CONTRIBUTORS.
|
27
vendor/golang.org/x/net/LICENSE
generated
vendored
Normal file
27
vendor/golang.org/x/net/LICENSE
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following disclaimer
|
||||||
|
in the documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
* Neither the name of Google Inc. nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
22
vendor/golang.org/x/net/PATENTS
generated
vendored
Normal file
22
vendor/golang.org/x/net/PATENTS
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
Additional IP Rights Grant (Patents)
|
||||||
|
|
||||||
|
"This implementation" means the copyrightable works distributed by
|
||||||
|
Google as part of the Go project.
|
||||||
|
|
||||||
|
Google hereby grants to You a perpetual, worldwide, non-exclusive,
|
||||||
|
no-charge, royalty-free, irrevocable (except as stated in this section)
|
||||||
|
patent license to make, have made, use, offer to sell, sell, import,
|
||||||
|
transfer and otherwise run, modify and propagate the contents of this
|
||||||
|
implementation of Go, where such license applies only to those patent
|
||||||
|
claims, both currently owned or controlled by Google and acquired in
|
||||||
|
the future, licensable by Google that are necessarily infringed by this
|
||||||
|
implementation of Go. This grant does not include claims that would be
|
||||||
|
infringed only as a consequence of further modification of this
|
||||||
|
implementation. If you or your agent or exclusive licensee institute or
|
||||||
|
order or agree to the institution of patent litigation against any
|
||||||
|
entity (including a cross-claim or counterclaim in a lawsuit) alleging
|
||||||
|
that this implementation of Go or any code incorporated within this
|
||||||
|
implementation of Go constitutes direct or contributory patent
|
||||||
|
infringement, or inducement of patent infringement, then any patent
|
||||||
|
rights granted to you under this License for this implementation of Go
|
||||||
|
shall terminate as of the date such litigation is filed.
|
78
vendor/golang.org/x/net/html/atom/atom.go
generated
vendored
Normal file
78
vendor/golang.org/x/net/html/atom/atom.go
generated
vendored
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
// Copyright 2012 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package atom provides integer codes (also known as atoms) for a fixed set of
|
||||||
|
// frequently occurring HTML strings: tag names and attribute keys such as "p"
|
||||||
|
// and "id".
|
||||||
|
//
|
||||||
|
// Sharing an atom's name between all elements with the same tag can result in
|
||||||
|
// fewer string allocations when tokenizing and parsing HTML. Integer
|
||||||
|
// comparisons are also generally faster than string comparisons.
|
||||||
|
//
|
||||||
|
// The value of an atom's particular code is not guaranteed to stay the same
|
||||||
|
// between versions of this package. Neither is any ordering guaranteed:
|
||||||
|
// whether atom.H1 < atom.H2 may also change. The codes are not guaranteed to
|
||||||
|
// be dense. The only guarantees are that e.g. looking up "div" will yield
|
||||||
|
// atom.Div, calling atom.Div.String will return "div", and atom.Div != 0.
|
||||||
|
package atom // import "golang.org/x/net/html/atom"
|
||||||
|
|
||||||
|
// Atom is an integer code for a string. The zero value maps to "".
|
||||||
|
type Atom uint32
|
||||||
|
|
||||||
|
// String returns the atom's name.
|
||||||
|
func (a Atom) String() string {
|
||||||
|
start := uint32(a >> 8)
|
||||||
|
n := uint32(a & 0xff)
|
||||||
|
if start+n > uint32(len(atomText)) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return atomText[start : start+n]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Atom) string() string {
|
||||||
|
return atomText[a>>8 : a>>8+a&0xff]
|
||||||
|
}
|
||||||
|
|
||||||
|
// fnv computes the FNV hash with an arbitrary starting value h.
|
||||||
|
func fnv(h uint32, s []byte) uint32 {
|
||||||
|
for i := range s {
|
||||||
|
h ^= uint32(s[i])
|
||||||
|
h *= 16777619
|
||||||
|
}
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
func match(s string, t []byte) bool {
|
||||||
|
for i, c := range t {
|
||||||
|
if s[i] != c {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lookup returns the atom whose name is s. It returns zero if there is no
|
||||||
|
// such atom. The lookup is case sensitive.
|
||||||
|
func Lookup(s []byte) Atom {
|
||||||
|
if len(s) == 0 || len(s) > maxAtomLen {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
h := fnv(hash0, s)
|
||||||
|
if a := table[h&uint32(len(table)-1)]; int(a&0xff) == len(s) && match(a.string(), s) {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
if a := table[(h>>16)&uint32(len(table)-1)]; int(a&0xff) == len(s) && match(a.string(), s) {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns a string whose contents are equal to s. In that sense, it is
|
||||||
|
// equivalent to string(s) but may be more efficient.
|
||||||
|
func String(s []byte) string {
|
||||||
|
if a := Lookup(s); a != 0 {
|
||||||
|
return a.String()
|
||||||
|
}
|
||||||
|
return string(s)
|
||||||
|
}
|
712
vendor/golang.org/x/net/html/atom/gen.go
generated
vendored
Normal file
712
vendor/golang.org/x/net/html/atom/gen.go
generated
vendored
Normal file
@ -0,0 +1,712 @@
|
|||||||
|
// Copyright 2012 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
//go:generate go run gen.go
|
||||||
|
//go:generate go run gen.go -test
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"go/format"
|
||||||
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// identifier converts s to a Go exported identifier.
|
||||||
|
// It converts "div" to "Div" and "accept-charset" to "AcceptCharset".
|
||||||
|
func identifier(s string) string {
|
||||||
|
b := make([]byte, 0, len(s))
|
||||||
|
cap := true
|
||||||
|
for _, c := range s {
|
||||||
|
if c == '-' {
|
||||||
|
cap = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if cap && 'a' <= c && c <= 'z' {
|
||||||
|
c -= 'a' - 'A'
|
||||||
|
}
|
||||||
|
cap = false
|
||||||
|
b = append(b, byte(c))
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
var test = flag.Bool("test", false, "generate table_test.go")
|
||||||
|
|
||||||
|
func genFile(name string, buf *bytes.Buffer) {
|
||||||
|
b, err := format.Source(buf.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if err := ioutil.WriteFile(name, b, 0644); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
var all []string
|
||||||
|
all = append(all, elements...)
|
||||||
|
all = append(all, attributes...)
|
||||||
|
all = append(all, eventHandlers...)
|
||||||
|
all = append(all, extra...)
|
||||||
|
sort.Strings(all)
|
||||||
|
|
||||||
|
// uniq - lists have dups
|
||||||
|
w := 0
|
||||||
|
for _, s := range all {
|
||||||
|
if w == 0 || all[w-1] != s {
|
||||||
|
all[w] = s
|
||||||
|
w++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
all = all[:w]
|
||||||
|
|
||||||
|
if *test {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
fmt.Fprintln(&buf, "// Code generated by go generate gen.go; DO NOT EDIT.\n")
|
||||||
|
fmt.Fprintln(&buf, "//go:generate go run gen.go -test\n")
|
||||||
|
fmt.Fprintln(&buf, "package atom\n")
|
||||||
|
fmt.Fprintln(&buf, "var testAtomList = []string{")
|
||||||
|
for _, s := range all {
|
||||||
|
fmt.Fprintf(&buf, "\t%q,\n", s)
|
||||||
|
}
|
||||||
|
fmt.Fprintln(&buf, "}")
|
||||||
|
|
||||||
|
genFile("table_test.go", &buf)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find hash that minimizes table size.
|
||||||
|
var best *table
|
||||||
|
for i := 0; i < 1000000; i++ {
|
||||||
|
if best != nil && 1<<(best.k-1) < len(all) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
h := rand.Uint32()
|
||||||
|
for k := uint(0); k <= 16; k++ {
|
||||||
|
if best != nil && k >= best.k {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
var t table
|
||||||
|
if t.init(h, k, all) {
|
||||||
|
best = &t
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if best == nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to construct string table\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lay out strings, using overlaps when possible.
|
||||||
|
layout := append([]string{}, all...)
|
||||||
|
|
||||||
|
// Remove strings that are substrings of other strings
|
||||||
|
for changed := true; changed; {
|
||||||
|
changed = false
|
||||||
|
for i, s := range layout {
|
||||||
|
if s == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for j, t := range layout {
|
||||||
|
if i != j && t != "" && strings.Contains(s, t) {
|
||||||
|
changed = true
|
||||||
|
layout[j] = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join strings where one suffix matches another prefix.
|
||||||
|
for {
|
||||||
|
// Find best i, j, k such that layout[i][len-k:] == layout[j][:k],
|
||||||
|
// maximizing overlap length k.
|
||||||
|
besti := -1
|
||||||
|
bestj := -1
|
||||||
|
bestk := 0
|
||||||
|
for i, s := range layout {
|
||||||
|
if s == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for j, t := range layout {
|
||||||
|
if i == j {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for k := bestk + 1; k <= len(s) && k <= len(t); k++ {
|
||||||
|
if s[len(s)-k:] == t[:k] {
|
||||||
|
besti = i
|
||||||
|
bestj = j
|
||||||
|
bestk = k
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if bestk > 0 {
|
||||||
|
layout[besti] += layout[bestj][bestk:]
|
||||||
|
layout[bestj] = ""
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
text := strings.Join(layout, "")
|
||||||
|
|
||||||
|
atom := map[string]uint32{}
|
||||||
|
for _, s := range all {
|
||||||
|
off := strings.Index(text, s)
|
||||||
|
if off < 0 {
|
||||||
|
panic("lost string " + s)
|
||||||
|
}
|
||||||
|
atom[s] = uint32(off<<8 | len(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
// Generate the Go code.
|
||||||
|
fmt.Fprintln(&buf, "// Code generated by go generate gen.go; DO NOT EDIT.\n")
|
||||||
|
fmt.Fprintln(&buf, "//go:generate go run gen.go\n")
|
||||||
|
fmt.Fprintln(&buf, "package atom\n\nconst (")
|
||||||
|
|
||||||
|
// compute max len
|
||||||
|
maxLen := 0
|
||||||
|
for _, s := range all {
|
||||||
|
if maxLen < len(s) {
|
||||||
|
maxLen = len(s)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&buf, "\t%s Atom = %#x\n", identifier(s), atom[s])
|
||||||
|
}
|
||||||
|
fmt.Fprintln(&buf, ")\n")
|
||||||
|
|
||||||
|
fmt.Fprintf(&buf, "const hash0 = %#x\n\n", best.h0)
|
||||||
|
fmt.Fprintf(&buf, "const maxAtomLen = %d\n\n", maxLen)
|
||||||
|
|
||||||
|
fmt.Fprintf(&buf, "var table = [1<<%d]Atom{\n", best.k)
|
||||||
|
for i, s := range best.tab {
|
||||||
|
if s == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&buf, "\t%#x: %#x, // %s\n", i, atom[s], s)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&buf, "}\n")
|
||||||
|
datasize := (1 << best.k) * 4
|
||||||
|
|
||||||
|
fmt.Fprintln(&buf, "const atomText =")
|
||||||
|
textsize := len(text)
|
||||||
|
for len(text) > 60 {
|
||||||
|
fmt.Fprintf(&buf, "\t%q +\n", text[:60])
|
||||||
|
text = text[60:]
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&buf, "\t%q\n\n", text)
|
||||||
|
|
||||||
|
genFile("table.go", &buf)
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stdout, "%d atoms; %d string bytes + %d tables = %d total data\n", len(all), textsize, datasize, textsize+datasize)
|
||||||
|
}
|
||||||
|
|
||||||
|
type byLen []string
|
||||||
|
|
||||||
|
func (x byLen) Less(i, j int) bool { return len(x[i]) > len(x[j]) }
|
||||||
|
func (x byLen) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||||
|
func (x byLen) Len() int { return len(x) }
|
||||||
|
|
||||||
|
// fnv computes the FNV hash with an arbitrary starting value h.
|
||||||
|
func fnv(h uint32, s string) uint32 {
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
h ^= uint32(s[i])
|
||||||
|
h *= 16777619
|
||||||
|
}
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
// A table represents an attempt at constructing the lookup table.
|
||||||
|
// The lookup table uses cuckoo hashing, meaning that each string
|
||||||
|
// can be found in one of two positions.
|
||||||
|
type table struct {
|
||||||
|
h0 uint32
|
||||||
|
k uint
|
||||||
|
mask uint32
|
||||||
|
tab []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// hash returns the two hashes for s.
|
||||||
|
func (t *table) hash(s string) (h1, h2 uint32) {
|
||||||
|
h := fnv(t.h0, s)
|
||||||
|
h1 = h & t.mask
|
||||||
|
h2 = (h >> 16) & t.mask
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// init initializes the table with the given parameters.
|
||||||
|
// h0 is the initial hash value,
|
||||||
|
// k is the number of bits of hash value to use, and
|
||||||
|
// x is the list of strings to store in the table.
|
||||||
|
// init returns false if the table cannot be constructed.
|
||||||
|
func (t *table) init(h0 uint32, k uint, x []string) bool {
|
||||||
|
t.h0 = h0
|
||||||
|
t.k = k
|
||||||
|
t.tab = make([]string, 1<<k)
|
||||||
|
t.mask = 1<<k - 1
|
||||||
|
for _, s := range x {
|
||||||
|
if !t.insert(s) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert inserts s in the table.
|
||||||
|
func (t *table) insert(s string) bool {
|
||||||
|
h1, h2 := t.hash(s)
|
||||||
|
if t.tab[h1] == "" {
|
||||||
|
t.tab[h1] = s
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if t.tab[h2] == "" {
|
||||||
|
t.tab[h2] = s
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if t.push(h1, 0) {
|
||||||
|
t.tab[h1] = s
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if t.push(h2, 0) {
|
||||||
|
t.tab[h2] = s
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// push attempts to push aside the entry in slot i.
|
||||||
|
func (t *table) push(i uint32, depth int) bool {
|
||||||
|
if depth > len(t.tab) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s := t.tab[i]
|
||||||
|
h1, h2 := t.hash(s)
|
||||||
|
j := h1 + h2 - i
|
||||||
|
if t.tab[j] != "" && !t.push(j, depth+1) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
t.tab[j] = s
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// The lists of element names and attribute keys were taken from
|
||||||
|
// https://html.spec.whatwg.org/multipage/indices.html#index
|
||||||
|
// as of the "HTML Living Standard - Last Updated 16 April 2018" version.
|
||||||
|
|
||||||
|
// "command", "keygen" and "menuitem" have been removed from the spec,
|
||||||
|
// but are kept here for backwards compatibility.
|
||||||
|
var elements = []string{
|
||||||
|
"a",
|
||||||
|
"abbr",
|
||||||
|
"address",
|
||||||
|
"area",
|
||||||
|
"article",
|
||||||
|
"aside",
|
||||||
|
"audio",
|
||||||
|
"b",
|
||||||
|
"base",
|
||||||
|
"bdi",
|
||||||
|
"bdo",
|
||||||
|
"blockquote",
|
||||||
|
"body",
|
||||||
|
"br",
|
||||||
|
"button",
|
||||||
|
"canvas",
|
||||||
|
"caption",
|
||||||
|
"cite",
|
||||||
|
"code",
|
||||||
|
"col",
|
||||||
|
"colgroup",
|
||||||
|
"command",
|
||||||
|
"data",
|
||||||
|
"datalist",
|
||||||
|
"dd",
|
||||||
|
"del",
|
||||||
|
"details",
|
||||||
|
"dfn",
|
||||||
|
"dialog",
|
||||||
|
"div",
|
||||||
|
"dl",
|
||||||
|
"dt",
|
||||||
|
"em",
|
||||||
|
"embed",
|
||||||
|
"fieldset",
|
||||||
|
"figcaption",
|
||||||
|
"figure",
|
||||||
|
"footer",
|
||||||
|
"form",
|
||||||
|
"h1",
|
||||||
|
"h2",
|
||||||
|
"h3",
|
||||||
|
"h4",
|
||||||
|
"h5",
|
||||||
|
"h6",
|
||||||
|
"head",
|
||||||
|
"header",
|
||||||
|
"hgroup",
|
||||||
|
"hr",
|
||||||
|
"html",
|
||||||
|
"i",
|
||||||
|
"iframe",
|
||||||
|
"img",
|
||||||
|
"input",
|
||||||
|
"ins",
|
||||||
|
"kbd",
|
||||||
|
"keygen",
|
||||||
|
"label",
|
||||||
|
"legend",
|
||||||
|
"li",
|
||||||
|
"link",
|
||||||
|
"main",
|
||||||
|
"map",
|
||||||
|
"mark",
|
||||||
|
"menu",
|
||||||
|
"menuitem",
|
||||||
|
"meta",
|
||||||
|
"meter",
|
||||||
|
"nav",
|
||||||
|
"noscript",
|
||||||
|
"object",
|
||||||
|
"ol",
|
||||||
|
"optgroup",
|
||||||
|
"option",
|
||||||
|
"output",
|
||||||
|
"p",
|
||||||
|
"param",
|
||||||
|
"picture",
|
||||||
|
"pre",
|
||||||
|
"progress",
|
||||||
|
"q",
|
||||||
|
"rp",
|
||||||
|
"rt",
|
||||||
|
"ruby",
|
||||||
|
"s",
|
||||||
|
"samp",
|
||||||
|
"script",
|
||||||
|
"section",
|
||||||
|
"select",
|
||||||
|
"slot",
|
||||||
|
"small",
|
||||||
|
"source",
|
||||||
|
"span",
|
||||||
|
"strong",
|
||||||
|
"style",
|
||||||
|
"sub",
|
||||||
|
"summary",
|
||||||
|
"sup",
|
||||||
|
"table",
|
||||||
|
"tbody",
|
||||||
|
"td",
|
||||||
|
"template",
|
||||||
|
"textarea",
|
||||||
|
"tfoot",
|
||||||
|
"th",
|
||||||
|
"thead",
|
||||||
|
"time",
|
||||||
|
"title",
|
||||||
|
"tr",
|
||||||
|
"track",
|
||||||
|
"u",
|
||||||
|
"ul",
|
||||||
|
"var",
|
||||||
|
"video",
|
||||||
|
"wbr",
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/indices.html#attributes-3
|
||||||
|
//
|
||||||
|
// "challenge", "command", "contextmenu", "dropzone", "icon", "keytype", "mediagroup",
|
||||||
|
// "radiogroup", "spellcheck", "scoped", "seamless", "sortable" and "sorted" have been removed from the spec,
|
||||||
|
// but are kept here for backwards compatibility.
|
||||||
|
var attributes = []string{
|
||||||
|
"abbr",
|
||||||
|
"accept",
|
||||||
|
"accept-charset",
|
||||||
|
"accesskey",
|
||||||
|
"action",
|
||||||
|
"allowfullscreen",
|
||||||
|
"allowpaymentrequest",
|
||||||
|
"allowusermedia",
|
||||||
|
"alt",
|
||||||
|
"as",
|
||||||
|
"async",
|
||||||
|
"autocomplete",
|
||||||
|
"autofocus",
|
||||||
|
"autoplay",
|
||||||
|
"challenge",
|
||||||
|
"charset",
|
||||||
|
"checked",
|
||||||
|
"cite",
|
||||||
|
"class",
|
||||||
|
"color",
|
||||||
|
"cols",
|
||||||
|
"colspan",
|
||||||
|
"command",
|
||||||
|
"content",
|
||||||
|
"contenteditable",
|
||||||
|
"contextmenu",
|
||||||
|
"controls",
|
||||||
|
"coords",
|
||||||
|
"crossorigin",
|
||||||
|
"data",
|
||||||
|
"datetime",
|
||||||
|
"default",
|
||||||
|
"defer",
|
||||||
|
"dir",
|
||||||
|
"dirname",
|
||||||
|
"disabled",
|
||||||
|
"download",
|
||||||
|
"draggable",
|
||||||
|
"dropzone",
|
||||||
|
"enctype",
|
||||||
|
"for",
|
||||||
|
"form",
|
||||||
|
"formaction",
|
||||||
|
"formenctype",
|
||||||
|
"formmethod",
|
||||||
|
"formnovalidate",
|
||||||
|
"formtarget",
|
||||||
|
"headers",
|
||||||
|
"height",
|
||||||
|
"hidden",
|
||||||
|
"high",
|
||||||
|
"href",
|
||||||
|
"hreflang",
|
||||||
|
"http-equiv",
|
||||||
|
"icon",
|
||||||
|
"id",
|
||||||
|
"inputmode",
|
||||||
|
"integrity",
|
||||||
|
"is",
|
||||||
|
"ismap",
|
||||||
|
"itemid",
|
||||||
|
"itemprop",
|
||||||
|
"itemref",
|
||||||
|
"itemscope",
|
||||||
|
"itemtype",
|
||||||
|
"keytype",
|
||||||
|
"kind",
|
||||||
|
"label",
|
||||||
|
"lang",
|
||||||
|
"list",
|
||||||
|
"loop",
|
||||||
|
"low",
|
||||||
|
"manifest",
|
||||||
|
"max",
|
||||||
|
"maxlength",
|
||||||
|
"media",
|
||||||
|
"mediagroup",
|
||||||
|
"method",
|
||||||
|
"min",
|
||||||
|
"minlength",
|
||||||
|
"multiple",
|
||||||
|
"muted",
|
||||||
|
"name",
|
||||||
|
"nomodule",
|
||||||
|
"nonce",
|
||||||
|
"novalidate",
|
||||||
|
"open",
|
||||||
|
"optimum",
|
||||||
|
"pattern",
|
||||||
|
"ping",
|
||||||
|
"placeholder",
|
||||||
|
"playsinline",
|
||||||
|
"poster",
|
||||||
|
"preload",
|
||||||
|
"radiogroup",
|
||||||
|
"readonly",
|
||||||
|
"referrerpolicy",
|
||||||
|
"rel",
|
||||||
|
"required",
|
||||||
|
"reversed",
|
||||||
|
"rows",
|
||||||
|
"rowspan",
|
||||||
|
"sandbox",
|
||||||
|
"spellcheck",
|
||||||
|
"scope",
|
||||||
|
"scoped",
|
||||||
|
"seamless",
|
||||||
|
"selected",
|
||||||
|
"shape",
|
||||||
|
"size",
|
||||||
|
"sizes",
|
||||||
|
"sortable",
|
||||||
|
"sorted",
|
||||||
|
"slot",
|
||||||
|
"span",
|
||||||
|
"spellcheck",
|
||||||
|
"src",
|
||||||
|
"srcdoc",
|
||||||
|
"srclang",
|
||||||
|
"srcset",
|
||||||
|
"start",
|
||||||
|
"step",
|
||||||
|
"style",
|
||||||
|
"tabindex",
|
||||||
|
"target",
|
||||||
|
"title",
|
||||||
|
"translate",
|
||||||
|
"type",
|
||||||
|
"typemustmatch",
|
||||||
|
"updateviacache",
|
||||||
|
"usemap",
|
||||||
|
"value",
|
||||||
|
"width",
|
||||||
|
"workertype",
|
||||||
|
"wrap",
|
||||||
|
}
|
||||||
|
|
||||||
|
// "onautocomplete", "onautocompleteerror", "onmousewheel",
|
||||||
|
// "onshow" and "onsort" have been removed from the spec,
|
||||||
|
// but are kept here for backwards compatibility.
|
||||||
|
var eventHandlers = []string{
|
||||||
|
"onabort",
|
||||||
|
"onautocomplete",
|
||||||
|
"onautocompleteerror",
|
||||||
|
"onauxclick",
|
||||||
|
"onafterprint",
|
||||||
|
"onbeforeprint",
|
||||||
|
"onbeforeunload",
|
||||||
|
"onblur",
|
||||||
|
"oncancel",
|
||||||
|
"oncanplay",
|
||||||
|
"oncanplaythrough",
|
||||||
|
"onchange",
|
||||||
|
"onclick",
|
||||||
|
"onclose",
|
||||||
|
"oncontextmenu",
|
||||||
|
"oncopy",
|
||||||
|
"oncuechange",
|
||||||
|
"oncut",
|
||||||
|
"ondblclick",
|
||||||
|
"ondrag",
|
||||||
|
"ondragend",
|
||||||
|
"ondragenter",
|
||||||
|
"ondragexit",
|
||||||
|
"ondragleave",
|
||||||
|
"ondragover",
|
||||||
|
"ondragstart",
|
||||||
|
"ondrop",
|
||||||
|
"ondurationchange",
|
||||||
|
"onemptied",
|
||||||
|
"onended",
|
||||||
|
"onerror",
|
||||||
|
"onfocus",
|
||||||
|
"onhashchange",
|
||||||
|
"oninput",
|
||||||
|
"oninvalid",
|
||||||
|
"onkeydown",
|
||||||
|
"onkeypress",
|
||||||
|
"onkeyup",
|
||||||
|
"onlanguagechange",
|
||||||
|
"onload",
|
||||||
|
"onloadeddata",
|
||||||
|
"onloadedmetadata",
|
||||||
|
"onloadend",
|
||||||
|
"onloadstart",
|
||||||
|
"onmessage",
|
||||||
|
"onmessageerror",
|
||||||
|
"onmousedown",
|
||||||
|
"onmouseenter",
|
||||||
|
"onmouseleave",
|
||||||
|
"onmousemove",
|
||||||
|
"onmouseout",
|
||||||
|
"onmouseover",
|
||||||
|
"onmouseup",
|
||||||
|
"onmousewheel",
|
||||||
|
"onwheel",
|
||||||
|
"onoffline",
|
||||||
|
"ononline",
|
||||||
|
"onpagehide",
|
||||||
|
"onpageshow",
|
||||||
|
"onpaste",
|
||||||
|
"onpause",
|
||||||
|
"onplay",
|
||||||
|
"onplaying",
|
||||||
|
"onpopstate",
|
||||||
|
"onprogress",
|
||||||
|
"onratechange",
|
||||||
|
"onreset",
|
||||||
|
"onresize",
|
||||||
|
"onrejectionhandled",
|
||||||
|
"onscroll",
|
||||||
|
"onsecuritypolicyviolation",
|
||||||
|
"onseeked",
|
||||||
|
"onseeking",
|
||||||
|
"onselect",
|
||||||
|
"onshow",
|
||||||
|
"onsort",
|
||||||
|
"onstalled",
|
||||||
|
"onstorage",
|
||||||
|
"onsubmit",
|
||||||
|
"onsuspend",
|
||||||
|
"ontimeupdate",
|
||||||
|
"ontoggle",
|
||||||
|
"onunhandledrejection",
|
||||||
|
"onunload",
|
||||||
|
"onvolumechange",
|
||||||
|
"onwaiting",
|
||||||
|
}
|
||||||
|
|
||||||
|
// extra are ad-hoc values not covered by any of the lists above.
|
||||||
|
var extra = []string{
|
||||||
|
"acronym",
|
||||||
|
"align",
|
||||||
|
"annotation",
|
||||||
|
"annotation-xml",
|
||||||
|
"applet",
|
||||||
|
"basefont",
|
||||||
|
"bgsound",
|
||||||
|
"big",
|
||||||
|
"blink",
|
||||||
|
"center",
|
||||||
|
"color",
|
||||||
|
"desc",
|
||||||
|
"face",
|
||||||
|
"font",
|
||||||
|
"foreignObject", // HTML is case-insensitive, but SVG-embedded-in-HTML is case-sensitive.
|
||||||
|
"foreignobject",
|
||||||
|
"frame",
|
||||||
|
"frameset",
|
||||||
|
"image",
|
||||||
|
"isindex",
|
||||||
|
"listing",
|
||||||
|
"malignmark",
|
||||||
|
"marquee",
|
||||||
|
"math",
|
||||||
|
"mglyph",
|
||||||
|
"mi",
|
||||||
|
"mn",
|
||||||
|
"mo",
|
||||||
|
"ms",
|
||||||
|
"mtext",
|
||||||
|
"nobr",
|
||||||
|
"noembed",
|
||||||
|
"noframes",
|
||||||
|
"plaintext",
|
||||||
|
"prompt",
|
||||||
|
"public",
|
||||||
|
"rb",
|
||||||
|
"rtc",
|
||||||
|
"spacer",
|
||||||
|
"strike",
|
||||||
|
"svg",
|
||||||
|
"system",
|
||||||
|
"tt",
|
||||||
|
"xmp",
|
||||||
|
}
|
783
vendor/golang.org/x/net/html/atom/table.go
generated
vendored
Normal file
783
vendor/golang.org/x/net/html/atom/table.go
generated
vendored
Normal file
@ -0,0 +1,783 @@
|
|||||||
|
// Code generated by go generate gen.go; DO NOT EDIT.
|
||||||
|
|
||||||
|
//go:generate go run gen.go
|
||||||
|
|
||||||
|
package atom
|
||||||
|
|
||||||
|
const (
|
||||||
|
A Atom = 0x1
|
||||||
|
Abbr Atom = 0x4
|
||||||
|
Accept Atom = 0x1a06
|
||||||
|
AcceptCharset Atom = 0x1a0e
|
||||||
|
Accesskey Atom = 0x2c09
|
||||||
|
Acronym Atom = 0xaa07
|
||||||
|
Action Atom = 0x27206
|
||||||
|
Address Atom = 0x6f307
|
||||||
|
Align Atom = 0xb105
|
||||||
|
Allowfullscreen Atom = 0x2080f
|
||||||
|
Allowpaymentrequest Atom = 0xc113
|
||||||
|
Allowusermedia Atom = 0xdd0e
|
||||||
|
Alt Atom = 0xf303
|
||||||
|
Annotation Atom = 0x1c90a
|
||||||
|
AnnotationXml Atom = 0x1c90e
|
||||||
|
Applet Atom = 0x31906
|
||||||
|
Area Atom = 0x35604
|
||||||
|
Article Atom = 0x3fc07
|
||||||
|
As Atom = 0x3c02
|
||||||
|
Aside Atom = 0x10705
|
||||||
|
Async Atom = 0xff05
|
||||||
|
Audio Atom = 0x11505
|
||||||
|
Autocomplete Atom = 0x2780c
|
||||||
|
Autofocus Atom = 0x12109
|
||||||
|
Autoplay Atom = 0x13c08
|
||||||
|
B Atom = 0x101
|
||||||
|
Base Atom = 0x3b04
|
||||||
|
Basefont Atom = 0x3b08
|
||||||
|
Bdi Atom = 0xba03
|
||||||
|
Bdo Atom = 0x14b03
|
||||||
|
Bgsound Atom = 0x15e07
|
||||||
|
Big Atom = 0x17003
|
||||||
|
Blink Atom = 0x17305
|
||||||
|
Blockquote Atom = 0x1870a
|
||||||
|
Body Atom = 0x2804
|
||||||
|
Br Atom = 0x202
|
||||||
|
Button Atom = 0x19106
|
||||||
|
Canvas Atom = 0x10306
|
||||||
|
Caption Atom = 0x23107
|
||||||
|
Center Atom = 0x22006
|
||||||
|
Challenge Atom = 0x29b09
|
||||||
|
Charset Atom = 0x2107
|
||||||
|
Checked Atom = 0x47907
|
||||||
|
Cite Atom = 0x19c04
|
||||||
|
Class Atom = 0x56405
|
||||||
|
Code Atom = 0x5c504
|
||||||
|
Col Atom = 0x1ab03
|
||||||
|
Colgroup Atom = 0x1ab08
|
||||||
|
Color Atom = 0x1bf05
|
||||||
|
Cols Atom = 0x1c404
|
||||||
|
Colspan Atom = 0x1c407
|
||||||
|
Command Atom = 0x1d707
|
||||||
|
Content Atom = 0x58b07
|
||||||
|
Contenteditable Atom = 0x58b0f
|
||||||
|
Contextmenu Atom = 0x3800b
|
||||||
|
Controls Atom = 0x1de08
|
||||||
|
Coords Atom = 0x1ea06
|
||||||
|
Crossorigin Atom = 0x1fb0b
|
||||||
|
Data Atom = 0x4a504
|
||||||
|
Datalist Atom = 0x4a508
|
||||||
|
Datetime Atom = 0x2b808
|
||||||
|
Dd Atom = 0x2d702
|
||||||
|
Default Atom = 0x10a07
|
||||||
|
Defer Atom = 0x5c705
|
||||||
|
Del Atom = 0x45203
|
||||||
|
Desc Atom = 0x56104
|
||||||
|
Details Atom = 0x7207
|
||||||
|
Dfn Atom = 0x8703
|
||||||
|
Dialog Atom = 0xbb06
|
||||||
|
Dir Atom = 0x9303
|
||||||
|
Dirname Atom = 0x9307
|
||||||
|
Disabled Atom = 0x16408
|
||||||
|
Div Atom = 0x16b03
|
||||||
|
Dl Atom = 0x5e602
|
||||||
|
Download Atom = 0x46308
|
||||||
|
Draggable Atom = 0x17a09
|
||||||
|
Dropzone Atom = 0x40508
|
||||||
|
Dt Atom = 0x64b02
|
||||||
|
Em Atom = 0x6e02
|
||||||
|
Embed Atom = 0x6e05
|
||||||
|
Enctype Atom = 0x28d07
|
||||||
|
Face Atom = 0x21e04
|
||||||
|
Fieldset Atom = 0x22608
|
||||||
|
Figcaption Atom = 0x22e0a
|
||||||
|
Figure Atom = 0x24806
|
||||||
|
Font Atom = 0x3f04
|
||||||
|
Footer Atom = 0xf606
|
||||||
|
For Atom = 0x25403
|
||||||
|
ForeignObject Atom = 0x2540d
|
||||||
|
Foreignobject Atom = 0x2610d
|
||||||
|
Form Atom = 0x26e04
|
||||||
|
Formaction Atom = 0x26e0a
|
||||||
|
Formenctype Atom = 0x2890b
|
||||||
|
Formmethod Atom = 0x2a40a
|
||||||
|
Formnovalidate Atom = 0x2ae0e
|
||||||
|
Formtarget Atom = 0x2c00a
|
||||||
|
Frame Atom = 0x8b05
|
||||||
|
Frameset Atom = 0x8b08
|
||||||
|
H1 Atom = 0x15c02
|
||||||
|
H2 Atom = 0x2de02
|
||||||
|
H3 Atom = 0x30d02
|
||||||
|
H4 Atom = 0x34502
|
||||||
|
H5 Atom = 0x34f02
|
||||||
|
H6 Atom = 0x64d02
|
||||||
|
Head Atom = 0x33104
|
||||||
|
Header Atom = 0x33106
|
||||||
|
Headers Atom = 0x33107
|
||||||
|
Height Atom = 0x5206
|
||||||
|
Hgroup Atom = 0x2ca06
|
||||||
|
Hidden Atom = 0x2d506
|
||||||
|
High Atom = 0x2db04
|
||||||
|
Hr Atom = 0x15702
|
||||||
|
Href Atom = 0x2e004
|
||||||
|
Hreflang Atom = 0x2e008
|
||||||
|
Html Atom = 0x5604
|
||||||
|
HttpEquiv Atom = 0x2e80a
|
||||||
|
I Atom = 0x601
|
||||||
|
Icon Atom = 0x58a04
|
||||||
|
Id Atom = 0x10902
|
||||||
|
Iframe Atom = 0x2fc06
|
||||||
|
Image Atom = 0x30205
|
||||||
|
Img Atom = 0x30703
|
||||||
|
Input Atom = 0x44b05
|
||||||
|
Inputmode Atom = 0x44b09
|
||||||
|
Ins Atom = 0x20403
|
||||||
|
Integrity Atom = 0x23f09
|
||||||
|
Is Atom = 0x16502
|
||||||
|
Isindex Atom = 0x30f07
|
||||||
|
Ismap Atom = 0x31605
|
||||||
|
Itemid Atom = 0x38b06
|
||||||
|
Itemprop Atom = 0x19d08
|
||||||
|
Itemref Atom = 0x3cd07
|
||||||
|
Itemscope Atom = 0x67109
|
||||||
|
Itemtype Atom = 0x31f08
|
||||||
|
Kbd Atom = 0xb903
|
||||||
|
Keygen Atom = 0x3206
|
||||||
|
Keytype Atom = 0xd607
|
||||||
|
Kind Atom = 0x17704
|
||||||
|
Label Atom = 0x5905
|
||||||
|
Lang Atom = 0x2e404
|
||||||
|
Legend Atom = 0x18106
|
||||||
|
Li Atom = 0xb202
|
||||||
|
Link Atom = 0x17404
|
||||||
|
List Atom = 0x4a904
|
||||||
|
Listing Atom = 0x4a907
|
||||||
|
Loop Atom = 0x5d04
|
||||||
|
Low Atom = 0xc303
|
||||||
|
Main Atom = 0x1004
|
||||||
|
Malignmark Atom = 0xb00a
|
||||||
|
Manifest Atom = 0x6d708
|
||||||
|
Map Atom = 0x31803
|
||||||
|
Mark Atom = 0xb604
|
||||||
|
Marquee Atom = 0x32707
|
||||||
|
Math Atom = 0x32e04
|
||||||
|
Max Atom = 0x33d03
|
||||||
|
Maxlength Atom = 0x33d09
|
||||||
|
Media Atom = 0xe605
|
||||||
|
Mediagroup Atom = 0xe60a
|
||||||
|
Menu Atom = 0x38704
|
||||||
|
Menuitem Atom = 0x38708
|
||||||
|
Meta Atom = 0x4b804
|
||||||
|
Meter Atom = 0x9805
|
||||||
|
Method Atom = 0x2a806
|
||||||
|
Mglyph Atom = 0x30806
|
||||||
|
Mi Atom = 0x34702
|
||||||
|
Min Atom = 0x34703
|
||||||
|
Minlength Atom = 0x34709
|
||||||
|
Mn Atom = 0x2b102
|
||||||
|
Mo Atom = 0xa402
|
||||||
|
Ms Atom = 0x67402
|
||||||
|
Mtext Atom = 0x35105
|
||||||
|
Multiple Atom = 0x35f08
|
||||||
|
Muted Atom = 0x36705
|
||||||
|
Name Atom = 0x9604
|
||||||
|
Nav Atom = 0x1303
|
||||||
|
Nobr Atom = 0x3704
|
||||||
|
Noembed Atom = 0x6c07
|
||||||
|
Noframes Atom = 0x8908
|
||||||
|
Nomodule Atom = 0xa208
|
||||||
|
Nonce Atom = 0x1a605
|
||||||
|
Noscript Atom = 0x21608
|
||||||
|
Novalidate Atom = 0x2b20a
|
||||||
|
Object Atom = 0x26806
|
||||||
|
Ol Atom = 0x13702
|
||||||
|
Onabort Atom = 0x19507
|
||||||
|
Onafterprint Atom = 0x2360c
|
||||||
|
Onautocomplete Atom = 0x2760e
|
||||||
|
Onautocompleteerror Atom = 0x27613
|
||||||
|
Onauxclick Atom = 0x61f0a
|
||||||
|
Onbeforeprint Atom = 0x69e0d
|
||||||
|
Onbeforeunload Atom = 0x6e70e
|
||||||
|
Onblur Atom = 0x56d06
|
||||||
|
Oncancel Atom = 0x11908
|
||||||
|
Oncanplay Atom = 0x14d09
|
||||||
|
Oncanplaythrough Atom = 0x14d10
|
||||||
|
Onchange Atom = 0x41b08
|
||||||
|
Onclick Atom = 0x2f507
|
||||||
|
Onclose Atom = 0x36c07
|
||||||
|
Oncontextmenu Atom = 0x37e0d
|
||||||
|
Oncopy Atom = 0x39106
|
||||||
|
Oncuechange Atom = 0x3970b
|
||||||
|
Oncut Atom = 0x3a205
|
||||||
|
Ondblclick Atom = 0x3a70a
|
||||||
|
Ondrag Atom = 0x3b106
|
||||||
|
Ondragend Atom = 0x3b109
|
||||||
|
Ondragenter Atom = 0x3ba0b
|
||||||
|
Ondragexit Atom = 0x3c50a
|
||||||
|
Ondragleave Atom = 0x3df0b
|
||||||
|
Ondragover Atom = 0x3ea0a
|
||||||
|
Ondragstart Atom = 0x3f40b
|
||||||
|
Ondrop Atom = 0x40306
|
||||||
|
Ondurationchange Atom = 0x41310
|
||||||
|
Onemptied Atom = 0x40a09
|
||||||
|
Onended Atom = 0x42307
|
||||||
|
Onerror Atom = 0x42a07
|
||||||
|
Onfocus Atom = 0x43107
|
||||||
|
Onhashchange Atom = 0x43d0c
|
||||||
|
Oninput Atom = 0x44907
|
||||||
|
Oninvalid Atom = 0x45509
|
||||||
|
Onkeydown Atom = 0x45e09
|
||||||
|
Onkeypress Atom = 0x46b0a
|
||||||
|
Onkeyup Atom = 0x48007
|
||||||
|
Onlanguagechange Atom = 0x48d10
|
||||||
|
Onload Atom = 0x49d06
|
||||||
|
Onloadeddata Atom = 0x49d0c
|
||||||
|
Onloadedmetadata Atom = 0x4b010
|
||||||
|
Onloadend Atom = 0x4c609
|
||||||
|
Onloadstart Atom = 0x4cf0b
|
||||||
|
Onmessage Atom = 0x4da09
|
||||||
|
Onmessageerror Atom = 0x4da0e
|
||||||
|
Onmousedown Atom = 0x4e80b
|
||||||
|
Onmouseenter Atom = 0x4f30c
|
||||||
|
Onmouseleave Atom = 0x4ff0c
|
||||||
|
Onmousemove Atom = 0x50b0b
|
||||||
|
Onmouseout Atom = 0x5160a
|
||||||
|
Onmouseover Atom = 0x5230b
|
||||||
|
Onmouseup Atom = 0x52e09
|
||||||
|
Onmousewheel Atom = 0x53c0c
|
||||||
|
Onoffline Atom = 0x54809
|
||||||
|
Ononline Atom = 0x55108
|
||||||
|
Onpagehide Atom = 0x5590a
|
||||||
|
Onpageshow Atom = 0x5730a
|
||||||
|
Onpaste Atom = 0x57f07
|
||||||
|
Onpause Atom = 0x59a07
|
||||||
|
Onplay Atom = 0x5a406
|
||||||
|
Onplaying Atom = 0x5a409
|
||||||
|
Onpopstate Atom = 0x5ad0a
|
||||||
|
Onprogress Atom = 0x5b70a
|
||||||
|
Onratechange Atom = 0x5cc0c
|
||||||
|
Onrejectionhandled Atom = 0x5d812
|
||||||
|
Onreset Atom = 0x5ea07
|
||||||
|
Onresize Atom = 0x5f108
|
||||||
|
Onscroll Atom = 0x60008
|
||||||
|
Onsecuritypolicyviolation Atom = 0x60819
|
||||||
|
Onseeked Atom = 0x62908
|
||||||
|
Onseeking Atom = 0x63109
|
||||||
|
Onselect Atom = 0x63a08
|
||||||
|
Onshow Atom = 0x64406
|
||||||
|
Onsort Atom = 0x64f06
|
||||||
|
Onstalled Atom = 0x65909
|
||||||
|
Onstorage Atom = 0x66209
|
||||||
|
Onsubmit Atom = 0x66b08
|
||||||
|
Onsuspend Atom = 0x67b09
|
||||||
|
Ontimeupdate Atom = 0x400c
|
||||||
|
Ontoggle Atom = 0x68408
|
||||||
|
Onunhandledrejection Atom = 0x68c14
|
||||||
|
Onunload Atom = 0x6ab08
|
||||||
|
Onvolumechange Atom = 0x6b30e
|
||||||
|
Onwaiting Atom = 0x6c109
|
||||||
|
Onwheel Atom = 0x6ca07
|
||||||
|
Open Atom = 0x1a304
|
||||||
|
Optgroup Atom = 0x5f08
|
||||||
|
Optimum Atom = 0x6d107
|
||||||
|
Option Atom = 0x6e306
|
||||||
|
Output Atom = 0x51d06
|
||||||
|
P Atom = 0xc01
|
||||||
|
Param Atom = 0xc05
|
||||||
|
Pattern Atom = 0x6607
|
||||||
|
Picture Atom = 0x7b07
|
||||||
|
Ping Atom = 0xef04
|
||||||
|
Placeholder Atom = 0x1310b
|
||||||
|
Plaintext Atom = 0x1b209
|
||||||
|
Playsinline Atom = 0x1400b
|
||||||
|
Poster Atom = 0x2cf06
|
||||||
|
Pre Atom = 0x47003
|
||||||
|
Preload Atom = 0x48607
|
||||||
|
Progress Atom = 0x5b908
|
||||||
|
Prompt Atom = 0x53606
|
||||||
|
Public Atom = 0x58606
|
||||||
|
Q Atom = 0xcf01
|
||||||
|
Radiogroup Atom = 0x30a
|
||||||
|
Rb Atom = 0x3a02
|
||||||
|
Readonly Atom = 0x35708
|
||||||
|
Referrerpolicy Atom = 0x3d10e
|
||||||
|
Rel Atom = 0x48703
|
||||||
|
Required Atom = 0x24c08
|
||||||
|
Reversed Atom = 0x8008
|
||||||
|
Rows Atom = 0x9c04
|
||||||
|
Rowspan Atom = 0x9c07
|
||||||
|
Rp Atom = 0x23c02
|
||||||
|
Rt Atom = 0x19a02
|
||||||
|
Rtc Atom = 0x19a03
|
||||||
|
Ruby Atom = 0xfb04
|
||||||
|
S Atom = 0x2501
|
||||||
|
Samp Atom = 0x7804
|
||||||
|
Sandbox Atom = 0x12907
|
||||||
|
Scope Atom = 0x67505
|
||||||
|
Scoped Atom = 0x67506
|
||||||
|
Script Atom = 0x21806
|
||||||
|
Seamless Atom = 0x37108
|
||||||
|
Section Atom = 0x56807
|
||||||
|
Select Atom = 0x63c06
|
||||||
|
Selected Atom = 0x63c08
|
||||||
|
Shape Atom = 0x1e505
|
||||||
|
Size Atom = 0x5f504
|
||||||
|
Sizes Atom = 0x5f505
|
||||||
|
Slot Atom = 0x1ef04
|
||||||
|
Small Atom = 0x20605
|
||||||
|
Sortable Atom = 0x65108
|
||||||
|
Sorted Atom = 0x33706
|
||||||
|
Source Atom = 0x37806
|
||||||
|
Spacer Atom = 0x43706
|
||||||
|
Span Atom = 0x9f04
|
||||||
|
Spellcheck Atom = 0x4740a
|
||||||
|
Src Atom = 0x5c003
|
||||||
|
Srcdoc Atom = 0x5c006
|
||||||
|
Srclang Atom = 0x5f907
|
||||||
|
Srcset Atom = 0x6f906
|
||||||
|
Start Atom = 0x3fa05
|
||||||
|
Step Atom = 0x58304
|
||||||
|
Strike Atom = 0xd206
|
||||||
|
Strong Atom = 0x6dd06
|
||||||
|
Style Atom = 0x6ff05
|
||||||
|
Sub Atom = 0x66d03
|
||||||
|
Summary Atom = 0x70407
|
||||||
|
Sup Atom = 0x70b03
|
||||||
|
Svg Atom = 0x70e03
|
||||||
|
System Atom = 0x71106
|
||||||
|
Tabindex Atom = 0x4be08
|
||||||
|
Table Atom = 0x59505
|
||||||
|
Target Atom = 0x2c406
|
||||||
|
Tbody Atom = 0x2705
|
||||||
|
Td Atom = 0x9202
|
||||||
|
Template Atom = 0x71408
|
||||||
|
Textarea Atom = 0x35208
|
||||||
|
Tfoot Atom = 0xf505
|
||||||
|
Th Atom = 0x15602
|
||||||
|
Thead Atom = 0x33005
|
||||||
|
Time Atom = 0x4204
|
||||||
|
Title Atom = 0x11005
|
||||||
|
Tr Atom = 0xcc02
|
||||||
|
Track Atom = 0x1ba05
|
||||||
|
Translate Atom = 0x1f209
|
||||||
|
Tt Atom = 0x6802
|
||||||
|
Type Atom = 0xd904
|
||||||
|
Typemustmatch Atom = 0x2900d
|
||||||
|
U Atom = 0xb01
|
||||||
|
Ul Atom = 0xa702
|
||||||
|
Updateviacache Atom = 0x460e
|
||||||
|
Usemap Atom = 0x59e06
|
||||||
|
Value Atom = 0x1505
|
||||||
|
Var Atom = 0x16d03
|
||||||
|
Video Atom = 0x2f105
|
||||||
|
Wbr Atom = 0x57c03
|
||||||
|
Width Atom = 0x64905
|
||||||
|
Workertype Atom = 0x71c0a
|
||||||
|
Wrap Atom = 0x72604
|
||||||
|
Xmp Atom = 0x12f03
|
||||||
|
)
|
||||||
|
|
||||||
|
const hash0 = 0x81cdf10e
|
||||||
|
|
||||||
|
const maxAtomLen = 25
|
||||||
|
|
||||||
|
var table = [1 << 9]Atom{
|
||||||
|
0x1: 0xe60a, // mediagroup
|
||||||
|
0x2: 0x2e404, // lang
|
||||||
|
0x4: 0x2c09, // accesskey
|
||||||
|
0x5: 0x8b08, // frameset
|
||||||
|
0x7: 0x63a08, // onselect
|
||||||
|
0x8: 0x71106, // system
|
||||||
|
0xa: 0x64905, // width
|
||||||
|
0xc: 0x2890b, // formenctype
|
||||||
|
0xd: 0x13702, // ol
|
||||||
|
0xe: 0x3970b, // oncuechange
|
||||||
|
0x10: 0x14b03, // bdo
|
||||||
|
0x11: 0x11505, // audio
|
||||||
|
0x12: 0x17a09, // draggable
|
||||||
|
0x14: 0x2f105, // video
|
||||||
|
0x15: 0x2b102, // mn
|
||||||
|
0x16: 0x38704, // menu
|
||||||
|
0x17: 0x2cf06, // poster
|
||||||
|
0x19: 0xf606, // footer
|
||||||
|
0x1a: 0x2a806, // method
|
||||||
|
0x1b: 0x2b808, // datetime
|
||||||
|
0x1c: 0x19507, // onabort
|
||||||
|
0x1d: 0x460e, // updateviacache
|
||||||
|
0x1e: 0xff05, // async
|
||||||
|
0x1f: 0x49d06, // onload
|
||||||
|
0x21: 0x11908, // oncancel
|
||||||
|
0x22: 0x62908, // onseeked
|
||||||
|
0x23: 0x30205, // image
|
||||||
|
0x24: 0x5d812, // onrejectionhandled
|
||||||
|
0x26: 0x17404, // link
|
||||||
|
0x27: 0x51d06, // output
|
||||||
|
0x28: 0x33104, // head
|
||||||
|
0x29: 0x4ff0c, // onmouseleave
|
||||||
|
0x2a: 0x57f07, // onpaste
|
||||||
|
0x2b: 0x5a409, // onplaying
|
||||||
|
0x2c: 0x1c407, // colspan
|
||||||
|
0x2f: 0x1bf05, // color
|
||||||
|
0x30: 0x5f504, // size
|
||||||
|
0x31: 0x2e80a, // http-equiv
|
||||||
|
0x33: 0x601, // i
|
||||||
|
0x34: 0x5590a, // onpagehide
|
||||||
|
0x35: 0x68c14, // onunhandledrejection
|
||||||
|
0x37: 0x42a07, // onerror
|
||||||
|
0x3a: 0x3b08, // basefont
|
||||||
|
0x3f: 0x1303, // nav
|
||||||
|
0x40: 0x17704, // kind
|
||||||
|
0x41: 0x35708, // readonly
|
||||||
|
0x42: 0x30806, // mglyph
|
||||||
|
0x44: 0xb202, // li
|
||||||
|
0x46: 0x2d506, // hidden
|
||||||
|
0x47: 0x70e03, // svg
|
||||||
|
0x48: 0x58304, // step
|
||||||
|
0x49: 0x23f09, // integrity
|
||||||
|
0x4a: 0x58606, // public
|
||||||
|
0x4c: 0x1ab03, // col
|
||||||
|
0x4d: 0x1870a, // blockquote
|
||||||
|
0x4e: 0x34f02, // h5
|
||||||
|
0x50: 0x5b908, // progress
|
||||||
|
0x51: 0x5f505, // sizes
|
||||||
|
0x52: 0x34502, // h4
|
||||||
|
0x56: 0x33005, // thead
|
||||||
|
0x57: 0xd607, // keytype
|
||||||
|
0x58: 0x5b70a, // onprogress
|
||||||
|
0x59: 0x44b09, // inputmode
|
||||||
|
0x5a: 0x3b109, // ondragend
|
||||||
|
0x5d: 0x3a205, // oncut
|
||||||
|
0x5e: 0x43706, // spacer
|
||||||
|
0x5f: 0x1ab08, // colgroup
|
||||||
|
0x62: 0x16502, // is
|
||||||
|
0x65: 0x3c02, // as
|
||||||
|
0x66: 0x54809, // onoffline
|
||||||
|
0x67: 0x33706, // sorted
|
||||||
|
0x69: 0x48d10, // onlanguagechange
|
||||||
|
0x6c: 0x43d0c, // onhashchange
|
||||||
|
0x6d: 0x9604, // name
|
||||||
|
0x6e: 0xf505, // tfoot
|
||||||
|
0x6f: 0x56104, // desc
|
||||||
|
0x70: 0x33d03, // max
|
||||||
|
0x72: 0x1ea06, // coords
|
||||||
|
0x73: 0x30d02, // h3
|
||||||
|
0x74: 0x6e70e, // onbeforeunload
|
||||||
|
0x75: 0x9c04, // rows
|
||||||
|
0x76: 0x63c06, // select
|
||||||
|
0x77: 0x9805, // meter
|
||||||
|
0x78: 0x38b06, // itemid
|
||||||
|
0x79: 0x53c0c, // onmousewheel
|
||||||
|
0x7a: 0x5c006, // srcdoc
|
||||||
|
0x7d: 0x1ba05, // track
|
||||||
|
0x7f: 0x31f08, // itemtype
|
||||||
|
0x82: 0xa402, // mo
|
||||||
|
0x83: 0x41b08, // onchange
|
||||||
|
0x84: 0x33107, // headers
|
||||||
|
0x85: 0x5cc0c, // onratechange
|
||||||
|
0x86: 0x60819, // onsecuritypolicyviolation
|
||||||
|
0x88: 0x4a508, // datalist
|
||||||
|
0x89: 0x4e80b, // onmousedown
|
||||||
|
0x8a: 0x1ef04, // slot
|
||||||
|
0x8b: 0x4b010, // onloadedmetadata
|
||||||
|
0x8c: 0x1a06, // accept
|
||||||
|
0x8d: 0x26806, // object
|
||||||
|
0x91: 0x6b30e, // onvolumechange
|
||||||
|
0x92: 0x2107, // charset
|
||||||
|
0x93: 0x27613, // onautocompleteerror
|
||||||
|
0x94: 0xc113, // allowpaymentrequest
|
||||||
|
0x95: 0x2804, // body
|
||||||
|
0x96: 0x10a07, // default
|
||||||
|
0x97: 0x63c08, // selected
|
||||||
|
0x98: 0x21e04, // face
|
||||||
|
0x99: 0x1e505, // shape
|
||||||
|
0x9b: 0x68408, // ontoggle
|
||||||
|
0x9e: 0x64b02, // dt
|
||||||
|
0x9f: 0xb604, // mark
|
||||||
|
0xa1: 0xb01, // u
|
||||||
|
0xa4: 0x6ab08, // onunload
|
||||||
|
0xa5: 0x5d04, // loop
|
||||||
|
0xa6: 0x16408, // disabled
|
||||||
|
0xaa: 0x42307, // onended
|
||||||
|
0xab: 0xb00a, // malignmark
|
||||||
|
0xad: 0x67b09, // onsuspend
|
||||||
|
0xae: 0x35105, // mtext
|
||||||
|
0xaf: 0x64f06, // onsort
|
||||||
|
0xb0: 0x19d08, // itemprop
|
||||||
|
0xb3: 0x67109, // itemscope
|
||||||
|
0xb4: 0x17305, // blink
|
||||||
|
0xb6: 0x3b106, // ondrag
|
||||||
|
0xb7: 0xa702, // ul
|
||||||
|
0xb8: 0x26e04, // form
|
||||||
|
0xb9: 0x12907, // sandbox
|
||||||
|
0xba: 0x8b05, // frame
|
||||||
|
0xbb: 0x1505, // value
|
||||||
|
0xbc: 0x66209, // onstorage
|
||||||
|
0xbf: 0xaa07, // acronym
|
||||||
|
0xc0: 0x19a02, // rt
|
||||||
|
0xc2: 0x202, // br
|
||||||
|
0xc3: 0x22608, // fieldset
|
||||||
|
0xc4: 0x2900d, // typemustmatch
|
||||||
|
0xc5: 0xa208, // nomodule
|
||||||
|
0xc6: 0x6c07, // noembed
|
||||||
|
0xc7: 0x69e0d, // onbeforeprint
|
||||||
|
0xc8: 0x19106, // button
|
||||||
|
0xc9: 0x2f507, // onclick
|
||||||
|
0xca: 0x70407, // summary
|
||||||
|
0xcd: 0xfb04, // ruby
|
||||||
|
0xce: 0x56405, // class
|
||||||
|
0xcf: 0x3f40b, // ondragstart
|
||||||
|
0xd0: 0x23107, // caption
|
||||||
|
0xd4: 0xdd0e, // allowusermedia
|
||||||
|
0xd5: 0x4cf0b, // onloadstart
|
||||||
|
0xd9: 0x16b03, // div
|
||||||
|
0xda: 0x4a904, // list
|
||||||
|
0xdb: 0x32e04, // math
|
||||||
|
0xdc: 0x44b05, // input
|
||||||
|
0xdf: 0x3ea0a, // ondragover
|
||||||
|
0xe0: 0x2de02, // h2
|
||||||
|
0xe2: 0x1b209, // plaintext
|
||||||
|
0xe4: 0x4f30c, // onmouseenter
|
||||||
|
0xe7: 0x47907, // checked
|
||||||
|
0xe8: 0x47003, // pre
|
||||||
|
0xea: 0x35f08, // multiple
|
||||||
|
0xeb: 0xba03, // bdi
|
||||||
|
0xec: 0x33d09, // maxlength
|
||||||
|
0xed: 0xcf01, // q
|
||||||
|
0xee: 0x61f0a, // onauxclick
|
||||||
|
0xf0: 0x57c03, // wbr
|
||||||
|
0xf2: 0x3b04, // base
|
||||||
|
0xf3: 0x6e306, // option
|
||||||
|
0xf5: 0x41310, // ondurationchange
|
||||||
|
0xf7: 0x8908, // noframes
|
||||||
|
0xf9: 0x40508, // dropzone
|
||||||
|
0xfb: 0x67505, // scope
|
||||||
|
0xfc: 0x8008, // reversed
|
||||||
|
0xfd: 0x3ba0b, // ondragenter
|
||||||
|
0xfe: 0x3fa05, // start
|
||||||
|
0xff: 0x12f03, // xmp
|
||||||
|
0x100: 0x5f907, // srclang
|
||||||
|
0x101: 0x30703, // img
|
||||||
|
0x104: 0x101, // b
|
||||||
|
0x105: 0x25403, // for
|
||||||
|
0x106: 0x10705, // aside
|
||||||
|
0x107: 0x44907, // oninput
|
||||||
|
0x108: 0x35604, // area
|
||||||
|
0x109: 0x2a40a, // formmethod
|
||||||
|
0x10a: 0x72604, // wrap
|
||||||
|
0x10c: 0x23c02, // rp
|
||||||
|
0x10d: 0x46b0a, // onkeypress
|
||||||
|
0x10e: 0x6802, // tt
|
||||||
|
0x110: 0x34702, // mi
|
||||||
|
0x111: 0x36705, // muted
|
||||||
|
0x112: 0xf303, // alt
|
||||||
|
0x113: 0x5c504, // code
|
||||||
|
0x114: 0x6e02, // em
|
||||||
|
0x115: 0x3c50a, // ondragexit
|
||||||
|
0x117: 0x9f04, // span
|
||||||
|
0x119: 0x6d708, // manifest
|
||||||
|
0x11a: 0x38708, // menuitem
|
||||||
|
0x11b: 0x58b07, // content
|
||||||
|
0x11d: 0x6c109, // onwaiting
|
||||||
|
0x11f: 0x4c609, // onloadend
|
||||||
|
0x121: 0x37e0d, // oncontextmenu
|
||||||
|
0x123: 0x56d06, // onblur
|
||||||
|
0x124: 0x3fc07, // article
|
||||||
|
0x125: 0x9303, // dir
|
||||||
|
0x126: 0xef04, // ping
|
||||||
|
0x127: 0x24c08, // required
|
||||||
|
0x128: 0x45509, // oninvalid
|
||||||
|
0x129: 0xb105, // align
|
||||||
|
0x12b: 0x58a04, // icon
|
||||||
|
0x12c: 0x64d02, // h6
|
||||||
|
0x12d: 0x1c404, // cols
|
||||||
|
0x12e: 0x22e0a, // figcaption
|
||||||
|
0x12f: 0x45e09, // onkeydown
|
||||||
|
0x130: 0x66b08, // onsubmit
|
||||||
|
0x131: 0x14d09, // oncanplay
|
||||||
|
0x132: 0x70b03, // sup
|
||||||
|
0x133: 0xc01, // p
|
||||||
|
0x135: 0x40a09, // onemptied
|
||||||
|
0x136: 0x39106, // oncopy
|
||||||
|
0x137: 0x19c04, // cite
|
||||||
|
0x138: 0x3a70a, // ondblclick
|
||||||
|
0x13a: 0x50b0b, // onmousemove
|
||||||
|
0x13c: 0x66d03, // sub
|
||||||
|
0x13d: 0x48703, // rel
|
||||||
|
0x13e: 0x5f08, // optgroup
|
||||||
|
0x142: 0x9c07, // rowspan
|
||||||
|
0x143: 0x37806, // source
|
||||||
|
0x144: 0x21608, // noscript
|
||||||
|
0x145: 0x1a304, // open
|
||||||
|
0x146: 0x20403, // ins
|
||||||
|
0x147: 0x2540d, // foreignObject
|
||||||
|
0x148: 0x5ad0a, // onpopstate
|
||||||
|
0x14a: 0x28d07, // enctype
|
||||||
|
0x14b: 0x2760e, // onautocomplete
|
||||||
|
0x14c: 0x35208, // textarea
|
||||||
|
0x14e: 0x2780c, // autocomplete
|
||||||
|
0x14f: 0x15702, // hr
|
||||||
|
0x150: 0x1de08, // controls
|
||||||
|
0x151: 0x10902, // id
|
||||||
|
0x153: 0x2360c, // onafterprint
|
||||||
|
0x155: 0x2610d, // foreignobject
|
||||||
|
0x156: 0x32707, // marquee
|
||||||
|
0x157: 0x59a07, // onpause
|
||||||
|
0x158: 0x5e602, // dl
|
||||||
|
0x159: 0x5206, // height
|
||||||
|
0x15a: 0x34703, // min
|
||||||
|
0x15b: 0x9307, // dirname
|
||||||
|
0x15c: 0x1f209, // translate
|
||||||
|
0x15d: 0x5604, // html
|
||||||
|
0x15e: 0x34709, // minlength
|
||||||
|
0x15f: 0x48607, // preload
|
||||||
|
0x160: 0x71408, // template
|
||||||
|
0x161: 0x3df0b, // ondragleave
|
||||||
|
0x162: 0x3a02, // rb
|
||||||
|
0x164: 0x5c003, // src
|
||||||
|
0x165: 0x6dd06, // strong
|
||||||
|
0x167: 0x7804, // samp
|
||||||
|
0x168: 0x6f307, // address
|
||||||
|
0x169: 0x55108, // ononline
|
||||||
|
0x16b: 0x1310b, // placeholder
|
||||||
|
0x16c: 0x2c406, // target
|
||||||
|
0x16d: 0x20605, // small
|
||||||
|
0x16e: 0x6ca07, // onwheel
|
||||||
|
0x16f: 0x1c90a, // annotation
|
||||||
|
0x170: 0x4740a, // spellcheck
|
||||||
|
0x171: 0x7207, // details
|
||||||
|
0x172: 0x10306, // canvas
|
||||||
|
0x173: 0x12109, // autofocus
|
||||||
|
0x174: 0xc05, // param
|
||||||
|
0x176: 0x46308, // download
|
||||||
|
0x177: 0x45203, // del
|
||||||
|
0x178: 0x36c07, // onclose
|
||||||
|
0x179: 0xb903, // kbd
|
||||||
|
0x17a: 0x31906, // applet
|
||||||
|
0x17b: 0x2e004, // href
|
||||||
|
0x17c: 0x5f108, // onresize
|
||||||
|
0x17e: 0x49d0c, // onloadeddata
|
||||||
|
0x180: 0xcc02, // tr
|
||||||
|
0x181: 0x2c00a, // formtarget
|
||||||
|
0x182: 0x11005, // title
|
||||||
|
0x183: 0x6ff05, // style
|
||||||
|
0x184: 0xd206, // strike
|
||||||
|
0x185: 0x59e06, // usemap
|
||||||
|
0x186: 0x2fc06, // iframe
|
||||||
|
0x187: 0x1004, // main
|
||||||
|
0x189: 0x7b07, // picture
|
||||||
|
0x18c: 0x31605, // ismap
|
||||||
|
0x18e: 0x4a504, // data
|
||||||
|
0x18f: 0x5905, // label
|
||||||
|
0x191: 0x3d10e, // referrerpolicy
|
||||||
|
0x192: 0x15602, // th
|
||||||
|
0x194: 0x53606, // prompt
|
||||||
|
0x195: 0x56807, // section
|
||||||
|
0x197: 0x6d107, // optimum
|
||||||
|
0x198: 0x2db04, // high
|
||||||
|
0x199: 0x15c02, // h1
|
||||||
|
0x19a: 0x65909, // onstalled
|
||||||
|
0x19b: 0x16d03, // var
|
||||||
|
0x19c: 0x4204, // time
|
||||||
|
0x19e: 0x67402, // ms
|
||||||
|
0x19f: 0x33106, // header
|
||||||
|
0x1a0: 0x4da09, // onmessage
|
||||||
|
0x1a1: 0x1a605, // nonce
|
||||||
|
0x1a2: 0x26e0a, // formaction
|
||||||
|
0x1a3: 0x22006, // center
|
||||||
|
0x1a4: 0x3704, // nobr
|
||||||
|
0x1a5: 0x59505, // table
|
||||||
|
0x1a6: 0x4a907, // listing
|
||||||
|
0x1a7: 0x18106, // legend
|
||||||
|
0x1a9: 0x29b09, // challenge
|
||||||
|
0x1aa: 0x24806, // figure
|
||||||
|
0x1ab: 0xe605, // media
|
||||||
|
0x1ae: 0xd904, // type
|
||||||
|
0x1af: 0x3f04, // font
|
||||||
|
0x1b0: 0x4da0e, // onmessageerror
|
||||||
|
0x1b1: 0x37108, // seamless
|
||||||
|
0x1b2: 0x8703, // dfn
|
||||||
|
0x1b3: 0x5c705, // defer
|
||||||
|
0x1b4: 0xc303, // low
|
||||||
|
0x1b5: 0x19a03, // rtc
|
||||||
|
0x1b6: 0x5230b, // onmouseover
|
||||||
|
0x1b7: 0x2b20a, // novalidate
|
||||||
|
0x1b8: 0x71c0a, // workertype
|
||||||
|
0x1ba: 0x3cd07, // itemref
|
||||||
|
0x1bd: 0x1, // a
|
||||||
|
0x1be: 0x31803, // map
|
||||||
|
0x1bf: 0x400c, // ontimeupdate
|
||||||
|
0x1c0: 0x15e07, // bgsound
|
||||||
|
0x1c1: 0x3206, // keygen
|
||||||
|
0x1c2: 0x2705, // tbody
|
||||||
|
0x1c5: 0x64406, // onshow
|
||||||
|
0x1c7: 0x2501, // s
|
||||||
|
0x1c8: 0x6607, // pattern
|
||||||
|
0x1cc: 0x14d10, // oncanplaythrough
|
||||||
|
0x1ce: 0x2d702, // dd
|
||||||
|
0x1cf: 0x6f906, // srcset
|
||||||
|
0x1d0: 0x17003, // big
|
||||||
|
0x1d2: 0x65108, // sortable
|
||||||
|
0x1d3: 0x48007, // onkeyup
|
||||||
|
0x1d5: 0x5a406, // onplay
|
||||||
|
0x1d7: 0x4b804, // meta
|
||||||
|
0x1d8: 0x40306, // ondrop
|
||||||
|
0x1da: 0x60008, // onscroll
|
||||||
|
0x1db: 0x1fb0b, // crossorigin
|
||||||
|
0x1dc: 0x5730a, // onpageshow
|
||||||
|
0x1dd: 0x4, // abbr
|
||||||
|
0x1de: 0x9202, // td
|
||||||
|
0x1df: 0x58b0f, // contenteditable
|
||||||
|
0x1e0: 0x27206, // action
|
||||||
|
0x1e1: 0x1400b, // playsinline
|
||||||
|
0x1e2: 0x43107, // onfocus
|
||||||
|
0x1e3: 0x2e008, // hreflang
|
||||||
|
0x1e5: 0x5160a, // onmouseout
|
||||||
|
0x1e6: 0x5ea07, // onreset
|
||||||
|
0x1e7: 0x13c08, // autoplay
|
||||||
|
0x1e8: 0x63109, // onseeking
|
||||||
|
0x1ea: 0x67506, // scoped
|
||||||
|
0x1ec: 0x30a, // radiogroup
|
||||||
|
0x1ee: 0x3800b, // contextmenu
|
||||||
|
0x1ef: 0x52e09, // onmouseup
|
||||||
|
0x1f1: 0x2ca06, // hgroup
|
||||||
|
0x1f2: 0x2080f, // allowfullscreen
|
||||||
|
0x1f3: 0x4be08, // tabindex
|
||||||
|
0x1f6: 0x30f07, // isindex
|
||||||
|
0x1f7: 0x1a0e, // accept-charset
|
||||||
|
0x1f8: 0x2ae0e, // formnovalidate
|
||||||
|
0x1fb: 0x1c90e, // annotation-xml
|
||||||
|
0x1fc: 0x6e05, // embed
|
||||||
|
0x1fd: 0x21806, // script
|
||||||
|
0x1fe: 0xbb06, // dialog
|
||||||
|
0x1ff: 0x1d707, // command
|
||||||
|
}
|
||||||
|
|
||||||
|
const atomText = "abbradiogrouparamainavalueaccept-charsetbodyaccesskeygenobrb" +
|
||||||
|
"asefontimeupdateviacacheightmlabelooptgroupatternoembedetail" +
|
||||||
|
"sampictureversedfnoframesetdirnameterowspanomoduleacronymali" +
|
||||||
|
"gnmarkbdialogallowpaymentrequestrikeytypeallowusermediagroup" +
|
||||||
|
"ingaltfooterubyasyncanvasidefaultitleaudioncancelautofocusan" +
|
||||||
|
"dboxmplaceholderautoplaysinlinebdoncanplaythrough1bgsoundisa" +
|
||||||
|
"bledivarbigblinkindraggablegendblockquotebuttonabortcitempro" +
|
||||||
|
"penoncecolgrouplaintextrackcolorcolspannotation-xmlcommandco" +
|
||||||
|
"ntrolshapecoordslotranslatecrossoriginsmallowfullscreenoscri" +
|
||||||
|
"ptfacenterfieldsetfigcaptionafterprintegrityfigurequiredfore" +
|
||||||
|
"ignObjectforeignobjectformactionautocompleteerrorformenctype" +
|
||||||
|
"mustmatchallengeformmethodformnovalidatetimeformtargethgroup" +
|
||||||
|
"osterhiddenhigh2hreflanghttp-equivideonclickiframeimageimgly" +
|
||||||
|
"ph3isindexismappletitemtypemarqueematheadersortedmaxlength4m" +
|
||||||
|
"inlength5mtextareadonlymultiplemutedoncloseamlessourceoncont" +
|
||||||
|
"extmenuitemidoncopyoncuechangeoncutondblclickondragendondrag" +
|
||||||
|
"enterondragexitemreferrerpolicyondragleaveondragoverondragst" +
|
||||||
|
"articleondropzonemptiedondurationchangeonendedonerroronfocus" +
|
||||||
|
"paceronhashchangeoninputmodeloninvalidonkeydownloadonkeypres" +
|
||||||
|
"spellcheckedonkeyupreloadonlanguagechangeonloadeddatalisting" +
|
||||||
|
"onloadedmetadatabindexonloadendonloadstartonmessageerroronmo" +
|
||||||
|
"usedownonmouseenteronmouseleaveonmousemoveonmouseoutputonmou" +
|
||||||
|
"seoveronmouseupromptonmousewheelonofflineononlineonpagehides" +
|
||||||
|
"classectionbluronpageshowbronpastepublicontenteditableonpaus" +
|
||||||
|
"emaponplayingonpopstateonprogressrcdocodeferonratechangeonre" +
|
||||||
|
"jectionhandledonresetonresizesrclangonscrollonsecuritypolicy" +
|
||||||
|
"violationauxclickonseekedonseekingonselectedonshowidth6onsor" +
|
||||||
|
"tableonstalledonstorageonsubmitemscopedonsuspendontoggleonun" +
|
||||||
|
"handledrejectionbeforeprintonunloadonvolumechangeonwaitingon" +
|
||||||
|
"wheeloptimumanifestrongoptionbeforeunloaddressrcsetstylesumm" +
|
||||||
|
"arysupsvgsystemplateworkertypewrap"
|
257
vendor/golang.org/x/net/html/charset/charset.go
generated
vendored
Normal file
257
vendor/golang.org/x/net/html/charset/charset.go
generated
vendored
Normal file
@ -0,0 +1,257 @@
|
|||||||
|
// Copyright 2013 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package charset provides common text encodings for HTML documents.
|
||||||
|
//
|
||||||
|
// The mapping from encoding labels to encodings is defined at
|
||||||
|
// https://encoding.spec.whatwg.org/.
|
||||||
|
package charset // import "golang.org/x/net/html/charset"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime"
|
||||||
|
"strings"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"golang.org/x/net/html"
|
||||||
|
"golang.org/x/text/encoding"
|
||||||
|
"golang.org/x/text/encoding/charmap"
|
||||||
|
"golang.org/x/text/encoding/htmlindex"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Lookup returns the encoding with the specified label, and its canonical
|
||||||
|
// name. It returns nil and the empty string if label is not one of the
|
||||||
|
// standard encodings for HTML. Matching is case-insensitive and ignores
|
||||||
|
// leading and trailing whitespace. Encoders will use HTML escape sequences for
|
||||||
|
// runes that are not supported by the character set.
|
||||||
|
func Lookup(label string) (e encoding.Encoding, name string) {
|
||||||
|
e, err := htmlindex.Get(label)
|
||||||
|
if err != nil {
|
||||||
|
return nil, ""
|
||||||
|
}
|
||||||
|
name, _ = htmlindex.Name(e)
|
||||||
|
return &htmlEncoding{e}, name
|
||||||
|
}
|
||||||
|
|
||||||
|
type htmlEncoding struct{ encoding.Encoding }
|
||||||
|
|
||||||
|
func (h *htmlEncoding) NewEncoder() *encoding.Encoder {
|
||||||
|
// HTML requires a non-terminating legacy encoder. We use HTML escapes to
|
||||||
|
// substitute unsupported code points.
|
||||||
|
return encoding.HTMLEscapeUnsupported(h.Encoding.NewEncoder())
|
||||||
|
}
|
||||||
|
|
||||||
|
// DetermineEncoding determines the encoding of an HTML document by examining
|
||||||
|
// up to the first 1024 bytes of content and the declared Content-Type.
|
||||||
|
//
|
||||||
|
// See http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#determining-the-character-encoding
|
||||||
|
func DetermineEncoding(content []byte, contentType string) (e encoding.Encoding, name string, certain bool) {
|
||||||
|
if len(content) > 1024 {
|
||||||
|
content = content[:1024]
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, b := range boms {
|
||||||
|
if bytes.HasPrefix(content, b.bom) {
|
||||||
|
e, name = Lookup(b.enc)
|
||||||
|
return e, name, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, params, err := mime.ParseMediaType(contentType); err == nil {
|
||||||
|
if cs, ok := params["charset"]; ok {
|
||||||
|
if e, name = Lookup(cs); e != nil {
|
||||||
|
return e, name, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(content) > 0 {
|
||||||
|
e, name = prescan(content)
|
||||||
|
if e != nil {
|
||||||
|
return e, name, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to detect UTF-8.
|
||||||
|
// First eliminate any partial rune at the end.
|
||||||
|
for i := len(content) - 1; i >= 0 && i > len(content)-4; i-- {
|
||||||
|
b := content[i]
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if utf8.RuneStart(b) {
|
||||||
|
content = content[:i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hasHighBit := false
|
||||||
|
for _, c := range content {
|
||||||
|
if c >= 0x80 {
|
||||||
|
hasHighBit = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if hasHighBit && utf8.Valid(content) {
|
||||||
|
return encoding.Nop, "utf-8", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: change default depending on user's locale?
|
||||||
|
return charmap.Windows1252, "windows-1252", false
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewReader returns an io.Reader that converts the content of r to UTF-8.
|
||||||
|
// It calls DetermineEncoding to find out what r's encoding is.
|
||||||
|
func NewReader(r io.Reader, contentType string) (io.Reader, error) {
|
||||||
|
preview := make([]byte, 1024)
|
||||||
|
n, err := io.ReadFull(r, preview)
|
||||||
|
switch {
|
||||||
|
case err == io.ErrUnexpectedEOF:
|
||||||
|
preview = preview[:n]
|
||||||
|
r = bytes.NewReader(preview)
|
||||||
|
case err != nil:
|
||||||
|
return nil, err
|
||||||
|
default:
|
||||||
|
r = io.MultiReader(bytes.NewReader(preview), r)
|
||||||
|
}
|
||||||
|
|
||||||
|
if e, _, _ := DetermineEncoding(preview, contentType); e != encoding.Nop {
|
||||||
|
r = transform.NewReader(r, e.NewDecoder())
|
||||||
|
}
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewReaderLabel returns a reader that converts from the specified charset to
|
||||||
|
// UTF-8. It uses Lookup to find the encoding that corresponds to label, and
|
||||||
|
// returns an error if Lookup returns nil. It is suitable for use as
|
||||||
|
// encoding/xml.Decoder's CharsetReader function.
|
||||||
|
func NewReaderLabel(label string, input io.Reader) (io.Reader, error) {
|
||||||
|
e, _ := Lookup(label)
|
||||||
|
if e == nil {
|
||||||
|
return nil, fmt.Errorf("unsupported charset: %q", label)
|
||||||
|
}
|
||||||
|
return transform.NewReader(input, e.NewDecoder()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func prescan(content []byte) (e encoding.Encoding, name string) {
|
||||||
|
z := html.NewTokenizer(bytes.NewReader(content))
|
||||||
|
for {
|
||||||
|
switch z.Next() {
|
||||||
|
case html.ErrorToken:
|
||||||
|
return nil, ""
|
||||||
|
|
||||||
|
case html.StartTagToken, html.SelfClosingTagToken:
|
||||||
|
tagName, hasAttr := z.TagName()
|
||||||
|
if !bytes.Equal(tagName, []byte("meta")) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
attrList := make(map[string]bool)
|
||||||
|
gotPragma := false
|
||||||
|
|
||||||
|
const (
|
||||||
|
dontKnow = iota
|
||||||
|
doNeedPragma
|
||||||
|
doNotNeedPragma
|
||||||
|
)
|
||||||
|
needPragma := dontKnow
|
||||||
|
|
||||||
|
name = ""
|
||||||
|
e = nil
|
||||||
|
for hasAttr {
|
||||||
|
var key, val []byte
|
||||||
|
key, val, hasAttr = z.TagAttr()
|
||||||
|
ks := string(key)
|
||||||
|
if attrList[ks] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
attrList[ks] = true
|
||||||
|
for i, c := range val {
|
||||||
|
if 'A' <= c && c <= 'Z' {
|
||||||
|
val[i] = c + 0x20
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ks {
|
||||||
|
case "http-equiv":
|
||||||
|
if bytes.Equal(val, []byte("content-type")) {
|
||||||
|
gotPragma = true
|
||||||
|
}
|
||||||
|
|
||||||
|
case "content":
|
||||||
|
if e == nil {
|
||||||
|
name = fromMetaElement(string(val))
|
||||||
|
if name != "" {
|
||||||
|
e, name = Lookup(name)
|
||||||
|
if e != nil {
|
||||||
|
needPragma = doNeedPragma
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "charset":
|
||||||
|
e, name = Lookup(string(val))
|
||||||
|
needPragma = doNotNeedPragma
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if needPragma == dontKnow || needPragma == doNeedPragma && !gotPragma {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(name, "utf-16") {
|
||||||
|
name = "utf-8"
|
||||||
|
e = encoding.Nop
|
||||||
|
}
|
||||||
|
|
||||||
|
if e != nil {
|
||||||
|
return e, name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromMetaElement(s string) string {
|
||||||
|
for s != "" {
|
||||||
|
csLoc := strings.Index(s, "charset")
|
||||||
|
if csLoc == -1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
s = s[csLoc+len("charset"):]
|
||||||
|
s = strings.TrimLeft(s, " \t\n\f\r")
|
||||||
|
if !strings.HasPrefix(s, "=") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
s = s[1:]
|
||||||
|
s = strings.TrimLeft(s, " \t\n\f\r")
|
||||||
|
if s == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if q := s[0]; q == '"' || q == '\'' {
|
||||||
|
s = s[1:]
|
||||||
|
closeQuote := strings.IndexRune(s, rune(q))
|
||||||
|
if closeQuote == -1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return s[:closeQuote]
|
||||||
|
}
|
||||||
|
|
||||||
|
end := strings.IndexAny(s, "; \t\n\f\r")
|
||||||
|
if end == -1 {
|
||||||
|
end = len(s)
|
||||||
|
}
|
||||||
|
return s[:end]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var boms = []struct {
|
||||||
|
bom []byte
|
||||||
|
enc string
|
||||||
|
}{
|
||||||
|
{[]byte{0xfe, 0xff}, "utf-16be"},
|
||||||
|
{[]byte{0xff, 0xfe}, "utf-16le"},
|
||||||
|
{[]byte{0xef, 0xbb, 0xbf}, "utf-8"},
|
||||||
|
}
|
112
vendor/golang.org/x/net/html/const.go
generated
vendored
Normal file
112
vendor/golang.org/x/net/html/const.go
generated
vendored
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
// Copyright 2011 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package html
|
||||||
|
|
||||||
|
// Section 12.2.4.2 of the HTML5 specification says "The following elements
|
||||||
|
// have varying levels of special parsing rules".
|
||||||
|
// https://html.spec.whatwg.org/multipage/syntax.html#the-stack-of-open-elements
|
||||||
|
var isSpecialElementMap = map[string]bool{
|
||||||
|
"address": true,
|
||||||
|
"applet": true,
|
||||||
|
"area": true,
|
||||||
|
"article": true,
|
||||||
|
"aside": true,
|
||||||
|
"base": true,
|
||||||
|
"basefont": true,
|
||||||
|
"bgsound": true,
|
||||||
|
"blockquote": true,
|
||||||
|
"body": true,
|
||||||
|
"br": true,
|
||||||
|
"button": true,
|
||||||
|
"caption": true,
|
||||||
|
"center": true,
|
||||||
|
"col": true,
|
||||||
|
"colgroup": true,
|
||||||
|
"dd": true,
|
||||||
|
"details": true,
|
||||||
|
"dir": true,
|
||||||
|
"div": true,
|
||||||
|
"dl": true,
|
||||||
|
"dt": true,
|
||||||
|
"embed": true,
|
||||||
|
"fieldset": true,
|
||||||
|
"figcaption": true,
|
||||||
|
"figure": true,
|
||||||
|
"footer": true,
|
||||||
|
"form": true,
|
||||||
|
"frame": true,
|
||||||
|
"frameset": true,
|
||||||
|
"h1": true,
|
||||||
|
"h2": true,
|
||||||
|
"h3": true,
|
||||||
|
"h4": true,
|
||||||
|
"h5": true,
|
||||||
|
"h6": true,
|
||||||
|
"head": true,
|
||||||
|
"header": true,
|
||||||
|
"hgroup": true,
|
||||||
|
"hr": true,
|
||||||
|
"html": true,
|
||||||
|
"iframe": true,
|
||||||
|
"img": true,
|
||||||
|
"input": true,
|
||||||
|
"isindex": true, // The 'isindex' element has been removed, but keep it for backwards compatibility.
|
||||||
|
"keygen": true,
|
||||||
|
"li": true,
|
||||||
|
"link": true,
|
||||||
|
"listing": true,
|
||||||
|
"main": true,
|
||||||
|
"marquee": true,
|
||||||
|
"menu": true,
|
||||||
|
"meta": true,
|
||||||
|
"nav": true,
|
||||||
|
"noembed": true,
|
||||||
|
"noframes": true,
|
||||||
|
"noscript": true,
|
||||||
|
"object": true,
|
||||||
|
"ol": true,
|
||||||
|
"p": true,
|
||||||
|
"param": true,
|
||||||
|
"plaintext": true,
|
||||||
|
"pre": true,
|
||||||
|
"script": true,
|
||||||
|
"section": true,
|
||||||
|
"select": true,
|
||||||
|
"source": true,
|
||||||
|
"style": true,
|
||||||
|
"summary": true,
|
||||||
|
"table": true,
|
||||||
|
"tbody": true,
|
||||||
|
"td": true,
|
||||||
|
"template": true,
|
||||||
|
"textarea": true,
|
||||||
|
"tfoot": true,
|
||||||
|
"th": true,
|
||||||
|
"thead": true,
|
||||||
|
"title": true,
|
||||||
|
"tr": true,
|
||||||
|
"track": true,
|
||||||
|
"ul": true,
|
||||||
|
"wbr": true,
|
||||||
|
"xmp": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
func isSpecialElement(element *Node) bool {
|
||||||
|
switch element.Namespace {
|
||||||
|
case "", "html":
|
||||||
|
return isSpecialElementMap[element.Data]
|
||||||
|
case "math":
|
||||||
|
switch element.Data {
|
||||||
|
case "mi", "mo", "mn", "ms", "mtext", "annotation-xml":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
case "svg":
|
||||||
|
switch element.Data {
|
||||||
|
case "foreignObject", "desc", "title":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
106
vendor/golang.org/x/net/html/doc.go
generated
vendored
Normal file
106
vendor/golang.org/x/net/html/doc.go
generated
vendored
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
// Copyright 2010 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package html implements an HTML5-compliant tokenizer and parser.
|
||||||
|
|
||||||
|
Tokenization is done by creating a Tokenizer for an io.Reader r. It is the
|
||||||
|
caller's responsibility to ensure that r provides UTF-8 encoded HTML.
|
||||||
|
|
||||||
|
z := html.NewTokenizer(r)
|
||||||
|
|
||||||
|
Given a Tokenizer z, the HTML is tokenized by repeatedly calling z.Next(),
|
||||||
|
which parses the next token and returns its type, or an error:
|
||||||
|
|
||||||
|
for {
|
||||||
|
tt := z.Next()
|
||||||
|
if tt == html.ErrorToken {
|
||||||
|
// ...
|
||||||
|
return ...
|
||||||
|
}
|
||||||
|
// Process the current token.
|
||||||
|
}
|
||||||
|
|
||||||
|
There are two APIs for retrieving the current token. The high-level API is to
|
||||||
|
call Token; the low-level API is to call Text or TagName / TagAttr. Both APIs
|
||||||
|
allow optionally calling Raw after Next but before Token, Text, TagName, or
|
||||||
|
TagAttr. In EBNF notation, the valid call sequence per token is:
|
||||||
|
|
||||||
|
Next {Raw} [ Token | Text | TagName {TagAttr} ]
|
||||||
|
|
||||||
|
Token returns an independent data structure that completely describes a token.
|
||||||
|
Entities (such as "<") are unescaped, tag names and attribute keys are
|
||||||
|
lower-cased, and attributes are collected into a []Attribute. For example:
|
||||||
|
|
||||||
|
for {
|
||||||
|
if z.Next() == html.ErrorToken {
|
||||||
|
// Returning io.EOF indicates success.
|
||||||
|
return z.Err()
|
||||||
|
}
|
||||||
|
emitToken(z.Token())
|
||||||
|
}
|
||||||
|
|
||||||
|
The low-level API performs fewer allocations and copies, but the contents of
|
||||||
|
the []byte values returned by Text, TagName and TagAttr may change on the next
|
||||||
|
call to Next. For example, to extract an HTML page's anchor text:
|
||||||
|
|
||||||
|
depth := 0
|
||||||
|
for {
|
||||||
|
tt := z.Next()
|
||||||
|
switch tt {
|
||||||
|
case html.ErrorToken:
|
||||||
|
return z.Err()
|
||||||
|
case html.TextToken:
|
||||||
|
if depth > 0 {
|
||||||
|
// emitBytes should copy the []byte it receives,
|
||||||
|
// if it doesn't process it immediately.
|
||||||
|
emitBytes(z.Text())
|
||||||
|
}
|
||||||
|
case html.StartTagToken, html.EndTagToken:
|
||||||
|
tn, _ := z.TagName()
|
||||||
|
if len(tn) == 1 && tn[0] == 'a' {
|
||||||
|
if tt == html.StartTagToken {
|
||||||
|
depth++
|
||||||
|
} else {
|
||||||
|
depth--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Parsing is done by calling Parse with an io.Reader, which returns the root of
|
||||||
|
the parse tree (the document element) as a *Node. It is the caller's
|
||||||
|
responsibility to ensure that the Reader provides UTF-8 encoded HTML. For
|
||||||
|
example, to process each anchor node in depth-first order:
|
||||||
|
|
||||||
|
doc, err := html.Parse(r)
|
||||||
|
if err != nil {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
var f func(*html.Node)
|
||||||
|
f = func(n *html.Node) {
|
||||||
|
if n.Type == html.ElementNode && n.Data == "a" {
|
||||||
|
// Do something with n...
|
||||||
|
}
|
||||||
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||||
|
f(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f(doc)
|
||||||
|
|
||||||
|
The relevant specifications include:
|
||||||
|
https://html.spec.whatwg.org/multipage/syntax.html and
|
||||||
|
https://html.spec.whatwg.org/multipage/syntax.html#tokenization
|
||||||
|
*/
|
||||||
|
package html // import "golang.org/x/net/html"
|
||||||
|
|
||||||
|
// The tokenization algorithm implemented by this package is not a line-by-line
|
||||||
|
// transliteration of the relatively verbose state-machine in the WHATWG
|
||||||
|
// specification. A more direct approach is used instead, where the program
|
||||||
|
// counter implies the state, such as whether it is tokenizing a tag or a text
|
||||||
|
// node. Specification compliance is verified by checking expected and actual
|
||||||
|
// outputs over a test suite rather than aiming for algorithmic fidelity.
|
||||||
|
|
||||||
|
// TODO(nigeltao): Does a DOM API belong in this package or a separate one?
|
||||||
|
// TODO(nigeltao): How does parsing interact with a JavaScript engine?
|
156
vendor/golang.org/x/net/html/doctype.go
generated
vendored
Normal file
156
vendor/golang.org/x/net/html/doctype.go
generated
vendored
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
// Copyright 2011 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package html
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// parseDoctype parses the data from a DoctypeToken into a name,
|
||||||
|
// public identifier, and system identifier. It returns a Node whose Type
|
||||||
|
// is DoctypeNode, whose Data is the name, and which has attributes
|
||||||
|
// named "system" and "public" for the two identifiers if they were present.
|
||||||
|
// quirks is whether the document should be parsed in "quirks mode".
|
||||||
|
func parseDoctype(s string) (n *Node, quirks bool) {
|
||||||
|
n = &Node{Type: DoctypeNode}
|
||||||
|
|
||||||
|
// Find the name.
|
||||||
|
space := strings.IndexAny(s, whitespace)
|
||||||
|
if space == -1 {
|
||||||
|
space = len(s)
|
||||||
|
}
|
||||||
|
n.Data = s[:space]
|
||||||
|
// The comparison to "html" is case-sensitive.
|
||||||
|
if n.Data != "html" {
|
||||||
|
quirks = true
|
||||||
|
}
|
||||||
|
n.Data = strings.ToLower(n.Data)
|
||||||
|
s = strings.TrimLeft(s[space:], whitespace)
|
||||||
|
|
||||||
|
if len(s) < 6 {
|
||||||
|
// It can't start with "PUBLIC" or "SYSTEM".
|
||||||
|
// Ignore the rest of the string.
|
||||||
|
return n, quirks || s != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
key := strings.ToLower(s[:6])
|
||||||
|
s = s[6:]
|
||||||
|
for key == "public" || key == "system" {
|
||||||
|
s = strings.TrimLeft(s, whitespace)
|
||||||
|
if s == "" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
quote := s[0]
|
||||||
|
if quote != '"' && quote != '\'' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
s = s[1:]
|
||||||
|
q := strings.IndexRune(s, rune(quote))
|
||||||
|
var id string
|
||||||
|
if q == -1 {
|
||||||
|
id = s
|
||||||
|
s = ""
|
||||||
|
} else {
|
||||||
|
id = s[:q]
|
||||||
|
s = s[q+1:]
|
||||||
|
}
|
||||||
|
n.Attr = append(n.Attr, Attribute{Key: key, Val: id})
|
||||||
|
if key == "public" {
|
||||||
|
key = "system"
|
||||||
|
} else {
|
||||||
|
key = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if key != "" || s != "" {
|
||||||
|
quirks = true
|
||||||
|
} else if len(n.Attr) > 0 {
|
||||||
|
if n.Attr[0].Key == "public" {
|
||||||
|
public := strings.ToLower(n.Attr[0].Val)
|
||||||
|
switch public {
|
||||||
|
case "-//w3o//dtd w3 html strict 3.0//en//", "-/w3d/dtd html 4.0 transitional/en", "html":
|
||||||
|
quirks = true
|
||||||
|
default:
|
||||||
|
for _, q := range quirkyIDs {
|
||||||
|
if strings.HasPrefix(public, q) {
|
||||||
|
quirks = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// The following two public IDs only cause quirks mode if there is no system ID.
|
||||||
|
if len(n.Attr) == 1 && (strings.HasPrefix(public, "-//w3c//dtd html 4.01 frameset//") ||
|
||||||
|
strings.HasPrefix(public, "-//w3c//dtd html 4.01 transitional//")) {
|
||||||
|
quirks = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if lastAttr := n.Attr[len(n.Attr)-1]; lastAttr.Key == "system" &&
|
||||||
|
strings.ToLower(lastAttr.Val) == "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd" {
|
||||||
|
quirks = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return n, quirks
|
||||||
|
}
|
||||||
|
|
||||||
|
// quirkyIDs is a list of public doctype identifiers that cause a document
|
||||||
|
// to be interpreted in quirks mode. The identifiers should be in lower case.
|
||||||
|
var quirkyIDs = []string{
|
||||||
|
"+//silmaril//dtd html pro v0r11 19970101//",
|
||||||
|
"-//advasoft ltd//dtd html 3.0 aswedit + extensions//",
|
||||||
|
"-//as//dtd html 3.0 aswedit + extensions//",
|
||||||
|
"-//ietf//dtd html 2.0 level 1//",
|
||||||
|
"-//ietf//dtd html 2.0 level 2//",
|
||||||
|
"-//ietf//dtd html 2.0 strict level 1//",
|
||||||
|
"-//ietf//dtd html 2.0 strict level 2//",
|
||||||
|
"-//ietf//dtd html 2.0 strict//",
|
||||||
|
"-//ietf//dtd html 2.0//",
|
||||||
|
"-//ietf//dtd html 2.1e//",
|
||||||
|
"-//ietf//dtd html 3.0//",
|
||||||
|
"-//ietf//dtd html 3.2 final//",
|
||||||
|
"-//ietf//dtd html 3.2//",
|
||||||
|
"-//ietf//dtd html 3//",
|
||||||
|
"-//ietf//dtd html level 0//",
|
||||||
|
"-//ietf//dtd html level 1//",
|
||||||
|
"-//ietf//dtd html level 2//",
|
||||||
|
"-//ietf//dtd html level 3//",
|
||||||
|
"-//ietf//dtd html strict level 0//",
|
||||||
|
"-//ietf//dtd html strict level 1//",
|
||||||
|
"-//ietf//dtd html strict level 2//",
|
||||||
|
"-//ietf//dtd html strict level 3//",
|
||||||
|
"-//ietf//dtd html strict//",
|
||||||
|
"-//ietf//dtd html//",
|
||||||
|
"-//metrius//dtd metrius presentational//",
|
||||||
|
"-//microsoft//dtd internet explorer 2.0 html strict//",
|
||||||
|
"-//microsoft//dtd internet explorer 2.0 html//",
|
||||||
|
"-//microsoft//dtd internet explorer 2.0 tables//",
|
||||||
|
"-//microsoft//dtd internet explorer 3.0 html strict//",
|
||||||
|
"-//microsoft//dtd internet explorer 3.0 html//",
|
||||||
|
"-//microsoft//dtd internet explorer 3.0 tables//",
|
||||||
|
"-//netscape comm. corp.//dtd html//",
|
||||||
|
"-//netscape comm. corp.//dtd strict html//",
|
||||||
|
"-//o'reilly and associates//dtd html 2.0//",
|
||||||
|
"-//o'reilly and associates//dtd html extended 1.0//",
|
||||||
|
"-//o'reilly and associates//dtd html extended relaxed 1.0//",
|
||||||
|
"-//softquad software//dtd hotmetal pro 6.0::19990601::extensions to html 4.0//",
|
||||||
|
"-//softquad//dtd hotmetal pro 4.0::19971010::extensions to html 4.0//",
|
||||||
|
"-//spyglass//dtd html 2.0 extended//",
|
||||||
|
"-//sq//dtd html 2.0 hotmetal + extensions//",
|
||||||
|
"-//sun microsystems corp.//dtd hotjava html//",
|
||||||
|
"-//sun microsystems corp.//dtd hotjava strict html//",
|
||||||
|
"-//w3c//dtd html 3 1995-03-24//",
|
||||||
|
"-//w3c//dtd html 3.2 draft//",
|
||||||
|
"-//w3c//dtd html 3.2 final//",
|
||||||
|
"-//w3c//dtd html 3.2//",
|
||||||
|
"-//w3c//dtd html 3.2s draft//",
|
||||||
|
"-//w3c//dtd html 4.0 frameset//",
|
||||||
|
"-//w3c//dtd html 4.0 transitional//",
|
||||||
|
"-//w3c//dtd html experimental 19960712//",
|
||||||
|
"-//w3c//dtd html experimental 970421//",
|
||||||
|
"-//w3c//dtd w3 html//",
|
||||||
|
"-//w3o//dtd w3 html 3.0//",
|
||||||
|
"-//webtechs//dtd mozilla html 2.0//",
|
||||||
|
"-//webtechs//dtd mozilla html//",
|
||||||
|
}
|
2253
vendor/golang.org/x/net/html/entity.go
generated
vendored
Normal file
2253
vendor/golang.org/x/net/html/entity.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
258
vendor/golang.org/x/net/html/escape.go
generated
vendored
Normal file
258
vendor/golang.org/x/net/html/escape.go
generated
vendored
Normal file
@ -0,0 +1,258 @@
|
|||||||
|
// Copyright 2010 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package html
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
// These replacements permit compatibility with old numeric entities that
|
||||||
|
// assumed Windows-1252 encoding.
|
||||||
|
// https://html.spec.whatwg.org/multipage/syntax.html#consume-a-character-reference
|
||||||
|
var replacementTable = [...]rune{
|
||||||
|
'\u20AC', // First entry is what 0x80 should be replaced with.
|
||||||
|
'\u0081',
|
||||||
|
'\u201A',
|
||||||
|
'\u0192',
|
||||||
|
'\u201E',
|
||||||
|
'\u2026',
|
||||||
|
'\u2020',
|
||||||
|
'\u2021',
|
||||||
|
'\u02C6',
|
||||||
|
'\u2030',
|
||||||
|
'\u0160',
|
||||||
|
'\u2039',
|
||||||
|
'\u0152',
|
||||||
|
'\u008D',
|
||||||
|
'\u017D',
|
||||||
|
'\u008F',
|
||||||
|
'\u0090',
|
||||||
|
'\u2018',
|
||||||
|
'\u2019',
|
||||||
|
'\u201C',
|
||||||
|
'\u201D',
|
||||||
|
'\u2022',
|
||||||
|
'\u2013',
|
||||||
|
'\u2014',
|
||||||
|
'\u02DC',
|
||||||
|
'\u2122',
|
||||||
|
'\u0161',
|
||||||
|
'\u203A',
|
||||||
|
'\u0153',
|
||||||
|
'\u009D',
|
||||||
|
'\u017E',
|
||||||
|
'\u0178', // Last entry is 0x9F.
|
||||||
|
// 0x00->'\uFFFD' is handled programmatically.
|
||||||
|
// 0x0D->'\u000D' is a no-op.
|
||||||
|
}
|
||||||
|
|
||||||
|
// unescapeEntity reads an entity like "<" from b[src:] and writes the
|
||||||
|
// corresponding "<" to b[dst:], returning the incremented dst and src cursors.
|
||||||
|
// Precondition: b[src] == '&' && dst <= src.
|
||||||
|
// attribute should be true if parsing an attribute value.
|
||||||
|
func unescapeEntity(b []byte, dst, src int, attribute bool) (dst1, src1 int) {
|
||||||
|
// https://html.spec.whatwg.org/multipage/syntax.html#consume-a-character-reference
|
||||||
|
|
||||||
|
// i starts at 1 because we already know that s[0] == '&'.
|
||||||
|
i, s := 1, b[src:]
|
||||||
|
|
||||||
|
if len(s) <= 1 {
|
||||||
|
b[dst] = b[src]
|
||||||
|
return dst + 1, src + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if s[i] == '#' {
|
||||||
|
if len(s) <= 3 { // We need to have at least "&#.".
|
||||||
|
b[dst] = b[src]
|
||||||
|
return dst + 1, src + 1
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
c := s[i]
|
||||||
|
hex := false
|
||||||
|
if c == 'x' || c == 'X' {
|
||||||
|
hex = true
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
x := '\x00'
|
||||||
|
for i < len(s) {
|
||||||
|
c = s[i]
|
||||||
|
i++
|
||||||
|
if hex {
|
||||||
|
if '0' <= c && c <= '9' {
|
||||||
|
x = 16*x + rune(c) - '0'
|
||||||
|
continue
|
||||||
|
} else if 'a' <= c && c <= 'f' {
|
||||||
|
x = 16*x + rune(c) - 'a' + 10
|
||||||
|
continue
|
||||||
|
} else if 'A' <= c && c <= 'F' {
|
||||||
|
x = 16*x + rune(c) - 'A' + 10
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else if '0' <= c && c <= '9' {
|
||||||
|
x = 10*x + rune(c) - '0'
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if c != ';' {
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if i <= 3 { // No characters matched.
|
||||||
|
b[dst] = b[src]
|
||||||
|
return dst + 1, src + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if 0x80 <= x && x <= 0x9F {
|
||||||
|
// Replace characters from Windows-1252 with UTF-8 equivalents.
|
||||||
|
x = replacementTable[x-0x80]
|
||||||
|
} else if x == 0 || (0xD800 <= x && x <= 0xDFFF) || x > 0x10FFFF {
|
||||||
|
// Replace invalid characters with the replacement character.
|
||||||
|
x = '\uFFFD'
|
||||||
|
}
|
||||||
|
|
||||||
|
return dst + utf8.EncodeRune(b[dst:], x), src + i
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consume the maximum number of characters possible, with the
|
||||||
|
// consumed characters matching one of the named references.
|
||||||
|
|
||||||
|
for i < len(s) {
|
||||||
|
c := s[i]
|
||||||
|
i++
|
||||||
|
// Lower-cased characters are more common in entities, so we check for them first.
|
||||||
|
if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if c != ';' {
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
entityName := string(s[1:i])
|
||||||
|
if entityName == "" {
|
||||||
|
// No-op.
|
||||||
|
} else if attribute && entityName[len(entityName)-1] != ';' && len(s) > i && s[i] == '=' {
|
||||||
|
// No-op.
|
||||||
|
} else if x := entity[entityName]; x != 0 {
|
||||||
|
return dst + utf8.EncodeRune(b[dst:], x), src + i
|
||||||
|
} else if x := entity2[entityName]; x[0] != 0 {
|
||||||
|
dst1 := dst + utf8.EncodeRune(b[dst:], x[0])
|
||||||
|
return dst1 + utf8.EncodeRune(b[dst1:], x[1]), src + i
|
||||||
|
} else if !attribute {
|
||||||
|
maxLen := len(entityName) - 1
|
||||||
|
if maxLen > longestEntityWithoutSemicolon {
|
||||||
|
maxLen = longestEntityWithoutSemicolon
|
||||||
|
}
|
||||||
|
for j := maxLen; j > 1; j-- {
|
||||||
|
if x := entity[entityName[:j]]; x != 0 {
|
||||||
|
return dst + utf8.EncodeRune(b[dst:], x), src + j + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dst1, src1 = dst+i, src+i
|
||||||
|
copy(b[dst:dst1], b[src:src1])
|
||||||
|
return dst1, src1
|
||||||
|
}
|
||||||
|
|
||||||
|
// unescape unescapes b's entities in-place, so that "a<b" becomes "a<b".
|
||||||
|
// attribute should be true if parsing an attribute value.
|
||||||
|
func unescape(b []byte, attribute bool) []byte {
|
||||||
|
for i, c := range b {
|
||||||
|
if c == '&' {
|
||||||
|
dst, src := unescapeEntity(b, i, i, attribute)
|
||||||
|
for src < len(b) {
|
||||||
|
c := b[src]
|
||||||
|
if c == '&' {
|
||||||
|
dst, src = unescapeEntity(b, dst, src, attribute)
|
||||||
|
} else {
|
||||||
|
b[dst] = c
|
||||||
|
dst, src = dst+1, src+1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b[0:dst]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// lower lower-cases the A-Z bytes in b in-place, so that "aBc" becomes "abc".
|
||||||
|
func lower(b []byte) []byte {
|
||||||
|
for i, c := range b {
|
||||||
|
if 'A' <= c && c <= 'Z' {
|
||||||
|
b[i] = c + 'a' - 'A'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
const escapedChars = "&'<>\"\r"
|
||||||
|
|
||||||
|
func escape(w writer, s string) error {
|
||||||
|
i := strings.IndexAny(s, escapedChars)
|
||||||
|
for i != -1 {
|
||||||
|
if _, err := w.WriteString(s[:i]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var esc string
|
||||||
|
switch s[i] {
|
||||||
|
case '&':
|
||||||
|
esc = "&"
|
||||||
|
case '\'':
|
||||||
|
// "'" is shorter than "'" and apos was not in HTML until HTML5.
|
||||||
|
esc = "'"
|
||||||
|
case '<':
|
||||||
|
esc = "<"
|
||||||
|
case '>':
|
||||||
|
esc = ">"
|
||||||
|
case '"':
|
||||||
|
// """ is shorter than """.
|
||||||
|
esc = """
|
||||||
|
case '\r':
|
||||||
|
esc = " "
|
||||||
|
default:
|
||||||
|
panic("unrecognized escape character")
|
||||||
|
}
|
||||||
|
s = s[i+1:]
|
||||||
|
if _, err := w.WriteString(esc); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i = strings.IndexAny(s, escapedChars)
|
||||||
|
}
|
||||||
|
_, err := w.WriteString(s)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// EscapeString escapes special characters like "<" to become "<". It
|
||||||
|
// escapes only five such characters: <, >, &, ' and ".
|
||||||
|
// UnescapeString(EscapeString(s)) == s always holds, but the converse isn't
|
||||||
|
// always true.
|
||||||
|
func EscapeString(s string) string {
|
||||||
|
if strings.IndexAny(s, escapedChars) == -1 {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
var buf bytes.Buffer
|
||||||
|
escape(&buf, s)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnescapeString unescapes entities like "<" to become "<". It unescapes a
|
||||||
|
// larger range of entities than EscapeString escapes. For example, "á"
|
||||||
|
// unescapes to "á", as does "á" and "&xE1;".
|
||||||
|
// UnescapeString(EscapeString(s)) == s always holds, but the converse isn't
|
||||||
|
// always true.
|
||||||
|
func UnescapeString(s string) string {
|
||||||
|
for _, c := range s {
|
||||||
|
if c == '&' {
|
||||||
|
return string(unescape([]byte(s), false))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
226
vendor/golang.org/x/net/html/foreign.go
generated
vendored
Normal file
226
vendor/golang.org/x/net/html/foreign.go
generated
vendored
Normal file
@ -0,0 +1,226 @@
|
|||||||
|
// Copyright 2011 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package html
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func adjustAttributeNames(aa []Attribute, nameMap map[string]string) {
|
||||||
|
for i := range aa {
|
||||||
|
if newName, ok := nameMap[aa[i].Key]; ok {
|
||||||
|
aa[i].Key = newName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func adjustForeignAttributes(aa []Attribute) {
|
||||||
|
for i, a := range aa {
|
||||||
|
if a.Key == "" || a.Key[0] != 'x' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch a.Key {
|
||||||
|
case "xlink:actuate", "xlink:arcrole", "xlink:href", "xlink:role", "xlink:show",
|
||||||
|
"xlink:title", "xlink:type", "xml:base", "xml:lang", "xml:space", "xmlns:xlink":
|
||||||
|
j := strings.Index(a.Key, ":")
|
||||||
|
aa[i].Namespace = a.Key[:j]
|
||||||
|
aa[i].Key = a.Key[j+1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func htmlIntegrationPoint(n *Node) bool {
|
||||||
|
if n.Type != ElementNode {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch n.Namespace {
|
||||||
|
case "math":
|
||||||
|
if n.Data == "annotation-xml" {
|
||||||
|
for _, a := range n.Attr {
|
||||||
|
if a.Key == "encoding" {
|
||||||
|
val := strings.ToLower(a.Val)
|
||||||
|
if val == "text/html" || val == "application/xhtml+xml" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "svg":
|
||||||
|
switch n.Data {
|
||||||
|
case "desc", "foreignObject", "title":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func mathMLTextIntegrationPoint(n *Node) bool {
|
||||||
|
if n.Namespace != "math" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch n.Data {
|
||||||
|
case "mi", "mo", "mn", "ms", "mtext":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Section 12.2.6.5.
|
||||||
|
var breakout = map[string]bool{
|
||||||
|
"b": true,
|
||||||
|
"big": true,
|
||||||
|
"blockquote": true,
|
||||||
|
"body": true,
|
||||||
|
"br": true,
|
||||||
|
"center": true,
|
||||||
|
"code": true,
|
||||||
|
"dd": true,
|
||||||
|
"div": true,
|
||||||
|
"dl": true,
|
||||||
|
"dt": true,
|
||||||
|
"em": true,
|
||||||
|
"embed": true,
|
||||||
|
"h1": true,
|
||||||
|
"h2": true,
|
||||||
|
"h3": true,
|
||||||
|
"h4": true,
|
||||||
|
"h5": true,
|
||||||
|
"h6": true,
|
||||||
|
"head": true,
|
||||||
|
"hr": true,
|
||||||
|
"i": true,
|
||||||
|
"img": true,
|
||||||
|
"li": true,
|
||||||
|
"listing": true,
|
||||||
|
"menu": true,
|
||||||
|
"meta": true,
|
||||||
|
"nobr": true,
|
||||||
|
"ol": true,
|
||||||
|
"p": true,
|
||||||
|
"pre": true,
|
||||||
|
"ruby": true,
|
||||||
|
"s": true,
|
||||||
|
"small": true,
|
||||||
|
"span": true,
|
||||||
|
"strong": true,
|
||||||
|
"strike": true,
|
||||||
|
"sub": true,
|
||||||
|
"sup": true,
|
||||||
|
"table": true,
|
||||||
|
"tt": true,
|
||||||
|
"u": true,
|
||||||
|
"ul": true,
|
||||||
|
"var": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Section 12.2.6.5.
|
||||||
|
var svgTagNameAdjustments = map[string]string{
|
||||||
|
"altglyph": "altGlyph",
|
||||||
|
"altglyphdef": "altGlyphDef",
|
||||||
|
"altglyphitem": "altGlyphItem",
|
||||||
|
"animatecolor": "animateColor",
|
||||||
|
"animatemotion": "animateMotion",
|
||||||
|
"animatetransform": "animateTransform",
|
||||||
|
"clippath": "clipPath",
|
||||||
|
"feblend": "feBlend",
|
||||||
|
"fecolormatrix": "feColorMatrix",
|
||||||
|
"fecomponenttransfer": "feComponentTransfer",
|
||||||
|
"fecomposite": "feComposite",
|
||||||
|
"feconvolvematrix": "feConvolveMatrix",
|
||||||
|
"fediffuselighting": "feDiffuseLighting",
|
||||||
|
"fedisplacementmap": "feDisplacementMap",
|
||||||
|
"fedistantlight": "feDistantLight",
|
||||||
|
"feflood": "feFlood",
|
||||||
|
"fefunca": "feFuncA",
|
||||||
|
"fefuncb": "feFuncB",
|
||||||
|
"fefuncg": "feFuncG",
|
||||||
|
"fefuncr": "feFuncR",
|
||||||
|
"fegaussianblur": "feGaussianBlur",
|
||||||
|
"feimage": "feImage",
|
||||||
|
"femerge": "feMerge",
|
||||||
|
"femergenode": "feMergeNode",
|
||||||
|
"femorphology": "feMorphology",
|
||||||
|
"feoffset": "feOffset",
|
||||||
|
"fepointlight": "fePointLight",
|
||||||
|
"fespecularlighting": "feSpecularLighting",
|
||||||
|
"fespotlight": "feSpotLight",
|
||||||
|
"fetile": "feTile",
|
||||||
|
"feturbulence": "feTurbulence",
|
||||||
|
"foreignobject": "foreignObject",
|
||||||
|
"glyphref": "glyphRef",
|
||||||
|
"lineargradient": "linearGradient",
|
||||||
|
"radialgradient": "radialGradient",
|
||||||
|
"textpath": "textPath",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Section 12.2.6.1
|
||||||
|
var mathMLAttributeAdjustments = map[string]string{
|
||||||
|
"definitionurl": "definitionURL",
|
||||||
|
}
|
||||||
|
|
||||||
|
var svgAttributeAdjustments = map[string]string{
|
||||||
|
"attributename": "attributeName",
|
||||||
|
"attributetype": "attributeType",
|
||||||
|
"basefrequency": "baseFrequency",
|
||||||
|
"baseprofile": "baseProfile",
|
||||||
|
"calcmode": "calcMode",
|
||||||
|
"clippathunits": "clipPathUnits",
|
||||||
|
"contentscripttype": "contentScriptType",
|
||||||
|
"contentstyletype": "contentStyleType",
|
||||||
|
"diffuseconstant": "diffuseConstant",
|
||||||
|
"edgemode": "edgeMode",
|
||||||
|
"externalresourcesrequired": "externalResourcesRequired",
|
||||||
|
"filterres": "filterRes",
|
||||||
|
"filterunits": "filterUnits",
|
||||||
|
"glyphref": "glyphRef",
|
||||||
|
"gradienttransform": "gradientTransform",
|
||||||
|
"gradientunits": "gradientUnits",
|
||||||
|
"kernelmatrix": "kernelMatrix",
|
||||||
|
"kernelunitlength": "kernelUnitLength",
|
||||||
|
"keypoints": "keyPoints",
|
||||||
|
"keysplines": "keySplines",
|
||||||
|
"keytimes": "keyTimes",
|
||||||
|
"lengthadjust": "lengthAdjust",
|
||||||
|
"limitingconeangle": "limitingConeAngle",
|
||||||
|
"markerheight": "markerHeight",
|
||||||
|
"markerunits": "markerUnits",
|
||||||
|
"markerwidth": "markerWidth",
|
||||||
|
"maskcontentunits": "maskContentUnits",
|
||||||
|
"maskunits": "maskUnits",
|
||||||
|
"numoctaves": "numOctaves",
|
||||||
|
"pathlength": "pathLength",
|
||||||
|
"patterncontentunits": "patternContentUnits",
|
||||||
|
"patterntransform": "patternTransform",
|
||||||
|
"patternunits": "patternUnits",
|
||||||
|
"pointsatx": "pointsAtX",
|
||||||
|
"pointsaty": "pointsAtY",
|
||||||
|
"pointsatz": "pointsAtZ",
|
||||||
|
"preservealpha": "preserveAlpha",
|
||||||
|
"preserveaspectratio": "preserveAspectRatio",
|
||||||
|
"primitiveunits": "primitiveUnits",
|
||||||
|
"refx": "refX",
|
||||||
|
"refy": "refY",
|
||||||
|
"repeatcount": "repeatCount",
|
||||||
|
"repeatdur": "repeatDur",
|
||||||
|
"requiredextensions": "requiredExtensions",
|
||||||
|
"requiredfeatures": "requiredFeatures",
|
||||||
|
"specularconstant": "specularConstant",
|
||||||
|
"specularexponent": "specularExponent",
|
||||||
|
"spreadmethod": "spreadMethod",
|
||||||
|
"startoffset": "startOffset",
|
||||||
|
"stddeviation": "stdDeviation",
|
||||||
|
"stitchtiles": "stitchTiles",
|
||||||
|
"surfacescale": "surfaceScale",
|
||||||
|
"systemlanguage": "systemLanguage",
|
||||||
|
"tablevalues": "tableValues",
|
||||||
|
"targetx": "targetX",
|
||||||
|
"targety": "targetY",
|
||||||
|
"textlength": "textLength",
|
||||||
|
"viewbox": "viewBox",
|
||||||
|
"viewtarget": "viewTarget",
|
||||||
|
"xchannelselector": "xChannelSelector",
|
||||||
|
"ychannelselector": "yChannelSelector",
|
||||||
|
"zoomandpan": "zoomAndPan",
|
||||||
|
}
|
220
vendor/golang.org/x/net/html/node.go
generated
vendored
Normal file
220
vendor/golang.org/x/net/html/node.go
generated
vendored
Normal file
@ -0,0 +1,220 @@
|
|||||||
|
// Copyright 2011 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package html
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/net/html/atom"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A NodeType is the type of a Node.
|
||||||
|
type NodeType uint32
|
||||||
|
|
||||||
|
const (
|
||||||
|
ErrorNode NodeType = iota
|
||||||
|
TextNode
|
||||||
|
DocumentNode
|
||||||
|
ElementNode
|
||||||
|
CommentNode
|
||||||
|
DoctypeNode
|
||||||
|
scopeMarkerNode
|
||||||
|
)
|
||||||
|
|
||||||
|
// Section 12.2.4.3 says "The markers are inserted when entering applet,
|
||||||
|
// object, marquee, template, td, th, and caption elements, and are used
|
||||||
|
// to prevent formatting from "leaking" into applet, object, marquee,
|
||||||
|
// template, td, th, and caption elements".
|
||||||
|
var scopeMarker = Node{Type: scopeMarkerNode}
|
||||||
|
|
||||||
|
// A Node consists of a NodeType and some Data (tag name for element nodes,
|
||||||
|
// content for text) and are part of a tree of Nodes. Element nodes may also
|
||||||
|
// have a Namespace and contain a slice of Attributes. Data is unescaped, so
|
||||||
|
// that it looks like "a<b" rather than "a<b". For element nodes, DataAtom
|
||||||
|
// is the atom for Data, or zero if Data is not a known tag name.
|
||||||
|
//
|
||||||
|
// An empty Namespace implies a "http://www.w3.org/1999/xhtml" namespace.
|
||||||
|
// Similarly, "math" is short for "http://www.w3.org/1998/Math/MathML", and
|
||||||
|
// "svg" is short for "http://www.w3.org/2000/svg".
|
||||||
|
type Node struct {
|
||||||
|
Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node
|
||||||
|
|
||||||
|
Type NodeType
|
||||||
|
DataAtom atom.Atom
|
||||||
|
Data string
|
||||||
|
Namespace string
|
||||||
|
Attr []Attribute
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertBefore inserts newChild as a child of n, immediately before oldChild
|
||||||
|
// in the sequence of n's children. oldChild may be nil, in which case newChild
|
||||||
|
// is appended to the end of n's children.
|
||||||
|
//
|
||||||
|
// It will panic if newChild already has a parent or siblings.
|
||||||
|
func (n *Node) InsertBefore(newChild, oldChild *Node) {
|
||||||
|
if newChild.Parent != nil || newChild.PrevSibling != nil || newChild.NextSibling != nil {
|
||||||
|
panic("html: InsertBefore called for an attached child Node")
|
||||||
|
}
|
||||||
|
var prev, next *Node
|
||||||
|
if oldChild != nil {
|
||||||
|
prev, next = oldChild.PrevSibling, oldChild
|
||||||
|
} else {
|
||||||
|
prev = n.LastChild
|
||||||
|
}
|
||||||
|
if prev != nil {
|
||||||
|
prev.NextSibling = newChild
|
||||||
|
} else {
|
||||||
|
n.FirstChild = newChild
|
||||||
|
}
|
||||||
|
if next != nil {
|
||||||
|
next.PrevSibling = newChild
|
||||||
|
} else {
|
||||||
|
n.LastChild = newChild
|
||||||
|
}
|
||||||
|
newChild.Parent = n
|
||||||
|
newChild.PrevSibling = prev
|
||||||
|
newChild.NextSibling = next
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppendChild adds a node c as a child of n.
|
||||||
|
//
|
||||||
|
// It will panic if c already has a parent or siblings.
|
||||||
|
func (n *Node) AppendChild(c *Node) {
|
||||||
|
if c.Parent != nil || c.PrevSibling != nil || c.NextSibling != nil {
|
||||||
|
panic("html: AppendChild called for an attached child Node")
|
||||||
|
}
|
||||||
|
last := n.LastChild
|
||||||
|
if last != nil {
|
||||||
|
last.NextSibling = c
|
||||||
|
} else {
|
||||||
|
n.FirstChild = c
|
||||||
|
}
|
||||||
|
n.LastChild = c
|
||||||
|
c.Parent = n
|
||||||
|
c.PrevSibling = last
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveChild removes a node c that is a child of n. Afterwards, c will have
|
||||||
|
// no parent and no siblings.
|
||||||
|
//
|
||||||
|
// It will panic if c's parent is not n.
|
||||||
|
func (n *Node) RemoveChild(c *Node) {
|
||||||
|
if c.Parent != n {
|
||||||
|
panic("html: RemoveChild called for a non-child Node")
|
||||||
|
}
|
||||||
|
if n.FirstChild == c {
|
||||||
|
n.FirstChild = c.NextSibling
|
||||||
|
}
|
||||||
|
if c.NextSibling != nil {
|
||||||
|
c.NextSibling.PrevSibling = c.PrevSibling
|
||||||
|
}
|
||||||
|
if n.LastChild == c {
|
||||||
|
n.LastChild = c.PrevSibling
|
||||||
|
}
|
||||||
|
if c.PrevSibling != nil {
|
||||||
|
c.PrevSibling.NextSibling = c.NextSibling
|
||||||
|
}
|
||||||
|
c.Parent = nil
|
||||||
|
c.PrevSibling = nil
|
||||||
|
c.NextSibling = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// reparentChildren reparents all of src's child nodes to dst.
|
||||||
|
func reparentChildren(dst, src *Node) {
|
||||||
|
for {
|
||||||
|
child := src.FirstChild
|
||||||
|
if child == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
src.RemoveChild(child)
|
||||||
|
dst.AppendChild(child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// clone returns a new node with the same type, data and attributes.
|
||||||
|
// The clone has no parent, no siblings and no children.
|
||||||
|
func (n *Node) clone() *Node {
|
||||||
|
m := &Node{
|
||||||
|
Type: n.Type,
|
||||||
|
DataAtom: n.DataAtom,
|
||||||
|
Data: n.Data,
|
||||||
|
Attr: make([]Attribute, len(n.Attr)),
|
||||||
|
}
|
||||||
|
copy(m.Attr, n.Attr)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
// nodeStack is a stack of nodes.
|
||||||
|
type nodeStack []*Node
|
||||||
|
|
||||||
|
// pop pops the stack. It will panic if s is empty.
|
||||||
|
func (s *nodeStack) pop() *Node {
|
||||||
|
i := len(*s)
|
||||||
|
n := (*s)[i-1]
|
||||||
|
*s = (*s)[:i-1]
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// top returns the most recently pushed node, or nil if s is empty.
|
||||||
|
func (s *nodeStack) top() *Node {
|
||||||
|
if i := len(*s); i > 0 {
|
||||||
|
return (*s)[i-1]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// index returns the index of the top-most occurrence of n in the stack, or -1
|
||||||
|
// if n is not present.
|
||||||
|
func (s *nodeStack) index(n *Node) int {
|
||||||
|
for i := len(*s) - 1; i >= 0; i-- {
|
||||||
|
if (*s)[i] == n {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// contains returns whether a is within s.
|
||||||
|
func (s *nodeStack) contains(a atom.Atom) bool {
|
||||||
|
for _, n := range *s {
|
||||||
|
if n.DataAtom == a && n.Namespace == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert inserts a node at the given index.
|
||||||
|
func (s *nodeStack) insert(i int, n *Node) {
|
||||||
|
(*s) = append(*s, nil)
|
||||||
|
copy((*s)[i+1:], (*s)[i:])
|
||||||
|
(*s)[i] = n
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove removes a node from the stack. It is a no-op if n is not present.
|
||||||
|
func (s *nodeStack) remove(n *Node) {
|
||||||
|
i := s.index(n)
|
||||||
|
if i == -1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
copy((*s)[i:], (*s)[i+1:])
|
||||||
|
j := len(*s) - 1
|
||||||
|
(*s)[j] = nil
|
||||||
|
*s = (*s)[:j]
|
||||||
|
}
|
||||||
|
|
||||||
|
type insertionModeStack []insertionMode
|
||||||
|
|
||||||
|
func (s *insertionModeStack) pop() (im insertionMode) {
|
||||||
|
i := len(*s)
|
||||||
|
im = (*s)[i-1]
|
||||||
|
*s = (*s)[:i-1]
|
||||||
|
return im
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *insertionModeStack) top() insertionMode {
|
||||||
|
if i := len(*s); i > 0 {
|
||||||
|
return (*s)[i-1]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
2417
vendor/golang.org/x/net/html/parse.go
generated
vendored
Normal file
2417
vendor/golang.org/x/net/html/parse.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
271
vendor/golang.org/x/net/html/render.go
generated
vendored
Normal file
271
vendor/golang.org/x/net/html/render.go
generated
vendored
Normal file
@ -0,0 +1,271 @@
|
|||||||
|
// Copyright 2011 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package html
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type writer interface {
|
||||||
|
io.Writer
|
||||||
|
io.ByteWriter
|
||||||
|
WriteString(string) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render renders the parse tree n to the given writer.
|
||||||
|
//
|
||||||
|
// Rendering is done on a 'best effort' basis: calling Parse on the output of
|
||||||
|
// Render will always result in something similar to the original tree, but it
|
||||||
|
// is not necessarily an exact clone unless the original tree was 'well-formed'.
|
||||||
|
// 'Well-formed' is not easily specified; the HTML5 specification is
|
||||||
|
// complicated.
|
||||||
|
//
|
||||||
|
// Calling Parse on arbitrary input typically results in a 'well-formed' parse
|
||||||
|
// tree. However, it is possible for Parse to yield a 'badly-formed' parse tree.
|
||||||
|
// For example, in a 'well-formed' parse tree, no <a> element is a child of
|
||||||
|
// another <a> element: parsing "<a><a>" results in two sibling elements.
|
||||||
|
// Similarly, in a 'well-formed' parse tree, no <a> element is a child of a
|
||||||
|
// <table> element: parsing "<p><table><a>" results in a <p> with two sibling
|
||||||
|
// children; the <a> is reparented to the <table>'s parent. However, calling
|
||||||
|
// Parse on "<a><table><a>" does not return an error, but the result has an <a>
|
||||||
|
// element with an <a> child, and is therefore not 'well-formed'.
|
||||||
|
//
|
||||||
|
// Programmatically constructed trees are typically also 'well-formed', but it
|
||||||
|
// is possible to construct a tree that looks innocuous but, when rendered and
|
||||||
|
// re-parsed, results in a different tree. A simple example is that a solitary
|
||||||
|
// text node would become a tree containing <html>, <head> and <body> elements.
|
||||||
|
// Another example is that the programmatic equivalent of "a<head>b</head>c"
|
||||||
|
// becomes "<html><head><head/><body>abc</body></html>".
|
||||||
|
func Render(w io.Writer, n *Node) error {
|
||||||
|
if x, ok := w.(writer); ok {
|
||||||
|
return render(x, n)
|
||||||
|
}
|
||||||
|
buf := bufio.NewWriter(w)
|
||||||
|
if err := render(buf, n); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return buf.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
// plaintextAbort is returned from render1 when a <plaintext> element
|
||||||
|
// has been rendered. No more end tags should be rendered after that.
|
||||||
|
var plaintextAbort = errors.New("html: internal error (plaintext abort)")
|
||||||
|
|
||||||
|
func render(w writer, n *Node) error {
|
||||||
|
err := render1(w, n)
|
||||||
|
if err == plaintextAbort {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func render1(w writer, n *Node) error {
|
||||||
|
// Render non-element nodes; these are the easy cases.
|
||||||
|
switch n.Type {
|
||||||
|
case ErrorNode:
|
||||||
|
return errors.New("html: cannot render an ErrorNode node")
|
||||||
|
case TextNode:
|
||||||
|
return escape(w, n.Data)
|
||||||
|
case DocumentNode:
|
||||||
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||||
|
if err := render1(w, c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case ElementNode:
|
||||||
|
// No-op.
|
||||||
|
case CommentNode:
|
||||||
|
if _, err := w.WriteString("<!--"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString(n.Data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString("-->"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case DoctypeNode:
|
||||||
|
if _, err := w.WriteString("<!DOCTYPE "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString(n.Data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n.Attr != nil {
|
||||||
|
var p, s string
|
||||||
|
for _, a := range n.Attr {
|
||||||
|
switch a.Key {
|
||||||
|
case "public":
|
||||||
|
p = a.Val
|
||||||
|
case "system":
|
||||||
|
s = a.Val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if p != "" {
|
||||||
|
if _, err := w.WriteString(" PUBLIC "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := writeQuoted(w, p); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if s != "" {
|
||||||
|
if err := w.WriteByte(' '); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := writeQuoted(w, s); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if s != "" {
|
||||||
|
if _, err := w.WriteString(" SYSTEM "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := writeQuoted(w, s); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return w.WriteByte('>')
|
||||||
|
default:
|
||||||
|
return errors.New("html: unknown node type")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render the <xxx> opening tag.
|
||||||
|
if err := w.WriteByte('<'); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString(n.Data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, a := range n.Attr {
|
||||||
|
if err := w.WriteByte(' '); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if a.Namespace != "" {
|
||||||
|
if _, err := w.WriteString(a.Namespace); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := w.WriteByte(':'); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString(a.Key); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString(`="`); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := escape(w, a.Val); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := w.WriteByte('"'); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if voidElements[n.Data] {
|
||||||
|
if n.FirstChild != nil {
|
||||||
|
return fmt.Errorf("html: void element <%s> has child nodes", n.Data)
|
||||||
|
}
|
||||||
|
_, err := w.WriteString("/>")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := w.WriteByte('>'); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add initial newline where there is danger of a newline beging ignored.
|
||||||
|
if c := n.FirstChild; c != nil && c.Type == TextNode && strings.HasPrefix(c.Data, "\n") {
|
||||||
|
switch n.Data {
|
||||||
|
case "pre", "listing", "textarea":
|
||||||
|
if err := w.WriteByte('\n'); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render any child nodes.
|
||||||
|
switch n.Data {
|
||||||
|
case "iframe", "noembed", "noframes", "noscript", "plaintext", "script", "style", "xmp":
|
||||||
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||||
|
if c.Type == TextNode {
|
||||||
|
if _, err := w.WriteString(c.Data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := render1(w, c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if n.Data == "plaintext" {
|
||||||
|
// Don't render anything else. <plaintext> must be the
|
||||||
|
// last element in the file, with no closing tag.
|
||||||
|
return plaintextAbort
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||||
|
if err := render1(w, c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render the </xxx> closing tag.
|
||||||
|
if _, err := w.WriteString("</"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString(n.Data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return w.WriteByte('>')
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeQuoted writes s to w surrounded by quotes. Normally it will use double
|
||||||
|
// quotes, but if s contains a double quote, it will use single quotes.
|
||||||
|
// It is used for writing the identifiers in a doctype declaration.
|
||||||
|
// In valid HTML, they can't contain both types of quotes.
|
||||||
|
func writeQuoted(w writer, s string) error {
|
||||||
|
var q byte = '"'
|
||||||
|
if strings.Contains(s, `"`) {
|
||||||
|
q = '\''
|
||||||
|
}
|
||||||
|
if err := w.WriteByte(q); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := w.WriteString(s); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := w.WriteByte(q); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Section 12.1.2, "Elements", gives this list of void elements. Void elements
|
||||||
|
// are those that can't have any contents.
|
||||||
|
var voidElements = map[string]bool{
|
||||||
|
"area": true,
|
||||||
|
"base": true,
|
||||||
|
"br": true,
|
||||||
|
"col": true,
|
||||||
|
"command": true,
|
||||||
|
"embed": true,
|
||||||
|
"hr": true,
|
||||||
|
"img": true,
|
||||||
|
"input": true,
|
||||||
|
"keygen": true,
|
||||||
|
"link": true,
|
||||||
|
"meta": true,
|
||||||
|
"param": true,
|
||||||
|
"source": true,
|
||||||
|
"track": true,
|
||||||
|
"wbr": true,
|
||||||
|
}
|
1219
vendor/golang.org/x/net/html/token.go
generated
vendored
Normal file
1219
vendor/golang.org/x/net/html/token.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
3
vendor/golang.org/x/text/AUTHORS
generated
vendored
Normal file
3
vendor/golang.org/x/text/AUTHORS
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# This source code refers to The Go Authors for copyright purposes.
|
||||||
|
# The master list of authors is in the main Go distribution,
|
||||||
|
# visible at http://tip.golang.org/AUTHORS.
|
3
vendor/golang.org/x/text/CONTRIBUTORS
generated
vendored
Normal file
3
vendor/golang.org/x/text/CONTRIBUTORS
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# This source code was written by the Go contributors.
|
||||||
|
# The master list of contributors is in the main Go distribution,
|
||||||
|
# visible at http://tip.golang.org/CONTRIBUTORS.
|
27
vendor/golang.org/x/text/LICENSE
generated
vendored
Normal file
27
vendor/golang.org/x/text/LICENSE
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above
|
||||||
|
copyright notice, this list of conditions and the following disclaimer
|
||||||
|
in the documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
* Neither the name of Google Inc. nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
22
vendor/golang.org/x/text/PATENTS
generated
vendored
Normal file
22
vendor/golang.org/x/text/PATENTS
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
Additional IP Rights Grant (Patents)
|
||||||
|
|
||||||
|
"This implementation" means the copyrightable works distributed by
|
||||||
|
Google as part of the Go project.
|
||||||
|
|
||||||
|
Google hereby grants to You a perpetual, worldwide, non-exclusive,
|
||||||
|
no-charge, royalty-free, irrevocable (except as stated in this section)
|
||||||
|
patent license to make, have made, use, offer to sell, sell, import,
|
||||||
|
transfer and otherwise run, modify and propagate the contents of this
|
||||||
|
implementation of Go, where such license applies only to those patent
|
||||||
|
claims, both currently owned or controlled by Google and acquired in
|
||||||
|
the future, licensable by Google that are necessarily infringed by this
|
||||||
|
implementation of Go. This grant does not include claims that would be
|
||||||
|
infringed only as a consequence of further modification of this
|
||||||
|
implementation. If you or your agent or exclusive licensee institute or
|
||||||
|
order or agree to the institution of patent litigation against any
|
||||||
|
entity (including a cross-claim or counterclaim in a lawsuit) alleging
|
||||||
|
that this implementation of Go or any code incorporated within this
|
||||||
|
implementation of Go constitutes direct or contributory patent
|
||||||
|
infringement, or inducement of patent infringement, then any patent
|
||||||
|
rights granted to you under this License for this implementation of Go
|
||||||
|
shall terminate as of the date such litigation is filed.
|
249
vendor/golang.org/x/text/encoding/charmap/charmap.go
generated
vendored
Normal file
249
vendor/golang.org/x/text/encoding/charmap/charmap.go
generated
vendored
Normal file
@ -0,0 +1,249 @@
|
|||||||
|
// Copyright 2013 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
//go:generate go run maketables.go
|
||||||
|
|
||||||
|
// Package charmap provides simple character encodings such as IBM Code Page 437
|
||||||
|
// and Windows 1252.
|
||||||
|
package charmap // import "golang.org/x/text/encoding/charmap"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"golang.org/x/text/encoding"
|
||||||
|
"golang.org/x/text/encoding/internal"
|
||||||
|
"golang.org/x/text/encoding/internal/identifier"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
|
)
|
||||||
|
|
||||||
|
// These encodings vary only in the way clients should interpret them. Their
|
||||||
|
// coded character set is identical and a single implementation can be shared.
|
||||||
|
var (
|
||||||
|
// ISO8859_6E is the ISO 8859-6E encoding.
|
||||||
|
ISO8859_6E encoding.Encoding = &iso8859_6E
|
||||||
|
|
||||||
|
// ISO8859_6I is the ISO 8859-6I encoding.
|
||||||
|
ISO8859_6I encoding.Encoding = &iso8859_6I
|
||||||
|
|
||||||
|
// ISO8859_8E is the ISO 8859-8E encoding.
|
||||||
|
ISO8859_8E encoding.Encoding = &iso8859_8E
|
||||||
|
|
||||||
|
// ISO8859_8I is the ISO 8859-8I encoding.
|
||||||
|
ISO8859_8I encoding.Encoding = &iso8859_8I
|
||||||
|
|
||||||
|
iso8859_6E = internal.Encoding{
|
||||||
|
Encoding: ISO8859_6,
|
||||||
|
Name: "ISO-8859-6E",
|
||||||
|
MIB: identifier.ISO88596E,
|
||||||
|
}
|
||||||
|
|
||||||
|
iso8859_6I = internal.Encoding{
|
||||||
|
Encoding: ISO8859_6,
|
||||||
|
Name: "ISO-8859-6I",
|
||||||
|
MIB: identifier.ISO88596I,
|
||||||
|
}
|
||||||
|
|
||||||
|
iso8859_8E = internal.Encoding{
|
||||||
|
Encoding: ISO8859_8,
|
||||||
|
Name: "ISO-8859-8E",
|
||||||
|
MIB: identifier.ISO88598E,
|
||||||
|
}
|
||||||
|
|
||||||
|
iso8859_8I = internal.Encoding{
|
||||||
|
Encoding: ISO8859_8,
|
||||||
|
Name: "ISO-8859-8I",
|
||||||
|
MIB: identifier.ISO88598I,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// All is a list of all defined encodings in this package.
|
||||||
|
var All []encoding.Encoding = listAll
|
||||||
|
|
||||||
|
// TODO: implement these encodings, in order of importance.
|
||||||
|
// ASCII, ISO8859_1: Rather common. Close to Windows 1252.
|
||||||
|
// ISO8859_9: Close to Windows 1254.
|
||||||
|
|
||||||
|
// utf8Enc holds a rune's UTF-8 encoding in data[:len].
|
||||||
|
type utf8Enc struct {
|
||||||
|
len uint8
|
||||||
|
data [3]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// Charmap is an 8-bit character set encoding.
|
||||||
|
type Charmap struct {
|
||||||
|
// name is the encoding's name.
|
||||||
|
name string
|
||||||
|
// mib is the encoding type of this encoder.
|
||||||
|
mib identifier.MIB
|
||||||
|
// asciiSuperset states whether the encoding is a superset of ASCII.
|
||||||
|
asciiSuperset bool
|
||||||
|
// low is the lower bound of the encoded byte for a non-ASCII rune. If
|
||||||
|
// Charmap.asciiSuperset is true then this will be 0x80, otherwise 0x00.
|
||||||
|
low uint8
|
||||||
|
// replacement is the encoded replacement character.
|
||||||
|
replacement byte
|
||||||
|
// decode is the map from encoded byte to UTF-8.
|
||||||
|
decode [256]utf8Enc
|
||||||
|
// encoding is the map from runes to encoded bytes. Each entry is a
|
||||||
|
// uint32: the high 8 bits are the encoded byte and the low 24 bits are
|
||||||
|
// the rune. The table entries are sorted by ascending rune.
|
||||||
|
encode [256]uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDecoder implements the encoding.Encoding interface.
|
||||||
|
func (m *Charmap) NewDecoder() *encoding.Decoder {
|
||||||
|
return &encoding.Decoder{Transformer: charmapDecoder{charmap: m}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewEncoder implements the encoding.Encoding interface.
|
||||||
|
func (m *Charmap) NewEncoder() *encoding.Encoder {
|
||||||
|
return &encoding.Encoder{Transformer: charmapEncoder{charmap: m}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the Charmap's name.
|
||||||
|
func (m *Charmap) String() string {
|
||||||
|
return m.name
|
||||||
|
}
|
||||||
|
|
||||||
|
// ID implements an internal interface.
|
||||||
|
func (m *Charmap) ID() (mib identifier.MIB, other string) {
|
||||||
|
return m.mib, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// charmapDecoder implements transform.Transformer by decoding to UTF-8.
|
||||||
|
type charmapDecoder struct {
|
||||||
|
transform.NopResetter
|
||||||
|
charmap *Charmap
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m charmapDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
|
||||||
|
for i, c := range src {
|
||||||
|
if m.charmap.asciiSuperset && c < utf8.RuneSelf {
|
||||||
|
if nDst >= len(dst) {
|
||||||
|
err = transform.ErrShortDst
|
||||||
|
break
|
||||||
|
}
|
||||||
|
dst[nDst] = c
|
||||||
|
nDst++
|
||||||
|
nSrc = i + 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
decode := &m.charmap.decode[c]
|
||||||
|
n := int(decode.len)
|
||||||
|
if nDst+n > len(dst) {
|
||||||
|
err = transform.ErrShortDst
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// It's 15% faster to avoid calling copy for these tiny slices.
|
||||||
|
for j := 0; j < n; j++ {
|
||||||
|
dst[nDst] = decode.data[j]
|
||||||
|
nDst++
|
||||||
|
}
|
||||||
|
nSrc = i + 1
|
||||||
|
}
|
||||||
|
return nDst, nSrc, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeByte returns the Charmap's rune decoding of the byte b.
|
||||||
|
func (m *Charmap) DecodeByte(b byte) rune {
|
||||||
|
switch x := &m.decode[b]; x.len {
|
||||||
|
case 1:
|
||||||
|
return rune(x.data[0])
|
||||||
|
case 2:
|
||||||
|
return rune(x.data[0]&0x1f)<<6 | rune(x.data[1]&0x3f)
|
||||||
|
default:
|
||||||
|
return rune(x.data[0]&0x0f)<<12 | rune(x.data[1]&0x3f)<<6 | rune(x.data[2]&0x3f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// charmapEncoder implements transform.Transformer by encoding from UTF-8.
|
||||||
|
type charmapEncoder struct {
|
||||||
|
transform.NopResetter
|
||||||
|
charmap *Charmap
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m charmapEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
|
||||||
|
r, size := rune(0), 0
|
||||||
|
loop:
|
||||||
|
for nSrc < len(src) {
|
||||||
|
if nDst >= len(dst) {
|
||||||
|
err = transform.ErrShortDst
|
||||||
|
break
|
||||||
|
}
|
||||||
|
r = rune(src[nSrc])
|
||||||
|
|
||||||
|
// Decode a 1-byte rune.
|
||||||
|
if r < utf8.RuneSelf {
|
||||||
|
if m.charmap.asciiSuperset {
|
||||||
|
nSrc++
|
||||||
|
dst[nDst] = uint8(r)
|
||||||
|
nDst++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
size = 1
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Decode a multi-byte rune.
|
||||||
|
r, size = utf8.DecodeRune(src[nSrc:])
|
||||||
|
if size == 1 {
|
||||||
|
// All valid runes of size 1 (those below utf8.RuneSelf) were
|
||||||
|
// handled above. We have invalid UTF-8 or we haven't seen the
|
||||||
|
// full character yet.
|
||||||
|
if !atEOF && !utf8.FullRune(src[nSrc:]) {
|
||||||
|
err = transform.ErrShortSrc
|
||||||
|
} else {
|
||||||
|
err = internal.RepertoireError(m.charmap.replacement)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Binary search in [low, high) for that rune in the m.charmap.encode table.
|
||||||
|
for low, high := int(m.charmap.low), 0x100; ; {
|
||||||
|
if low >= high {
|
||||||
|
err = internal.RepertoireError(m.charmap.replacement)
|
||||||
|
break loop
|
||||||
|
}
|
||||||
|
mid := (low + high) / 2
|
||||||
|
got := m.charmap.encode[mid]
|
||||||
|
gotRune := rune(got & (1<<24 - 1))
|
||||||
|
if gotRune < r {
|
||||||
|
low = mid + 1
|
||||||
|
} else if gotRune > r {
|
||||||
|
high = mid
|
||||||
|
} else {
|
||||||
|
dst[nDst] = byte(got >> 24)
|
||||||
|
nDst++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nSrc += size
|
||||||
|
}
|
||||||
|
return nDst, nSrc, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncodeRune returns the Charmap's byte encoding of the rune r. ok is whether
|
||||||
|
// r is in the Charmap's repertoire. If not, b is set to the Charmap's
|
||||||
|
// replacement byte. This is often the ASCII substitute character '\x1a'.
|
||||||
|
func (m *Charmap) EncodeRune(r rune) (b byte, ok bool) {
|
||||||
|
if r < utf8.RuneSelf && m.asciiSuperset {
|
||||||
|
return byte(r), true
|
||||||
|
}
|
||||||
|
for low, high := int(m.low), 0x100; ; {
|
||||||
|
if low >= high {
|
||||||
|
return m.replacement, false
|
||||||
|
}
|
||||||
|
mid := (low + high) / 2
|
||||||
|
got := m.encode[mid]
|
||||||
|
gotRune := rune(got & (1<<24 - 1))
|
||||||
|
if gotRune < r {
|
||||||
|
low = mid + 1
|
||||||
|
} else if gotRune > r {
|
||||||
|
high = mid
|
||||||
|
} else {
|
||||||
|
return byte(got >> 24), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
556
vendor/golang.org/x/text/encoding/charmap/maketables.go
generated
vendored
Normal file
556
vendor/golang.org/x/text/encoding/charmap/maketables.go
generated
vendored
Normal file
@ -0,0 +1,556 @@
|
|||||||
|
// Copyright 2013 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"golang.org/x/text/encoding"
|
||||||
|
"golang.org/x/text/internal/gen"
|
||||||
|
)
|
||||||
|
|
||||||
|
const ascii = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" +
|
||||||
|
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
|
||||||
|
` !"#$%&'()*+,-./0123456789:;<=>?` +
|
||||||
|
`@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_` +
|
||||||
|
"`abcdefghijklmnopqrstuvwxyz{|}~\u007f"
|
||||||
|
|
||||||
|
var encodings = []struct {
|
||||||
|
name string
|
||||||
|
mib string
|
||||||
|
comment string
|
||||||
|
varName string
|
||||||
|
replacement byte
|
||||||
|
mapping string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"IBM Code Page 037",
|
||||||
|
"IBM037",
|
||||||
|
"",
|
||||||
|
"CodePage037",
|
||||||
|
0x3f,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM037-2.1.2.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 437",
|
||||||
|
"PC8CodePage437",
|
||||||
|
"",
|
||||||
|
"CodePage437",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM437-2.1.2.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 850",
|
||||||
|
"PC850Multilingual",
|
||||||
|
"",
|
||||||
|
"CodePage850",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM850-2.1.2.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 852",
|
||||||
|
"PCp852",
|
||||||
|
"",
|
||||||
|
"CodePage852",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM852-2.1.2.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 855",
|
||||||
|
"IBM855",
|
||||||
|
"",
|
||||||
|
"CodePage855",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM855-2.1.2.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows Code Page 858", // PC latin1 with Euro
|
||||||
|
"IBM00858",
|
||||||
|
"",
|
||||||
|
"CodePage858",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/windows-858-2000.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 860",
|
||||||
|
"IBM860",
|
||||||
|
"",
|
||||||
|
"CodePage860",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM860-2.1.2.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 862",
|
||||||
|
"PC862LatinHebrew",
|
||||||
|
"",
|
||||||
|
"CodePage862",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM862-2.1.2.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 863",
|
||||||
|
"IBM863",
|
||||||
|
"",
|
||||||
|
"CodePage863",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM863-2.1.2.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 865",
|
||||||
|
"IBM865",
|
||||||
|
"",
|
||||||
|
"CodePage865",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM865-2.1.2.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 866",
|
||||||
|
"IBM866",
|
||||||
|
"",
|
||||||
|
"CodePage866",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-ibm866.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 1047",
|
||||||
|
"IBM1047",
|
||||||
|
"",
|
||||||
|
"CodePage1047",
|
||||||
|
0x3f,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/glibc-IBM1047-2.1.2.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"IBM Code Page 1140",
|
||||||
|
"IBM01140",
|
||||||
|
"",
|
||||||
|
"CodePage1140",
|
||||||
|
0x3f,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/ibm-1140_P100-1997.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-1",
|
||||||
|
"ISOLatin1",
|
||||||
|
"",
|
||||||
|
"ISO8859_1",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/iso-8859_1-1998.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-2",
|
||||||
|
"ISOLatin2",
|
||||||
|
"",
|
||||||
|
"ISO8859_2",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-2.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-3",
|
||||||
|
"ISOLatin3",
|
||||||
|
"",
|
||||||
|
"ISO8859_3",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-3.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-4",
|
||||||
|
"ISOLatin4",
|
||||||
|
"",
|
||||||
|
"ISO8859_4",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-4.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-5",
|
||||||
|
"ISOLatinCyrillic",
|
||||||
|
"",
|
||||||
|
"ISO8859_5",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-5.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-6",
|
||||||
|
"ISOLatinArabic",
|
||||||
|
"",
|
||||||
|
"ISO8859_6,ISO8859_6E,ISO8859_6I",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-6.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-7",
|
||||||
|
"ISOLatinGreek",
|
||||||
|
"",
|
||||||
|
"ISO8859_7",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-7.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-8",
|
||||||
|
"ISOLatinHebrew",
|
||||||
|
"",
|
||||||
|
"ISO8859_8,ISO8859_8E,ISO8859_8I",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-8.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-9",
|
||||||
|
"ISOLatin5",
|
||||||
|
"",
|
||||||
|
"ISO8859_9",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/iso-8859_9-1999.ucm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-10",
|
||||||
|
"ISOLatin6",
|
||||||
|
"",
|
||||||
|
"ISO8859_10",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-10.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-13",
|
||||||
|
"ISO885913",
|
||||||
|
"",
|
||||||
|
"ISO8859_13",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-13.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-14",
|
||||||
|
"ISO885914",
|
||||||
|
"",
|
||||||
|
"ISO8859_14",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-14.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-15",
|
||||||
|
"ISO885915",
|
||||||
|
"",
|
||||||
|
"ISO8859_15",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-15.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ISO 8859-16",
|
||||||
|
"ISO885916",
|
||||||
|
"",
|
||||||
|
"ISO8859_16",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-iso-8859-16.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"KOI8-R",
|
||||||
|
"KOI8R",
|
||||||
|
"",
|
||||||
|
"KOI8R",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-koi8-r.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"KOI8-U",
|
||||||
|
"KOI8U",
|
||||||
|
"",
|
||||||
|
"KOI8U",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-koi8-u.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Macintosh",
|
||||||
|
"Macintosh",
|
||||||
|
"",
|
||||||
|
"Macintosh",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-macintosh.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Macintosh Cyrillic",
|
||||||
|
"MacintoshCyrillic",
|
||||||
|
"",
|
||||||
|
"MacintoshCyrillic",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-x-mac-cyrillic.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows 874",
|
||||||
|
"Windows874",
|
||||||
|
"",
|
||||||
|
"Windows874",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-windows-874.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows 1250",
|
||||||
|
"Windows1250",
|
||||||
|
"",
|
||||||
|
"Windows1250",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-windows-1250.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows 1251",
|
||||||
|
"Windows1251",
|
||||||
|
"",
|
||||||
|
"Windows1251",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-windows-1251.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows 1252",
|
||||||
|
"Windows1252",
|
||||||
|
"",
|
||||||
|
"Windows1252",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-windows-1252.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows 1253",
|
||||||
|
"Windows1253",
|
||||||
|
"",
|
||||||
|
"Windows1253",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-windows-1253.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows 1254",
|
||||||
|
"Windows1254",
|
||||||
|
"",
|
||||||
|
"Windows1254",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-windows-1254.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows 1255",
|
||||||
|
"Windows1255",
|
||||||
|
"",
|
||||||
|
"Windows1255",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-windows-1255.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows 1256",
|
||||||
|
"Windows1256",
|
||||||
|
"",
|
||||||
|
"Windows1256",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-windows-1256.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows 1257",
|
||||||
|
"Windows1257",
|
||||||
|
"",
|
||||||
|
"Windows1257",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-windows-1257.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Windows 1258",
|
||||||
|
"Windows1258",
|
||||||
|
"",
|
||||||
|
"Windows1258",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
"http://encoding.spec.whatwg.org/index-windows-1258.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"X-User-Defined",
|
||||||
|
"XUserDefined",
|
||||||
|
"It is defined at http://encoding.spec.whatwg.org/#x-user-defined",
|
||||||
|
"XUserDefined",
|
||||||
|
encoding.ASCIISub,
|
||||||
|
ascii +
|
||||||
|
"\uf780\uf781\uf782\uf783\uf784\uf785\uf786\uf787" +
|
||||||
|
"\uf788\uf789\uf78a\uf78b\uf78c\uf78d\uf78e\uf78f" +
|
||||||
|
"\uf790\uf791\uf792\uf793\uf794\uf795\uf796\uf797" +
|
||||||
|
"\uf798\uf799\uf79a\uf79b\uf79c\uf79d\uf79e\uf79f" +
|
||||||
|
"\uf7a0\uf7a1\uf7a2\uf7a3\uf7a4\uf7a5\uf7a6\uf7a7" +
|
||||||
|
"\uf7a8\uf7a9\uf7aa\uf7ab\uf7ac\uf7ad\uf7ae\uf7af" +
|
||||||
|
"\uf7b0\uf7b1\uf7b2\uf7b3\uf7b4\uf7b5\uf7b6\uf7b7" +
|
||||||
|
"\uf7b8\uf7b9\uf7ba\uf7bb\uf7bc\uf7bd\uf7be\uf7bf" +
|
||||||
|
"\uf7c0\uf7c1\uf7c2\uf7c3\uf7c4\uf7c5\uf7c6\uf7c7" +
|
||||||
|
"\uf7c8\uf7c9\uf7ca\uf7cb\uf7cc\uf7cd\uf7ce\uf7cf" +
|
||||||
|
"\uf7d0\uf7d1\uf7d2\uf7d3\uf7d4\uf7d5\uf7d6\uf7d7" +
|
||||||
|
"\uf7d8\uf7d9\uf7da\uf7db\uf7dc\uf7dd\uf7de\uf7df" +
|
||||||
|
"\uf7e0\uf7e1\uf7e2\uf7e3\uf7e4\uf7e5\uf7e6\uf7e7" +
|
||||||
|
"\uf7e8\uf7e9\uf7ea\uf7eb\uf7ec\uf7ed\uf7ee\uf7ef" +
|
||||||
|
"\uf7f0\uf7f1\uf7f2\uf7f3\uf7f4\uf7f5\uf7f6\uf7f7" +
|
||||||
|
"\uf7f8\uf7f9\uf7fa\uf7fb\uf7fc\uf7fd\uf7fe\uf7ff",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func getWHATWG(url string) string {
|
||||||
|
res, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("%q: Get: %v", url, err)
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
mapping := make([]rune, 128)
|
||||||
|
for i := range mapping {
|
||||||
|
mapping[i] = '\ufffd'
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(res.Body)
|
||||||
|
for scanner.Scan() {
|
||||||
|
s := strings.TrimSpace(scanner.Text())
|
||||||
|
if s == "" || s[0] == '#' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
x, y := 0, 0
|
||||||
|
if _, err := fmt.Sscanf(s, "%d\t0x%x", &x, &y); err != nil {
|
||||||
|
log.Fatalf("could not parse %q", s)
|
||||||
|
}
|
||||||
|
if x < 0 || 128 <= x {
|
||||||
|
log.Fatalf("code %d is out of range", x)
|
||||||
|
}
|
||||||
|
if 0x80 <= y && y < 0xa0 {
|
||||||
|
// We diverge from the WHATWG spec by mapping control characters
|
||||||
|
// in the range [0x80, 0xa0) to U+FFFD.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
mapping[x] = rune(y)
|
||||||
|
}
|
||||||
|
return ascii + string(mapping)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUCM(url string) string {
|
||||||
|
res, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("%q: Get: %v", url, err)
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
mapping := make([]rune, 256)
|
||||||
|
for i := range mapping {
|
||||||
|
mapping[i] = '\ufffd'
|
||||||
|
}
|
||||||
|
|
||||||
|
charsFound := 0
|
||||||
|
scanner := bufio.NewScanner(res.Body)
|
||||||
|
for scanner.Scan() {
|
||||||
|
s := strings.TrimSpace(scanner.Text())
|
||||||
|
if s == "" || s[0] == '#' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var c byte
|
||||||
|
var r rune
|
||||||
|
if _, err := fmt.Sscanf(s, `<U%x> \x%x |0`, &r, &c); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
mapping[c] = r
|
||||||
|
charsFound++
|
||||||
|
}
|
||||||
|
|
||||||
|
if charsFound < 200 {
|
||||||
|
log.Fatalf("%q: only %d characters found (wrong page format?)", url, charsFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(mapping)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
mibs := map[string]bool{}
|
||||||
|
all := []string{}
|
||||||
|
|
||||||
|
w := gen.NewCodeWriter()
|
||||||
|
defer w.WriteGoFile("tables.go", "charmap")
|
||||||
|
|
||||||
|
printf := func(s string, a ...interface{}) { fmt.Fprintf(w, s, a...) }
|
||||||
|
|
||||||
|
printf("import (\n")
|
||||||
|
printf("\t\"golang.org/x/text/encoding\"\n")
|
||||||
|
printf("\t\"golang.org/x/text/encoding/internal/identifier\"\n")
|
||||||
|
printf(")\n\n")
|
||||||
|
for _, e := range encodings {
|
||||||
|
varNames := strings.Split(e.varName, ",")
|
||||||
|
all = append(all, varNames...)
|
||||||
|
varName := varNames[0]
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(e.mapping, "http://encoding.spec.whatwg.org/"):
|
||||||
|
e.mapping = getWHATWG(e.mapping)
|
||||||
|
case strings.HasPrefix(e.mapping, "http://source.icu-project.org/repos/icu/data/trunk/charset/data/ucm/"):
|
||||||
|
e.mapping = getUCM(e.mapping)
|
||||||
|
}
|
||||||
|
|
||||||
|
asciiSuperset, low := strings.HasPrefix(e.mapping, ascii), 0x00
|
||||||
|
if asciiSuperset {
|
||||||
|
low = 0x80
|
||||||
|
}
|
||||||
|
lvn := 1
|
||||||
|
if strings.HasPrefix(varName, "ISO") || strings.HasPrefix(varName, "KOI") {
|
||||||
|
lvn = 3
|
||||||
|
}
|
||||||
|
lowerVarName := strings.ToLower(varName[:lvn]) + varName[lvn:]
|
||||||
|
printf("// %s is the %s encoding.\n", varName, e.name)
|
||||||
|
if e.comment != "" {
|
||||||
|
printf("//\n// %s\n", e.comment)
|
||||||
|
}
|
||||||
|
printf("var %s *Charmap = &%s\n\nvar %s = Charmap{\nname: %q,\n",
|
||||||
|
varName, lowerVarName, lowerVarName, e.name)
|
||||||
|
if mibs[e.mib] {
|
||||||
|
log.Fatalf("MIB type %q declared multiple times.", e.mib)
|
||||||
|
}
|
||||||
|
printf("mib: identifier.%s,\n", e.mib)
|
||||||
|
printf("asciiSuperset: %t,\n", asciiSuperset)
|
||||||
|
printf("low: 0x%02x,\n", low)
|
||||||
|
printf("replacement: 0x%02x,\n", e.replacement)
|
||||||
|
|
||||||
|
printf("decode: [256]utf8Enc{\n")
|
||||||
|
i, backMapping := 0, map[rune]byte{}
|
||||||
|
for _, c := range e.mapping {
|
||||||
|
if _, ok := backMapping[c]; !ok && c != utf8.RuneError {
|
||||||
|
backMapping[c] = byte(i)
|
||||||
|
}
|
||||||
|
var buf [8]byte
|
||||||
|
n := utf8.EncodeRune(buf[:], c)
|
||||||
|
if n > 3 {
|
||||||
|
panic(fmt.Sprintf("rune %q (%U) is too long", c, c))
|
||||||
|
}
|
||||||
|
printf("{%d,[3]byte{0x%02x,0x%02x,0x%02x}},", n, buf[0], buf[1], buf[2])
|
||||||
|
if i%2 == 1 {
|
||||||
|
printf("\n")
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
printf("},\n")
|
||||||
|
|
||||||
|
printf("encode: [256]uint32{\n")
|
||||||
|
encode := make([]uint32, 0, 256)
|
||||||
|
for c, i := range backMapping {
|
||||||
|
encode = append(encode, uint32(i)<<24|uint32(c))
|
||||||
|
}
|
||||||
|
sort.Sort(byRune(encode))
|
||||||
|
for len(encode) < cap(encode) {
|
||||||
|
encode = append(encode, encode[len(encode)-1])
|
||||||
|
}
|
||||||
|
for i, enc := range encode {
|
||||||
|
printf("0x%08x,", enc)
|
||||||
|
if i%8 == 7 {
|
||||||
|
printf("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("},\n}\n")
|
||||||
|
|
||||||
|
// Add an estimate of the size of a single Charmap{} struct value, which
|
||||||
|
// includes two 256 elem arrays of 4 bytes and some extra fields, which
|
||||||
|
// align to 3 uint64s on 64-bit architectures.
|
||||||
|
w.Size += 2*4*256 + 3*8
|
||||||
|
}
|
||||||
|
// TODO: add proper line breaking.
|
||||||
|
printf("var listAll = []encoding.Encoding{\n%s,\n}\n\n", strings.Join(all, ",\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
type byRune []uint32
|
||||||
|
|
||||||
|
func (b byRune) Len() int { return len(b) }
|
||||||
|
func (b byRune) Less(i, j int) bool { return b[i]&0xffffff < b[j]&0xffffff }
|
||||||
|
func (b byRune) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
7410
vendor/golang.org/x/text/encoding/charmap/tables.go
generated
vendored
Normal file
7410
vendor/golang.org/x/text/encoding/charmap/tables.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
335
vendor/golang.org/x/text/encoding/encoding.go
generated
vendored
Normal file
335
vendor/golang.org/x/text/encoding/encoding.go
generated
vendored
Normal file
@ -0,0 +1,335 @@
|
|||||||
|
// Copyright 2013 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package encoding defines an interface for character encodings, such as Shift
|
||||||
|
// JIS and Windows 1252, that can convert to and from UTF-8.
|
||||||
|
//
|
||||||
|
// Encoding implementations are provided in other packages, such as
|
||||||
|
// golang.org/x/text/encoding/charmap and
|
||||||
|
// golang.org/x/text/encoding/japanese.
|
||||||
|
package encoding // import "golang.org/x/text/encoding"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"strconv"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"golang.org/x/text/encoding/internal/identifier"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// - There seems to be some inconsistency in when decoders return errors
|
||||||
|
// and when not. Also documentation seems to suggest they shouldn't return
|
||||||
|
// errors at all (except for UTF-16).
|
||||||
|
// - Encoders seem to rely on or at least benefit from the input being in NFC
|
||||||
|
// normal form. Perhaps add an example how users could prepare their output.
|
||||||
|
|
||||||
|
// Encoding is a character set encoding that can be transformed to and from
|
||||||
|
// UTF-8.
|
||||||
|
type Encoding interface {
|
||||||
|
// NewDecoder returns a Decoder.
|
||||||
|
NewDecoder() *Decoder
|
||||||
|
|
||||||
|
// NewEncoder returns an Encoder.
|
||||||
|
NewEncoder() *Encoder
|
||||||
|
}
|
||||||
|
|
||||||
|
// A Decoder converts bytes to UTF-8. It implements transform.Transformer.
|
||||||
|
//
|
||||||
|
// Transforming source bytes that are not of that encoding will not result in an
|
||||||
|
// error per se. Each byte that cannot be transcoded will be represented in the
|
||||||
|
// output by the UTF-8 encoding of '\uFFFD', the replacement rune.
|
||||||
|
type Decoder struct {
|
||||||
|
transform.Transformer
|
||||||
|
|
||||||
|
// This forces external creators of Decoders to use names in struct
|
||||||
|
// initializers, allowing for future extendibility without having to break
|
||||||
|
// code.
|
||||||
|
_ struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bytes converts the given encoded bytes to UTF-8. It returns the converted
|
||||||
|
// bytes or nil, err if any error occurred.
|
||||||
|
func (d *Decoder) Bytes(b []byte) ([]byte, error) {
|
||||||
|
b, _, err := transform.Bytes(d, b)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String converts the given encoded string to UTF-8. It returns the converted
|
||||||
|
// string or "", err if any error occurred.
|
||||||
|
func (d *Decoder) String(s string) (string, error) {
|
||||||
|
s, _, err := transform.String(d, s)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reader wraps another Reader to decode its bytes.
|
||||||
|
//
|
||||||
|
// The Decoder may not be used for any other operation as long as the returned
|
||||||
|
// Reader is in use.
|
||||||
|
func (d *Decoder) Reader(r io.Reader) io.Reader {
|
||||||
|
return transform.NewReader(r, d)
|
||||||
|
}
|
||||||
|
|
||||||
|
// An Encoder converts bytes from UTF-8. It implements transform.Transformer.
|
||||||
|
//
|
||||||
|
// Each rune that cannot be transcoded will result in an error. In this case,
|
||||||
|
// the transform will consume all source byte up to, not including the offending
|
||||||
|
// rune. Transforming source bytes that are not valid UTF-8 will be replaced by
|
||||||
|
// `\uFFFD`. To return early with an error instead, use transform.Chain to
|
||||||
|
// preprocess the data with a UTF8Validator.
|
||||||
|
type Encoder struct {
|
||||||
|
transform.Transformer
|
||||||
|
|
||||||
|
// This forces external creators of Encoders to use names in struct
|
||||||
|
// initializers, allowing for future extendibility without having to break
|
||||||
|
// code.
|
||||||
|
_ struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bytes converts bytes from UTF-8. It returns the converted bytes or nil, err if
|
||||||
|
// any error occurred.
|
||||||
|
func (e *Encoder) Bytes(b []byte) ([]byte, error) {
|
||||||
|
b, _, err := transform.Bytes(e, b)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String converts a string from UTF-8. It returns the converted string or
|
||||||
|
// "", err if any error occurred.
|
||||||
|
func (e *Encoder) String(s string) (string, error) {
|
||||||
|
s, _, err := transform.String(e, s)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writer wraps another Writer to encode its UTF-8 output.
|
||||||
|
//
|
||||||
|
// The Encoder may not be used for any other operation as long as the returned
|
||||||
|
// Writer is in use.
|
||||||
|
func (e *Encoder) Writer(w io.Writer) io.Writer {
|
||||||
|
return transform.NewWriter(w, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ASCIISub is the ASCII substitute character, as recommended by
|
||||||
|
// https://unicode.org/reports/tr36/#Text_Comparison
|
||||||
|
const ASCIISub = '\x1a'
|
||||||
|
|
||||||
|
// Nop is the nop encoding. Its transformed bytes are the same as the source
|
||||||
|
// bytes; it does not replace invalid UTF-8 sequences.
|
||||||
|
var Nop Encoding = nop{}
|
||||||
|
|
||||||
|
type nop struct{}
|
||||||
|
|
||||||
|
func (nop) NewDecoder() *Decoder {
|
||||||
|
return &Decoder{Transformer: transform.Nop}
|
||||||
|
}
|
||||||
|
func (nop) NewEncoder() *Encoder {
|
||||||
|
return &Encoder{Transformer: transform.Nop}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replacement is the replacement encoding. Decoding from the replacement
|
||||||
|
// encoding yields a single '\uFFFD' replacement rune. Encoding from UTF-8 to
|
||||||
|
// the replacement encoding yields the same as the source bytes except that
|
||||||
|
// invalid UTF-8 is converted to '\uFFFD'.
|
||||||
|
//
|
||||||
|
// It is defined at http://encoding.spec.whatwg.org/#replacement
|
||||||
|
var Replacement Encoding = replacement{}
|
||||||
|
|
||||||
|
type replacement struct{}
|
||||||
|
|
||||||
|
func (replacement) NewDecoder() *Decoder {
|
||||||
|
return &Decoder{Transformer: replacementDecoder{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (replacement) NewEncoder() *Encoder {
|
||||||
|
return &Encoder{Transformer: replacementEncoder{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (replacement) ID() (mib identifier.MIB, other string) {
|
||||||
|
return identifier.Replacement, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
type replacementDecoder struct{ transform.NopResetter }
|
||||||
|
|
||||||
|
func (replacementDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
|
||||||
|
if len(dst) < 3 {
|
||||||
|
return 0, 0, transform.ErrShortDst
|
||||||
|
}
|
||||||
|
if atEOF {
|
||||||
|
const fffd = "\ufffd"
|
||||||
|
dst[0] = fffd[0]
|
||||||
|
dst[1] = fffd[1]
|
||||||
|
dst[2] = fffd[2]
|
||||||
|
nDst = 3
|
||||||
|
}
|
||||||
|
return nDst, len(src), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type replacementEncoder struct{ transform.NopResetter }
|
||||||
|
|
||||||
|
func (replacementEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
|
||||||
|
r, size := rune(0), 0
|
||||||
|
|
||||||
|
for ; nSrc < len(src); nSrc += size {
|
||||||
|
r = rune(src[nSrc])
|
||||||
|
|
||||||
|
// Decode a 1-byte rune.
|
||||||
|
if r < utf8.RuneSelf {
|
||||||
|
size = 1
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Decode a multi-byte rune.
|
||||||
|
r, size = utf8.DecodeRune(src[nSrc:])
|
||||||
|
if size == 1 {
|
||||||
|
// All valid runes of size 1 (those below utf8.RuneSelf) were
|
||||||
|
// handled above. We have invalid UTF-8 or we haven't seen the
|
||||||
|
// full character yet.
|
||||||
|
if !atEOF && !utf8.FullRune(src[nSrc:]) {
|
||||||
|
err = transform.ErrShortSrc
|
||||||
|
break
|
||||||
|
}
|
||||||
|
r = '\ufffd'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if nDst+utf8.RuneLen(r) > len(dst) {
|
||||||
|
err = transform.ErrShortDst
|
||||||
|
break
|
||||||
|
}
|
||||||
|
nDst += utf8.EncodeRune(dst[nDst:], r)
|
||||||
|
}
|
||||||
|
return nDst, nSrc, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTMLEscapeUnsupported wraps encoders to replace source runes outside the
|
||||||
|
// repertoire of the destination encoding with HTML escape sequences.
|
||||||
|
//
|
||||||
|
// This wrapper exists to comply to URL and HTML forms requiring a
|
||||||
|
// non-terminating legacy encoder. The produced sequences may lead to data
|
||||||
|
// loss as they are indistinguishable from legitimate input. To avoid this
|
||||||
|
// issue, use UTF-8 encodings whenever possible.
|
||||||
|
func HTMLEscapeUnsupported(e *Encoder) *Encoder {
|
||||||
|
return &Encoder{Transformer: &errorHandler{e, errorToHTML}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceUnsupported wraps encoders to replace source runes outside the
|
||||||
|
// repertoire of the destination encoding with an encoding-specific
|
||||||
|
// replacement.
|
||||||
|
//
|
||||||
|
// This wrapper is only provided for backwards compatibility and legacy
|
||||||
|
// handling. Its use is strongly discouraged. Use UTF-8 whenever possible.
|
||||||
|
func ReplaceUnsupported(e *Encoder) *Encoder {
|
||||||
|
return &Encoder{Transformer: &errorHandler{e, errorToReplacement}}
|
||||||
|
}
|
||||||
|
|
||||||
|
type errorHandler struct {
|
||||||
|
*Encoder
|
||||||
|
handler func(dst []byte, r rune, err repertoireError) (n int, ok bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: consider making this error public in some form.
|
||||||
|
type repertoireError interface {
|
||||||
|
Replacement() byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h errorHandler) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
|
||||||
|
nDst, nSrc, err = h.Transformer.Transform(dst, src, atEOF)
|
||||||
|
for err != nil {
|
||||||
|
rerr, ok := err.(repertoireError)
|
||||||
|
if !ok {
|
||||||
|
return nDst, nSrc, err
|
||||||
|
}
|
||||||
|
r, sz := utf8.DecodeRune(src[nSrc:])
|
||||||
|
n, ok := h.handler(dst[nDst:], r, rerr)
|
||||||
|
if !ok {
|
||||||
|
return nDst, nSrc, transform.ErrShortDst
|
||||||
|
}
|
||||||
|
err = nil
|
||||||
|
nDst += n
|
||||||
|
if nSrc += sz; nSrc < len(src) {
|
||||||
|
var dn, sn int
|
||||||
|
dn, sn, err = h.Transformer.Transform(dst[nDst:], src[nSrc:], atEOF)
|
||||||
|
nDst += dn
|
||||||
|
nSrc += sn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nDst, nSrc, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func errorToHTML(dst []byte, r rune, err repertoireError) (n int, ok bool) {
|
||||||
|
buf := [8]byte{}
|
||||||
|
b := strconv.AppendUint(buf[:0], uint64(r), 10)
|
||||||
|
if n = len(b) + len("&#;"); n >= len(dst) {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
dst[0] = '&'
|
||||||
|
dst[1] = '#'
|
||||||
|
dst[copy(dst[2:], b)+2] = ';'
|
||||||
|
return n, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func errorToReplacement(dst []byte, r rune, err repertoireError) (n int, ok bool) {
|
||||||
|
if len(dst) == 0 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
dst[0] = err.Replacement()
|
||||||
|
return 1, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrInvalidUTF8 means that a transformer encountered invalid UTF-8.
|
||||||
|
var ErrInvalidUTF8 = errors.New("encoding: invalid UTF-8")
|
||||||
|
|
||||||
|
// UTF8Validator is a transformer that returns ErrInvalidUTF8 on the first
|
||||||
|
// input byte that is not valid UTF-8.
|
||||||
|
var UTF8Validator transform.Transformer = utf8Validator{}
|
||||||
|
|
||||||
|
type utf8Validator struct{ transform.NopResetter }
|
||||||
|
|
||||||
|
func (utf8Validator) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
|
||||||
|
n := len(src)
|
||||||
|
if n > len(dst) {
|
||||||
|
n = len(dst)
|
||||||
|
}
|
||||||
|
for i := 0; i < n; {
|
||||||
|
if c := src[i]; c < utf8.RuneSelf {
|
||||||
|
dst[i] = c
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_, size := utf8.DecodeRune(src[i:])
|
||||||
|
if size == 1 {
|
||||||
|
// All valid runes of size 1 (those below utf8.RuneSelf) were
|
||||||
|
// handled above. We have invalid UTF-8 or we haven't seen the
|
||||||
|
// full character yet.
|
||||||
|
err = ErrInvalidUTF8
|
||||||
|
if !atEOF && !utf8.FullRune(src[i:]) {
|
||||||
|
err = transform.ErrShortSrc
|
||||||
|
}
|
||||||
|
return i, i, err
|
||||||
|
}
|
||||||
|
if i+size > len(dst) {
|
||||||
|
return i, i, transform.ErrShortDst
|
||||||
|
}
|
||||||
|
for ; size > 0; size-- {
|
||||||
|
dst[i] = src[i]
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(src) > len(dst) {
|
||||||
|
err = transform.ErrShortDst
|
||||||
|
}
|
||||||
|
return n, n, err
|
||||||
|
}
|
173
vendor/golang.org/x/text/encoding/htmlindex/gen.go
generated
vendored
Normal file
173
vendor/golang.org/x/text/encoding/htmlindex/gen.go
generated
vendored
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/text/internal/gen"
|
||||||
|
)
|
||||||
|
|
||||||
|
type group struct {
|
||||||
|
Encodings []struct {
|
||||||
|
Labels []string
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
gen.Init()
|
||||||
|
|
||||||
|
r := gen.Open("https://encoding.spec.whatwg.org", "whatwg", "encodings.json")
|
||||||
|
var groups []group
|
||||||
|
if err := json.NewDecoder(r).Decode(&groups); err != nil {
|
||||||
|
log.Fatalf("Error reading encodings.json: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := &bytes.Buffer{}
|
||||||
|
fmt.Fprintln(w, "type htmlEncoding byte")
|
||||||
|
fmt.Fprintln(w, "const (")
|
||||||
|
for i, g := range groups {
|
||||||
|
for _, e := range g.Encodings {
|
||||||
|
key := strings.ToLower(e.Name)
|
||||||
|
name := consts[key]
|
||||||
|
if name == "" {
|
||||||
|
log.Fatalf("No const defined for %s.", key)
|
||||||
|
}
|
||||||
|
if i == 0 {
|
||||||
|
fmt.Fprintf(w, "%s htmlEncoding = iota\n", name)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(w, "%s\n", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Fprintln(w, "numEncodings")
|
||||||
|
fmt.Fprint(w, ")\n\n")
|
||||||
|
|
||||||
|
fmt.Fprintln(w, "var canonical = [numEncodings]string{")
|
||||||
|
for _, g := range groups {
|
||||||
|
for _, e := range g.Encodings {
|
||||||
|
fmt.Fprintf(w, "%q,\n", strings.ToLower(e.Name))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, "}\n\n")
|
||||||
|
|
||||||
|
fmt.Fprintln(w, "var nameMap = map[string]htmlEncoding{")
|
||||||
|
for _, g := range groups {
|
||||||
|
for _, e := range g.Encodings {
|
||||||
|
for _, l := range e.Labels {
|
||||||
|
key := strings.ToLower(e.Name)
|
||||||
|
name := consts[key]
|
||||||
|
fmt.Fprintf(w, "%q: %s,\n", l, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, "}\n\n")
|
||||||
|
|
||||||
|
var tags []string
|
||||||
|
fmt.Fprintln(w, "var localeMap = []htmlEncoding{")
|
||||||
|
for _, loc := range locales {
|
||||||
|
tags = append(tags, loc.tag)
|
||||||
|
fmt.Fprintf(w, "%s, // %s \n", consts[loc.name], loc.tag)
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, "}\n\n")
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "const locales = %q\n", strings.Join(tags, " "))
|
||||||
|
|
||||||
|
gen.WriteGoFile("tables.go", "htmlindex", w.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
// consts maps canonical encoding name to internal constant.
|
||||||
|
var consts = map[string]string{
|
||||||
|
"utf-8": "utf8",
|
||||||
|
"ibm866": "ibm866",
|
||||||
|
"iso-8859-2": "iso8859_2",
|
||||||
|
"iso-8859-3": "iso8859_3",
|
||||||
|
"iso-8859-4": "iso8859_4",
|
||||||
|
"iso-8859-5": "iso8859_5",
|
||||||
|
"iso-8859-6": "iso8859_6",
|
||||||
|
"iso-8859-7": "iso8859_7",
|
||||||
|
"iso-8859-8": "iso8859_8",
|
||||||
|
"iso-8859-8-i": "iso8859_8I",
|
||||||
|
"iso-8859-10": "iso8859_10",
|
||||||
|
"iso-8859-13": "iso8859_13",
|
||||||
|
"iso-8859-14": "iso8859_14",
|
||||||
|
"iso-8859-15": "iso8859_15",
|
||||||
|
"iso-8859-16": "iso8859_16",
|
||||||
|
"koi8-r": "koi8r",
|
||||||
|
"koi8-u": "koi8u",
|
||||||
|
"macintosh": "macintosh",
|
||||||
|
"windows-874": "windows874",
|
||||||
|
"windows-1250": "windows1250",
|
||||||
|
"windows-1251": "windows1251",
|
||||||
|
"windows-1252": "windows1252",
|
||||||
|
"windows-1253": "windows1253",
|
||||||
|
"windows-1254": "windows1254",
|
||||||
|
"windows-1255": "windows1255",
|
||||||
|
"windows-1256": "windows1256",
|
||||||
|
"windows-1257": "windows1257",
|
||||||
|
"windows-1258": "windows1258",
|
||||||
|
"x-mac-cyrillic": "macintoshCyrillic",
|
||||||
|
"gbk": "gbk",
|
||||||
|
"gb18030": "gb18030",
|
||||||
|
// "hz-gb-2312": "hzgb2312", // Was removed from WhatWG
|
||||||
|
"big5": "big5",
|
||||||
|
"euc-jp": "eucjp",
|
||||||
|
"iso-2022-jp": "iso2022jp",
|
||||||
|
"shift_jis": "shiftJIS",
|
||||||
|
"euc-kr": "euckr",
|
||||||
|
"replacement": "replacement",
|
||||||
|
"utf-16be": "utf16be",
|
||||||
|
"utf-16le": "utf16le",
|
||||||
|
"x-user-defined": "xUserDefined",
|
||||||
|
}
|
||||||
|
|
||||||
|
// locales is taken from
|
||||||
|
// https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm.
|
||||||
|
var locales = []struct{ tag, name string }{
|
||||||
|
// The default value. Explicitly state latin to benefit from the exact
|
||||||
|
// script option, while still making 1252 the default encoding for languages
|
||||||
|
// written in Latin script.
|
||||||
|
{"und_Latn", "windows-1252"},
|
||||||
|
{"ar", "windows-1256"},
|
||||||
|
{"ba", "windows-1251"},
|
||||||
|
{"be", "windows-1251"},
|
||||||
|
{"bg", "windows-1251"},
|
||||||
|
{"cs", "windows-1250"},
|
||||||
|
{"el", "iso-8859-7"},
|
||||||
|
{"et", "windows-1257"},
|
||||||
|
{"fa", "windows-1256"},
|
||||||
|
{"he", "windows-1255"},
|
||||||
|
{"hr", "windows-1250"},
|
||||||
|
{"hu", "iso-8859-2"},
|
||||||
|
{"ja", "shift_jis"},
|
||||||
|
{"kk", "windows-1251"},
|
||||||
|
{"ko", "euc-kr"},
|
||||||
|
{"ku", "windows-1254"},
|
||||||
|
{"ky", "windows-1251"},
|
||||||
|
{"lt", "windows-1257"},
|
||||||
|
{"lv", "windows-1257"},
|
||||||
|
{"mk", "windows-1251"},
|
||||||
|
{"pl", "iso-8859-2"},
|
||||||
|
{"ru", "windows-1251"},
|
||||||
|
{"sah", "windows-1251"},
|
||||||
|
{"sk", "windows-1250"},
|
||||||
|
{"sl", "iso-8859-2"},
|
||||||
|
{"sr", "windows-1251"},
|
||||||
|
{"tg", "windows-1251"},
|
||||||
|
{"th", "windows-874"},
|
||||||
|
{"tr", "windows-1254"},
|
||||||
|
{"tt", "windows-1251"},
|
||||||
|
{"uk", "windows-1251"},
|
||||||
|
{"vi", "windows-1258"},
|
||||||
|
{"zh-hans", "gb18030"},
|
||||||
|
{"zh-hant", "big5"},
|
||||||
|
}
|
86
vendor/golang.org/x/text/encoding/htmlindex/htmlindex.go
generated
vendored
Normal file
86
vendor/golang.org/x/text/encoding/htmlindex/htmlindex.go
generated
vendored
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
//go:generate go run gen.go
|
||||||
|
|
||||||
|
// Package htmlindex maps character set encoding names to Encodings as
|
||||||
|
// recommended by the W3C for use in HTML 5. See http://www.w3.org/TR/encoding.
|
||||||
|
package htmlindex
|
||||||
|
|
||||||
|
// TODO: perhaps have a "bare" version of the index (used by this package) that
|
||||||
|
// is not pre-loaded with all encodings. Global variables in encodings prevent
|
||||||
|
// the linker from being able to purge unneeded tables. This means that
|
||||||
|
// referencing all encodings, as this package does for the default index, links
|
||||||
|
// in all encodings unconditionally.
|
||||||
|
//
|
||||||
|
// This issue can be solved by either solving the linking issue (see
|
||||||
|
// https://github.com/golang/go/issues/6330) or refactoring the encoding tables
|
||||||
|
// (e.g. moving the tables to internal packages that do not use global
|
||||||
|
// variables).
|
||||||
|
|
||||||
|
// TODO: allow canonicalizing names
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"golang.org/x/text/encoding"
|
||||||
|
"golang.org/x/text/encoding/internal/identifier"
|
||||||
|
"golang.org/x/text/language"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errInvalidName = errors.New("htmlindex: invalid encoding name")
|
||||||
|
errUnknown = errors.New("htmlindex: unknown Encoding")
|
||||||
|
errUnsupported = errors.New("htmlindex: this encoding is not supported")
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
matcherOnce sync.Once
|
||||||
|
matcher language.Matcher
|
||||||
|
)
|
||||||
|
|
||||||
|
// LanguageDefault returns the canonical name of the default encoding for a
|
||||||
|
// given language.
|
||||||
|
func LanguageDefault(tag language.Tag) string {
|
||||||
|
matcherOnce.Do(func() {
|
||||||
|
tags := []language.Tag{}
|
||||||
|
for _, t := range strings.Split(locales, " ") {
|
||||||
|
tags = append(tags, language.MustParse(t))
|
||||||
|
}
|
||||||
|
matcher = language.NewMatcher(tags, language.PreferSameScript(true))
|
||||||
|
})
|
||||||
|
_, i, _ := matcher.Match(tag)
|
||||||
|
return canonical[localeMap[i]] // Default is Windows-1252.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns an Encoding for one of the names listed in
|
||||||
|
// http://www.w3.org/TR/encoding using the Default Index. Matching is case-
|
||||||
|
// insensitive.
|
||||||
|
func Get(name string) (encoding.Encoding, error) {
|
||||||
|
x, ok := nameMap[strings.ToLower(strings.TrimSpace(name))]
|
||||||
|
if !ok {
|
||||||
|
return nil, errInvalidName
|
||||||
|
}
|
||||||
|
return encodings[x], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name reports the canonical name of the given Encoding. It will return
|
||||||
|
// an error if e is not associated with a supported encoding scheme.
|
||||||
|
func Name(e encoding.Encoding) (string, error) {
|
||||||
|
id, ok := e.(identifier.Interface)
|
||||||
|
if !ok {
|
||||||
|
return "", errUnknown
|
||||||
|
}
|
||||||
|
mib, _ := id.ID()
|
||||||
|
if mib == 0 {
|
||||||
|
return "", errUnknown
|
||||||
|
}
|
||||||
|
v, ok := mibMap[mib]
|
||||||
|
if !ok {
|
||||||
|
return "", errUnsupported
|
||||||
|
}
|
||||||
|
return canonical[v], nil
|
||||||
|
}
|
105
vendor/golang.org/x/text/encoding/htmlindex/map.go
generated
vendored
Normal file
105
vendor/golang.org/x/text/encoding/htmlindex/map.go
generated
vendored
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package htmlindex
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/text/encoding"
|
||||||
|
"golang.org/x/text/encoding/charmap"
|
||||||
|
"golang.org/x/text/encoding/internal/identifier"
|
||||||
|
"golang.org/x/text/encoding/japanese"
|
||||||
|
"golang.org/x/text/encoding/korean"
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
|
"golang.org/x/text/encoding/traditionalchinese"
|
||||||
|
"golang.org/x/text/encoding/unicode"
|
||||||
|
)
|
||||||
|
|
||||||
|
// mibMap maps a MIB identifier to an htmlEncoding index.
|
||||||
|
var mibMap = map[identifier.MIB]htmlEncoding{
|
||||||
|
identifier.UTF8: utf8,
|
||||||
|
identifier.UTF16BE: utf16be,
|
||||||
|
identifier.UTF16LE: utf16le,
|
||||||
|
identifier.IBM866: ibm866,
|
||||||
|
identifier.ISOLatin2: iso8859_2,
|
||||||
|
identifier.ISOLatin3: iso8859_3,
|
||||||
|
identifier.ISOLatin4: iso8859_4,
|
||||||
|
identifier.ISOLatinCyrillic: iso8859_5,
|
||||||
|
identifier.ISOLatinArabic: iso8859_6,
|
||||||
|
identifier.ISOLatinGreek: iso8859_7,
|
||||||
|
identifier.ISOLatinHebrew: iso8859_8,
|
||||||
|
identifier.ISO88598I: iso8859_8I,
|
||||||
|
identifier.ISOLatin6: iso8859_10,
|
||||||
|
identifier.ISO885913: iso8859_13,
|
||||||
|
identifier.ISO885914: iso8859_14,
|
||||||
|
identifier.ISO885915: iso8859_15,
|
||||||
|
identifier.ISO885916: iso8859_16,
|
||||||
|
identifier.KOI8R: koi8r,
|
||||||
|
identifier.KOI8U: koi8u,
|
||||||
|
identifier.Macintosh: macintosh,
|
||||||
|
identifier.MacintoshCyrillic: macintoshCyrillic,
|
||||||
|
identifier.Windows874: windows874,
|
||||||
|
identifier.Windows1250: windows1250,
|
||||||
|
identifier.Windows1251: windows1251,
|
||||||
|
identifier.Windows1252: windows1252,
|
||||||
|
identifier.Windows1253: windows1253,
|
||||||
|
identifier.Windows1254: windows1254,
|
||||||
|
identifier.Windows1255: windows1255,
|
||||||
|
identifier.Windows1256: windows1256,
|
||||||
|
identifier.Windows1257: windows1257,
|
||||||
|
identifier.Windows1258: windows1258,
|
||||||
|
identifier.XUserDefined: xUserDefined,
|
||||||
|
identifier.GBK: gbk,
|
||||||
|
identifier.GB18030: gb18030,
|
||||||
|
identifier.Big5: big5,
|
||||||
|
identifier.EUCPkdFmtJapanese: eucjp,
|
||||||
|
identifier.ISO2022JP: iso2022jp,
|
||||||
|
identifier.ShiftJIS: shiftJIS,
|
||||||
|
identifier.EUCKR: euckr,
|
||||||
|
identifier.Replacement: replacement,
|
||||||
|
}
|
||||||
|
|
||||||
|
// encodings maps the internal htmlEncoding to an Encoding.
|
||||||
|
// TODO: consider using a reusable index in encoding/internal.
|
||||||
|
var encodings = [numEncodings]encoding.Encoding{
|
||||||
|
utf8: unicode.UTF8,
|
||||||
|
ibm866: charmap.CodePage866,
|
||||||
|
iso8859_2: charmap.ISO8859_2,
|
||||||
|
iso8859_3: charmap.ISO8859_3,
|
||||||
|
iso8859_4: charmap.ISO8859_4,
|
||||||
|
iso8859_5: charmap.ISO8859_5,
|
||||||
|
iso8859_6: charmap.ISO8859_6,
|
||||||
|
iso8859_7: charmap.ISO8859_7,
|
||||||
|
iso8859_8: charmap.ISO8859_8,
|
||||||
|
iso8859_8I: charmap.ISO8859_8I,
|
||||||
|
iso8859_10: charmap.ISO8859_10,
|
||||||
|
iso8859_13: charmap.ISO8859_13,
|
||||||
|
iso8859_14: charmap.ISO8859_14,
|
||||||
|
iso8859_15: charmap.ISO8859_15,
|
||||||
|
iso8859_16: charmap.ISO8859_16,
|
||||||
|
koi8r: charmap.KOI8R,
|
||||||
|
koi8u: charmap.KOI8U,
|
||||||
|
macintosh: charmap.Macintosh,
|
||||||
|
windows874: charmap.Windows874,
|
||||||
|
windows1250: charmap.Windows1250,
|
||||||
|
windows1251: charmap.Windows1251,
|
||||||
|
windows1252: charmap.Windows1252,
|
||||||
|
windows1253: charmap.Windows1253,
|
||||||
|
windows1254: charmap.Windows1254,
|
||||||
|
windows1255: charmap.Windows1255,
|
||||||
|
windows1256: charmap.Windows1256,
|
||||||
|
windows1257: charmap.Windows1257,
|
||||||
|
windows1258: charmap.Windows1258,
|
||||||
|
macintoshCyrillic: charmap.MacintoshCyrillic,
|
||||||
|
gbk: simplifiedchinese.GBK,
|
||||||
|
gb18030: simplifiedchinese.GB18030,
|
||||||
|
big5: traditionalchinese.Big5,
|
||||||
|
eucjp: japanese.EUCJP,
|
||||||
|
iso2022jp: japanese.ISO2022JP,
|
||||||
|
shiftJIS: japanese.ShiftJIS,
|
||||||
|
euckr: korean.EUCKR,
|
||||||
|
replacement: encoding.Replacement,
|
||||||
|
utf16be: unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM),
|
||||||
|
utf16le: unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM),
|
||||||
|
xUserDefined: charmap.XUserDefined,
|
||||||
|
}
|
353
vendor/golang.org/x/text/encoding/htmlindex/tables.go
generated
vendored
Normal file
353
vendor/golang.org/x/text/encoding/htmlindex/tables.go
generated
vendored
Normal file
@ -0,0 +1,353 @@
|
|||||||
|
// Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
|
||||||
|
|
||||||
|
package htmlindex
|
||||||
|
|
||||||
|
type htmlEncoding byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
utf8 htmlEncoding = iota
|
||||||
|
ibm866
|
||||||
|
iso8859_2
|
||||||
|
iso8859_3
|
||||||
|
iso8859_4
|
||||||
|
iso8859_5
|
||||||
|
iso8859_6
|
||||||
|
iso8859_7
|
||||||
|
iso8859_8
|
||||||
|
iso8859_8I
|
||||||
|
iso8859_10
|
||||||
|
iso8859_13
|
||||||
|
iso8859_14
|
||||||
|
iso8859_15
|
||||||
|
iso8859_16
|
||||||
|
koi8r
|
||||||
|
koi8u
|
||||||
|
macintosh
|
||||||
|
windows874
|
||||||
|
windows1250
|
||||||
|
windows1251
|
||||||
|
windows1252
|
||||||
|
windows1253
|
||||||
|
windows1254
|
||||||
|
windows1255
|
||||||
|
windows1256
|
||||||
|
windows1257
|
||||||
|
windows1258
|
||||||
|
macintoshCyrillic
|
||||||
|
gbk
|
||||||
|
gb18030
|
||||||
|
big5
|
||||||
|
eucjp
|
||||||
|
iso2022jp
|
||||||
|
shiftJIS
|
||||||
|
euckr
|
||||||
|
replacement
|
||||||
|
utf16be
|
||||||
|
utf16le
|
||||||
|
xUserDefined
|
||||||
|
numEncodings
|
||||||
|
)
|
||||||
|
|
||||||
|
var canonical = [numEncodings]string{
|
||||||
|
"utf-8",
|
||||||
|
"ibm866",
|
||||||
|
"iso-8859-2",
|
||||||
|
"iso-8859-3",
|
||||||
|
"iso-8859-4",
|
||||||
|
"iso-8859-5",
|
||||||
|
"iso-8859-6",
|
||||||
|
"iso-8859-7",
|
||||||
|
"iso-8859-8",
|
||||||
|
"iso-8859-8-i",
|
||||||
|
"iso-8859-10",
|
||||||
|
"iso-8859-13",
|
||||||
|
"iso-8859-14",
|
||||||
|
"iso-8859-15",
|
||||||
|
"iso-8859-16",
|
||||||
|
"koi8-r",
|
||||||
|
"koi8-u",
|
||||||
|
"macintosh",
|
||||||
|
"windows-874",
|
||||||
|
"windows-1250",
|
||||||
|
"windows-1251",
|
||||||
|
"windows-1252",
|
||||||
|
"windows-1253",
|
||||||
|
"windows-1254",
|
||||||
|
"windows-1255",
|
||||||
|
"windows-1256",
|
||||||
|
"windows-1257",
|
||||||
|
"windows-1258",
|
||||||
|
"x-mac-cyrillic",
|
||||||
|
"gbk",
|
||||||
|
"gb18030",
|
||||||
|
"big5",
|
||||||
|
"euc-jp",
|
||||||
|
"iso-2022-jp",
|
||||||
|
"shift_jis",
|
||||||
|
"euc-kr",
|
||||||
|
"replacement",
|
||||||
|
"utf-16be",
|
||||||
|
"utf-16le",
|
||||||
|
"x-user-defined",
|
||||||
|
}
|
||||||
|
|
||||||
|
var nameMap = map[string]htmlEncoding{
|
||||||
|
"unicode-1-1-utf-8": utf8,
|
||||||
|
"utf-8": utf8,
|
||||||
|
"utf8": utf8,
|
||||||
|
"866": ibm866,
|
||||||
|
"cp866": ibm866,
|
||||||
|
"csibm866": ibm866,
|
||||||
|
"ibm866": ibm866,
|
||||||
|
"csisolatin2": iso8859_2,
|
||||||
|
"iso-8859-2": iso8859_2,
|
||||||
|
"iso-ir-101": iso8859_2,
|
||||||
|
"iso8859-2": iso8859_2,
|
||||||
|
"iso88592": iso8859_2,
|
||||||
|
"iso_8859-2": iso8859_2,
|
||||||
|
"iso_8859-2:1987": iso8859_2,
|
||||||
|
"l2": iso8859_2,
|
||||||
|
"latin2": iso8859_2,
|
||||||
|
"csisolatin3": iso8859_3,
|
||||||
|
"iso-8859-3": iso8859_3,
|
||||||
|
"iso-ir-109": iso8859_3,
|
||||||
|
"iso8859-3": iso8859_3,
|
||||||
|
"iso88593": iso8859_3,
|
||||||
|
"iso_8859-3": iso8859_3,
|
||||||
|
"iso_8859-3:1988": iso8859_3,
|
||||||
|
"l3": iso8859_3,
|
||||||
|
"latin3": iso8859_3,
|
||||||
|
"csisolatin4": iso8859_4,
|
||||||
|
"iso-8859-4": iso8859_4,
|
||||||
|
"iso-ir-110": iso8859_4,
|
||||||
|
"iso8859-4": iso8859_4,
|
||||||
|
"iso88594": iso8859_4,
|
||||||
|
"iso_8859-4": iso8859_4,
|
||||||
|
"iso_8859-4:1988": iso8859_4,
|
||||||
|
"l4": iso8859_4,
|
||||||
|
"latin4": iso8859_4,
|
||||||
|
"csisolatincyrillic": iso8859_5,
|
||||||
|
"cyrillic": iso8859_5,
|
||||||
|
"iso-8859-5": iso8859_5,
|
||||||
|
"iso-ir-144": iso8859_5,
|
||||||
|
"iso8859-5": iso8859_5,
|
||||||
|
"iso88595": iso8859_5,
|
||||||
|
"iso_8859-5": iso8859_5,
|
||||||
|
"iso_8859-5:1988": iso8859_5,
|
||||||
|
"arabic": iso8859_6,
|
||||||
|
"asmo-708": iso8859_6,
|
||||||
|
"csiso88596e": iso8859_6,
|
||||||
|
"csiso88596i": iso8859_6,
|
||||||
|
"csisolatinarabic": iso8859_6,
|
||||||
|
"ecma-114": iso8859_6,
|
||||||
|
"iso-8859-6": iso8859_6,
|
||||||
|
"iso-8859-6-e": iso8859_6,
|
||||||
|
"iso-8859-6-i": iso8859_6,
|
||||||
|
"iso-ir-127": iso8859_6,
|
||||||
|
"iso8859-6": iso8859_6,
|
||||||
|
"iso88596": iso8859_6,
|
||||||
|
"iso_8859-6": iso8859_6,
|
||||||
|
"iso_8859-6:1987": iso8859_6,
|
||||||
|
"csisolatingreek": iso8859_7,
|
||||||
|
"ecma-118": iso8859_7,
|
||||||
|
"elot_928": iso8859_7,
|
||||||
|
"greek": iso8859_7,
|
||||||
|
"greek8": iso8859_7,
|
||||||
|
"iso-8859-7": iso8859_7,
|
||||||
|
"iso-ir-126": iso8859_7,
|
||||||
|
"iso8859-7": iso8859_7,
|
||||||
|
"iso88597": iso8859_7,
|
||||||
|
"iso_8859-7": iso8859_7,
|
||||||
|
"iso_8859-7:1987": iso8859_7,
|
||||||
|
"sun_eu_greek": iso8859_7,
|
||||||
|
"csiso88598e": iso8859_8,
|
||||||
|
"csisolatinhebrew": iso8859_8,
|
||||||
|
"hebrew": iso8859_8,
|
||||||
|
"iso-8859-8": iso8859_8,
|
||||||
|
"iso-8859-8-e": iso8859_8,
|
||||||
|
"iso-ir-138": iso8859_8,
|
||||||
|
"iso8859-8": iso8859_8,
|
||||||
|
"iso88598": iso8859_8,
|
||||||
|
"iso_8859-8": iso8859_8,
|
||||||
|
"iso_8859-8:1988": iso8859_8,
|
||||||
|
"visual": iso8859_8,
|
||||||
|
"csiso88598i": iso8859_8I,
|
||||||
|
"iso-8859-8-i": iso8859_8I,
|
||||||
|
"logical": iso8859_8I,
|
||||||
|
"csisolatin6": iso8859_10,
|
||||||
|
"iso-8859-10": iso8859_10,
|
||||||
|
"iso-ir-157": iso8859_10,
|
||||||
|
"iso8859-10": iso8859_10,
|
||||||
|
"iso885910": iso8859_10,
|
||||||
|
"l6": iso8859_10,
|
||||||
|
"latin6": iso8859_10,
|
||||||
|
"iso-8859-13": iso8859_13,
|
||||||
|
"iso8859-13": iso8859_13,
|
||||||
|
"iso885913": iso8859_13,
|
||||||
|
"iso-8859-14": iso8859_14,
|
||||||
|
"iso8859-14": iso8859_14,
|
||||||
|
"iso885914": iso8859_14,
|
||||||
|
"csisolatin9": iso8859_15,
|
||||||
|
"iso-8859-15": iso8859_15,
|
||||||
|
"iso8859-15": iso8859_15,
|
||||||
|
"iso885915": iso8859_15,
|
||||||
|
"iso_8859-15": iso8859_15,
|
||||||
|
"l9": iso8859_15,
|
||||||
|
"iso-8859-16": iso8859_16,
|
||||||
|
"cskoi8r": koi8r,
|
||||||
|
"koi": koi8r,
|
||||||
|
"koi8": koi8r,
|
||||||
|
"koi8-r": koi8r,
|
||||||
|
"koi8_r": koi8r,
|
||||||
|
"koi8-ru": koi8u,
|
||||||
|
"koi8-u": koi8u,
|
||||||
|
"csmacintosh": macintosh,
|
||||||
|
"mac": macintosh,
|
||||||
|
"macintosh": macintosh,
|
||||||
|
"x-mac-roman": macintosh,
|
||||||
|
"dos-874": windows874,
|
||||||
|
"iso-8859-11": windows874,
|
||||||
|
"iso8859-11": windows874,
|
||||||
|
"iso885911": windows874,
|
||||||
|
"tis-620": windows874,
|
||||||
|
"windows-874": windows874,
|
||||||
|
"cp1250": windows1250,
|
||||||
|
"windows-1250": windows1250,
|
||||||
|
"x-cp1250": windows1250,
|
||||||
|
"cp1251": windows1251,
|
||||||
|
"windows-1251": windows1251,
|
||||||
|
"x-cp1251": windows1251,
|
||||||
|
"ansi_x3.4-1968": windows1252,
|
||||||
|
"ascii": windows1252,
|
||||||
|
"cp1252": windows1252,
|
||||||
|
"cp819": windows1252,
|
||||||
|
"csisolatin1": windows1252,
|
||||||
|
"ibm819": windows1252,
|
||||||
|
"iso-8859-1": windows1252,
|
||||||
|
"iso-ir-100": windows1252,
|
||||||
|
"iso8859-1": windows1252,
|
||||||
|
"iso88591": windows1252,
|
||||||
|
"iso_8859-1": windows1252,
|
||||||
|
"iso_8859-1:1987": windows1252,
|
||||||
|
"l1": windows1252,
|
||||||
|
"latin1": windows1252,
|
||||||
|
"us-ascii": windows1252,
|
||||||
|
"windows-1252": windows1252,
|
||||||
|
"x-cp1252": windows1252,
|
||||||
|
"cp1253": windows1253,
|
||||||
|
"windows-1253": windows1253,
|
||||||
|
"x-cp1253": windows1253,
|
||||||
|
"cp1254": windows1254,
|
||||||
|
"csisolatin5": windows1254,
|
||||||
|
"iso-8859-9": windows1254,
|
||||||
|
"iso-ir-148": windows1254,
|
||||||
|
"iso8859-9": windows1254,
|
||||||
|
"iso88599": windows1254,
|
||||||
|
"iso_8859-9": windows1254,
|
||||||
|
"iso_8859-9:1989": windows1254,
|
||||||
|
"l5": windows1254,
|
||||||
|
"latin5": windows1254,
|
||||||
|
"windows-1254": windows1254,
|
||||||
|
"x-cp1254": windows1254,
|
||||||
|
"cp1255": windows1255,
|
||||||
|
"windows-1255": windows1255,
|
||||||
|
"x-cp1255": windows1255,
|
||||||
|
"cp1256": windows1256,
|
||||||
|
"windows-1256": windows1256,
|
||||||
|
"x-cp1256": windows1256,
|
||||||
|
"cp1257": windows1257,
|
||||||
|
"windows-1257": windows1257,
|
||||||
|
"x-cp1257": windows1257,
|
||||||
|
"cp1258": windows1258,
|
||||||
|
"windows-1258": windows1258,
|
||||||
|
"x-cp1258": windows1258,
|
||||||
|
"x-mac-cyrillic": macintoshCyrillic,
|
||||||
|
"x-mac-ukrainian": macintoshCyrillic,
|
||||||
|
"chinese": gbk,
|
||||||
|
"csgb2312": gbk,
|
||||||
|
"csiso58gb231280": gbk,
|
||||||
|
"gb2312": gbk,
|
||||||
|
"gb_2312": gbk,
|
||||||
|
"gb_2312-80": gbk,
|
||||||
|
"gbk": gbk,
|
||||||
|
"iso-ir-58": gbk,
|
||||||
|
"x-gbk": gbk,
|
||||||
|
"gb18030": gb18030,
|
||||||
|
"big5": big5,
|
||||||
|
"big5-hkscs": big5,
|
||||||
|
"cn-big5": big5,
|
||||||
|
"csbig5": big5,
|
||||||
|
"x-x-big5": big5,
|
||||||
|
"cseucpkdfmtjapanese": eucjp,
|
||||||
|
"euc-jp": eucjp,
|
||||||
|
"x-euc-jp": eucjp,
|
||||||
|
"csiso2022jp": iso2022jp,
|
||||||
|
"iso-2022-jp": iso2022jp,
|
||||||
|
"csshiftjis": shiftJIS,
|
||||||
|
"ms932": shiftJIS,
|
||||||
|
"ms_kanji": shiftJIS,
|
||||||
|
"shift-jis": shiftJIS,
|
||||||
|
"shift_jis": shiftJIS,
|
||||||
|
"sjis": shiftJIS,
|
||||||
|
"windows-31j": shiftJIS,
|
||||||
|
"x-sjis": shiftJIS,
|
||||||
|
"cseuckr": euckr,
|
||||||
|
"csksc56011987": euckr,
|
||||||
|
"euc-kr": euckr,
|
||||||
|
"iso-ir-149": euckr,
|
||||||
|
"korean": euckr,
|
||||||
|
"ks_c_5601-1987": euckr,
|
||||||
|
"ks_c_5601-1989": euckr,
|
||||||
|
"ksc5601": euckr,
|
||||||
|
"ksc_5601": euckr,
|
||||||
|
"windows-949": euckr,
|
||||||
|
"csiso2022kr": replacement,
|
||||||
|
"hz-gb-2312": replacement,
|
||||||
|
"iso-2022-cn": replacement,
|
||||||
|
"iso-2022-cn-ext": replacement,
|
||||||
|
"iso-2022-kr": replacement,
|
||||||
|
"replacement": replacement,
|
||||||
|
"utf-16be": utf16be,
|
||||||
|
"utf-16": utf16le,
|
||||||
|
"utf-16le": utf16le,
|
||||||
|
"x-user-defined": xUserDefined,
|
||||||
|
}
|
||||||
|
|
||||||
|
var localeMap = []htmlEncoding{
|
||||||
|
windows1252, // und_Latn
|
||||||
|
windows1256, // ar
|
||||||
|
windows1251, // ba
|
||||||
|
windows1251, // be
|
||||||
|
windows1251, // bg
|
||||||
|
windows1250, // cs
|
||||||
|
iso8859_7, // el
|
||||||
|
windows1257, // et
|
||||||
|
windows1256, // fa
|
||||||
|
windows1255, // he
|
||||||
|
windows1250, // hr
|
||||||
|
iso8859_2, // hu
|
||||||
|
shiftJIS, // ja
|
||||||
|
windows1251, // kk
|
||||||
|
euckr, // ko
|
||||||
|
windows1254, // ku
|
||||||
|
windows1251, // ky
|
||||||
|
windows1257, // lt
|
||||||
|
windows1257, // lv
|
||||||
|
windows1251, // mk
|
||||||
|
iso8859_2, // pl
|
||||||
|
windows1251, // ru
|
||||||
|
windows1251, // sah
|
||||||
|
windows1250, // sk
|
||||||
|
iso8859_2, // sl
|
||||||
|
windows1251, // sr
|
||||||
|
windows1251, // tg
|
||||||
|
windows874, // th
|
||||||
|
windows1254, // tr
|
||||||
|
windows1251, // tt
|
||||||
|
windows1251, // uk
|
||||||
|
windows1258, // vi
|
||||||
|
gb18030, // zh-hans
|
||||||
|
big5, // zh-hant
|
||||||
|
}
|
||||||
|
|
||||||
|
const locales = "und_Latn ar ba be bg cs el et fa he hr hu ja kk ko ku ky lt lv mk pl ru sah sk sl sr tg th tr tt uk vi zh-hans zh-hant"
|
142
vendor/golang.org/x/text/encoding/internal/identifier/gen.go
generated
vendored
Normal file
142
vendor/golang.org/x/text/encoding/internal/identifier/gen.go
generated
vendored
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// +build ignore
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/text/internal/gen"
|
||||||
|
)
|
||||||
|
|
||||||
|
type registry struct {
|
||||||
|
XMLName xml.Name `xml:"registry"`
|
||||||
|
Updated string `xml:"updated"`
|
||||||
|
Registry []struct {
|
||||||
|
ID string `xml:"id,attr"`
|
||||||
|
Record []struct {
|
||||||
|
Name string `xml:"name"`
|
||||||
|
Xref []struct {
|
||||||
|
Type string `xml:"type,attr"`
|
||||||
|
Data string `xml:"data,attr"`
|
||||||
|
} `xml:"xref"`
|
||||||
|
Desc struct {
|
||||||
|
Data string `xml:",innerxml"`
|
||||||
|
// Any []struct {
|
||||||
|
// Data string `xml:",chardata"`
|
||||||
|
// } `xml:",any"`
|
||||||
|
// Data string `xml:",chardata"`
|
||||||
|
} `xml:"description,"`
|
||||||
|
MIB string `xml:"value"`
|
||||||
|
Alias []string `xml:"alias"`
|
||||||
|
MIME string `xml:"preferred_alias"`
|
||||||
|
} `xml:"record"`
|
||||||
|
} `xml:"registry"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gen.OpenIANAFile("assignments/character-sets/character-sets.xml")
|
||||||
|
reg := ®istry{}
|
||||||
|
if err := xml.NewDecoder(r).Decode(®); err != nil && err != io.EOF {
|
||||||
|
log.Fatalf("Error decoding charset registry: %v", err)
|
||||||
|
}
|
||||||
|
if len(reg.Registry) == 0 || reg.Registry[0].ID != "character-sets-1" {
|
||||||
|
log.Fatalf("Unexpected ID %s", reg.Registry[0].ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := &bytes.Buffer{}
|
||||||
|
fmt.Fprintf(w, "const (\n")
|
||||||
|
for _, rec := range reg.Registry[0].Record {
|
||||||
|
constName := ""
|
||||||
|
for _, a := range rec.Alias {
|
||||||
|
if strings.HasPrefix(a, "cs") && strings.IndexByte(a, '-') == -1 {
|
||||||
|
// Some of the constant definitions have comments in them. Strip those.
|
||||||
|
constName = strings.Title(strings.SplitN(a[2:], "\n", 2)[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if constName == "" {
|
||||||
|
switch rec.MIB {
|
||||||
|
case "2085":
|
||||||
|
constName = "HZGB2312" // Not listed as alias for some reason.
|
||||||
|
default:
|
||||||
|
log.Fatalf("No cs alias defined for %s.", rec.MIB)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if rec.MIME != "" {
|
||||||
|
rec.MIME = fmt.Sprintf(" (MIME: %s)", rec.MIME)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "// %s is the MIB identifier with IANA name %s%s.\n//\n", constName, rec.Name, rec.MIME)
|
||||||
|
if len(rec.Desc.Data) > 0 {
|
||||||
|
fmt.Fprint(w, "// ")
|
||||||
|
d := xml.NewDecoder(strings.NewReader(rec.Desc.Data))
|
||||||
|
inElem := true
|
||||||
|
attr := ""
|
||||||
|
for {
|
||||||
|
t, err := d.Token()
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
switch x := t.(type) {
|
||||||
|
case xml.CharData:
|
||||||
|
attr = "" // Don't need attribute info.
|
||||||
|
a := bytes.Split([]byte(x), []byte("\n"))
|
||||||
|
for i, b := range a {
|
||||||
|
if b = bytes.TrimSpace(b); len(b) != 0 {
|
||||||
|
if !inElem && i > 0 {
|
||||||
|
fmt.Fprint(w, "\n// ")
|
||||||
|
}
|
||||||
|
inElem = false
|
||||||
|
fmt.Fprintf(w, "%s ", string(b))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case xml.StartElement:
|
||||||
|
if x.Name.Local == "xref" {
|
||||||
|
inElem = true
|
||||||
|
use := false
|
||||||
|
for _, a := range x.Attr {
|
||||||
|
if a.Name.Local == "type" {
|
||||||
|
use = use || a.Value != "person"
|
||||||
|
}
|
||||||
|
if a.Name.Local == "data" && use {
|
||||||
|
// Patch up URLs to use https. From some links, the
|
||||||
|
// https version is different from the http one.
|
||||||
|
s := a.Value
|
||||||
|
s = strings.Replace(s, "http://", "https://", -1)
|
||||||
|
s = strings.Replace(s, "/unicode/", "/", -1)
|
||||||
|
attr = s + " "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case xml.EndElement:
|
||||||
|
inElem = false
|
||||||
|
fmt.Fprint(w, attr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, "\n")
|
||||||
|
}
|
||||||
|
for _, x := range rec.Xref {
|
||||||
|
switch x.Type {
|
||||||
|
case "rfc":
|
||||||
|
fmt.Fprintf(w, "// Reference: %s\n", strings.ToUpper(x.Data))
|
||||||
|
case "uri":
|
||||||
|
fmt.Fprintf(w, "// Reference: %s\n", x.Data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%s MIB = %s\n", constName, rec.MIB)
|
||||||
|
fmt.Fprintln(w)
|
||||||
|
}
|
||||||
|
fmt.Fprintln(w, ")")
|
||||||
|
|
||||||
|
gen.WriteGoFile("mib.go", "identifier", w.Bytes())
|
||||||
|
}
|
81
vendor/golang.org/x/text/encoding/internal/identifier/identifier.go
generated
vendored
Normal file
81
vendor/golang.org/x/text/encoding/internal/identifier/identifier.go
generated
vendored
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
//go:generate go run gen.go
|
||||||
|
|
||||||
|
// Package identifier defines the contract between implementations of Encoding
|
||||||
|
// and Index by defining identifiers that uniquely identify standardized coded
|
||||||
|
// character sets (CCS) and character encoding schemes (CES), which we will
|
||||||
|
// together refer to as encodings, for which Encoding implementations provide
|
||||||
|
// converters to and from UTF-8. This package is typically only of concern to
|
||||||
|
// implementers of Indexes and Encodings.
|
||||||
|
//
|
||||||
|
// One part of the identifier is the MIB code, which is defined by IANA and
|
||||||
|
// uniquely identifies a CCS or CES. Each code is associated with data that
|
||||||
|
// references authorities, official documentation as well as aliases and MIME
|
||||||
|
// names.
|
||||||
|
//
|
||||||
|
// Not all CESs are covered by the IANA registry. The "other" string that is
|
||||||
|
// returned by ID can be used to identify other character sets or versions of
|
||||||
|
// existing ones.
|
||||||
|
//
|
||||||
|
// It is recommended that each package that provides a set of Encodings provide
|
||||||
|
// the All and Common variables to reference all supported encodings and
|
||||||
|
// commonly used subset. This allows Index implementations to include all
|
||||||
|
// available encodings without explicitly referencing or knowing about them.
|
||||||
|
package identifier
|
||||||
|
|
||||||
|
// Note: this package is internal, but could be made public if there is a need
|
||||||
|
// for writing third-party Indexes and Encodings.
|
||||||
|
|
||||||
|
// References:
|
||||||
|
// - http://source.icu-project.org/repos/icu/icu/trunk/source/data/mappings/convrtrs.txt
|
||||||
|
// - http://www.iana.org/assignments/character-sets/character-sets.xhtml
|
||||||
|
// - http://www.iana.org/assignments/ianacharset-mib/ianacharset-mib
|
||||||
|
// - http://www.ietf.org/rfc/rfc2978.txt
|
||||||
|
// - https://www.unicode.org/reports/tr22/
|
||||||
|
// - http://www.w3.org/TR/encoding/
|
||||||
|
// - https://encoding.spec.whatwg.org/
|
||||||
|
// - https://encoding.spec.whatwg.org/encodings.json
|
||||||
|
// - https://tools.ietf.org/html/rfc6657#section-5
|
||||||
|
|
||||||
|
// Interface can be implemented by Encodings to define the CCS or CES for which
|
||||||
|
// it implements conversions.
|
||||||
|
type Interface interface {
|
||||||
|
// ID returns an encoding identifier. Exactly one of the mib and other
|
||||||
|
// values should be non-zero.
|
||||||
|
//
|
||||||
|
// In the usual case it is only necessary to indicate the MIB code. The
|
||||||
|
// other string can be used to specify encodings for which there is no MIB,
|
||||||
|
// such as "x-mac-dingbat".
|
||||||
|
//
|
||||||
|
// The other string may only contain the characters a-z, A-Z, 0-9, - and _.
|
||||||
|
ID() (mib MIB, other string)
|
||||||
|
|
||||||
|
// NOTE: the restrictions on the encoding are to allow extending the syntax
|
||||||
|
// with additional information such as versions, vendors and other variants.
|
||||||
|
}
|
||||||
|
|
||||||
|
// A MIB identifies an encoding. It is derived from the IANA MIB codes and adds
|
||||||
|
// some identifiers for some encodings that are not covered by the IANA
|
||||||
|
// standard.
|
||||||
|
//
|
||||||
|
// See http://www.iana.org/assignments/ianacharset-mib.
|
||||||
|
type MIB uint16
|
||||||
|
|
||||||
|
// These additional MIB types are not defined in IANA. They are added because
|
||||||
|
// they are common and defined within the text repo.
|
||||||
|
const (
|
||||||
|
// Unofficial marks the start of encodings not registered by IANA.
|
||||||
|
Unofficial MIB = 10000 + iota
|
||||||
|
|
||||||
|
// Replacement is the WhatWG replacement encoding.
|
||||||
|
Replacement
|
||||||
|
|
||||||
|
// XUserDefined is the code for x-user-defined.
|
||||||
|
XUserDefined
|
||||||
|
|
||||||
|
// MacintoshCyrillic is the code for x-mac-cyrillic.
|
||||||
|
MacintoshCyrillic
|
||||||
|
)
|
1619
vendor/golang.org/x/text/encoding/internal/identifier/mib.go
generated
vendored
Normal file
1619
vendor/golang.org/x/text/encoding/internal/identifier/mib.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
75
vendor/golang.org/x/text/encoding/internal/internal.go
generated
vendored
Normal file
75
vendor/golang.org/x/text/encoding/internal/internal.go
generated
vendored
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package internal contains code that is shared among encoding implementations.
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/text/encoding"
|
||||||
|
"golang.org/x/text/encoding/internal/identifier"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Encoding is an implementation of the Encoding interface that adds the String
|
||||||
|
// and ID methods to an existing encoding.
|
||||||
|
type Encoding struct {
|
||||||
|
encoding.Encoding
|
||||||
|
Name string
|
||||||
|
MIB identifier.MIB
|
||||||
|
}
|
||||||
|
|
||||||
|
// _ verifies that Encoding implements identifier.Interface.
|
||||||
|
var _ identifier.Interface = (*Encoding)(nil)
|
||||||
|
|
||||||
|
func (e *Encoding) String() string {
|
||||||
|
return e.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Encoding) ID() (mib identifier.MIB, other string) {
|
||||||
|
return e.MIB, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// SimpleEncoding is an Encoding that combines two Transformers.
|
||||||
|
type SimpleEncoding struct {
|
||||||
|
Decoder transform.Transformer
|
||||||
|
Encoder transform.Transformer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *SimpleEncoding) NewDecoder() *encoding.Decoder {
|
||||||
|
return &encoding.Decoder{Transformer: e.Decoder}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *SimpleEncoding) NewEncoder() *encoding.Encoder {
|
||||||
|
return &encoding.Encoder{Transformer: e.Encoder}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FuncEncoding is an Encoding that combines two functions returning a new
|
||||||
|
// Transformer.
|
||||||
|
type FuncEncoding struct {
|
||||||
|
Decoder func() transform.Transformer
|
||||||
|
Encoder func() transform.Transformer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e FuncEncoding) NewDecoder() *encoding.Decoder {
|
||||||
|
return &encoding.Decoder{Transformer: e.Decoder()}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e FuncEncoding) NewEncoder() *encoding.Encoder {
|
||||||
|
return &encoding.Encoder{Transformer: e.Encoder()}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A RepertoireError indicates a rune is not in the repertoire of a destination
|
||||||
|
// encoding. It is associated with an encoding-specific suggested replacement
|
||||||
|
// byte.
|
||||||
|
type RepertoireError byte
|
||||||
|
|
||||||
|
// Error implements the error interrface.
|
||||||
|
func (r RepertoireError) Error() string {
|
||||||
|
return "encoding: rune not supported by encoding."
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replacement returns the replacement string associated with this error.
|
||||||
|
func (r RepertoireError) Replacement() byte { return byte(r) }
|
||||||
|
|
||||||
|
var ErrASCIIReplacement = RepertoireError(encoding.ASCIISub)
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user