How can I use goquery parse single element - go

<speak><voice name=\"en-US-JennyNeural\"><prosody rate=\"1\">aaaaaaaa<break time=\"5s\"/> bbbb. <br time=\"2s\"/>ccccccdddddddd </prosody></voice></speak>
I use this code to parse and get:
doc, err := goquery.NewDocumentFromReader(strings.NewReader(text))
if err != nil {
return "", err
}
ssml, err := doc.Find("html body").Html()
if err != nil {
return "", err
}
Result:
<speak><voice name="en-US-JennyNeural"><prosody rate="1">aaaaaaaa<break time="5s"> bbbb. <br time="2s"/>ccccccdddddddd </break></prosody></voice></speak>
I think the break doesn't parse Correctly. I want to parse <break/> like <br/>.

Assuming you're using github.com/PuerkitoBio/goquery, it uses golang.org/x/net/html under the hood for HTML parsing, which is an HTML5-compliant tokenizer and parser.
<br> and <break> are parsed differently because in HTML <br> is a tag that must not have a closing tag, but <break> is not such a tag.
If you want goquery to handle your HTML properly, you must use an explicit closing tag for <break> instead of the self-closing tag.
E.g. instead of this:
<break time="5s"/>
You must use this:
<break time="5s"></break>
With this change your output will be (try it on the Go Playground):
<speak><voice name="en-US-JennyNeural"><prosody rate="1">aaaaaaaa<break time="5s"></break> bbbb. <br time="2s"/>ccccccdddddddd </prosody></voice></speak>

d := xml.NewDecoder(strings.NewReader(text))
var writer bytes.Buffer
e := xml.NewEncoder(&writer)
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return "", err
}
if t == nil {
break
}
switch se := t.(type) {
case xml.StartElement:
e.EncodeToken(se)
case xml.EndElement, xml.CharData, xml.Comment, xml.ProcInst, xml.Directive:
e.EncodeToken(se)
}
}
e.Flush()
return writer.String(), nil

Related

Unmarshal to struct with slice returns null value instead of empty slice

If I create a "photo" without any tags it is stored in dynamodb as
"tags": {
"NULL": true
},
But when I query and unmarshal the record I would expect that it converts this into an empty slice instead I get this:
[{"photo_id":"bmpuh3jg","tags":null}]
Is it possible to have it convert it into an empty slice instead? e.g.
[{"photo_id":"bmpuh3jg","tags":[]}]
CODE EXAMPLE
My struct
type Photo struct {
Id string `json:"photo_id"`
Tags []string `json:"tags"`
}
Query
photo := &Photo{}
input := &dynamodb.QueryInput{
TableName: aws.String("local.photos"),
KeyConditionExpression: aws.String("photo_id = :photo_id"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":photo_id": {
S: aws.String(photo_id),
},
},
}
db_result, err := db.Query(input)
if err != nil {
return nil, err
} else if *db_result.Count == int64(0) {
// No item found
return nil, err
}
err = dynamodbattribute.UnmarshalListOfMaps(db_result.Items, photo)
if err != nil {
return nil, err
}
photoJSON, err := json.Marshal(photo)
if err != nil {
return nil, err
}
return photoJSON, nil
If I understand your question correctly, to achieve a result with an empty slice for Tags ({"photo_id":"bmpuh3jg","tags":[]}), you can do it like this:
jsonString := `{"photo_id":"bmpuh3jg","tags":null}`
photo := &Photo{}
err := json.Unmarshal([]byte(jsonString), &photo)
if err != nil {
fmt.Println(err.Error())
}
// Here is a trick. Replace nil with an empty slice.
if photo.Tags == nil {
photo.Tags = []string{}
}
elemJSON, err := json.Marshal(photo)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(elemJSON)) //{"photo_id":"bmpuh3jg","tags":[]}
To understand, why a nil slice encodes as the null JSON, you can check official documentation https://golang.org/pkg/encoding/json/
Array and slice values encode as JSON arrays, except that []byte
encodes as a base64-encoded string, and a nil slice encodes as the
null JSON value.
Check on Go Playground: https://play.golang.org/p/BsxTpBlypV5

Trouble getting content type of file in Go

I have a function in which I take in a base64 string and get the content of it (PDF or JPEG).
I read in the base64 content, convert it to bytes and decode it into the file that it is.
I then create a file where I will output the decoded file (JPEG or PDF).
Then I write the bytes to it.
Then I call my GetFileContentType on it and it returns to me an empty string.
If I run the functions separately, as in I first the first function to create the decoded file, and end it. And then call the second function to get the content type, it works and returns it as JPEG or PDF.
What am I doing wrong here?
And is there a better way to do this?
func ConvertToJPEGBase64(
src string,
dst string,
) error {
b, err := ioutil.ReadFile(src)
if err != nil {
return err
}
str := string(b)
byteArray, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return err
}
f, err := os.Create(dst)
if err != nil {
return err
}
if _, err := f.Write(byteArray); err != nil {
return err
}
f.Sync()
filetype, err := client.GetFileContentType(f)
if err != nil {
return err
}
if strings.Contains(filetype, "jpeg") {
// do something
} else {
// do something else
}
return nil
}
// GetFileContentType tells us the type of file
func GetFileContentType(out *os.File) (string, error) {
// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)
_, err := out.Read(buffer)
if err != nil {
return "", err
}
contentType := http.DetectContentType(buffer)
return contentType, nil
}
The problem is that GetFileContentType reads from the end of the file. Fix this be seeking back to the beginning of the file before calling calling GetFileContentType:
if _, err := f.Seek(io.SeekStart, 0); err != nil {
return err
}
A better fix is to use the file data that's already in memory. This simplifies the code to the point where there's no need for the GetFileContentType function.
func ConvertToJPEGBase64(
src string,
dst string,
) error {
b, err := ioutil.ReadFile(src)
if err != nil {
return err
}
str := string(b)
byteArray, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return err
}
f, err := os.Create(dst)
if err != nil {
return err
}
defer f.Close() // <-- Close the file on return.
if _, err := f.Write(byteArray); err != nil {
return err
}
fileType := http.DetectContentType(byteArray) // <-- use data in memory
if strings.Contains(fileType, "jpeg") {
// do something
} else {
// do something else
}
return nil
}
More code can be eliminated by using ioutil.WriteFile:
func ConvertToJPEGBase64(src, dst string) error {
b, err := ioutil.ReadFile(src)
if err != nil {
return err
}
byteArray, err := base64.StdEncoding.DecodeString(string(b))
if err != nil {
return err
}
if err := ioutil.WriteFile(dst, byteArray, 0666); err != nil {
return err
}
fileType := http.DetectContentType(byteArray)
if strings.Contains(fileType, "jpeg") {
// do something
} else {
// do something else
}
return nil
}

Close/return ResponseWriter from child function in Go

I am new to Go and building web apps. An example of my handler is something like this:
func getAllPostsHandler(w http.ResponseWriter, r *http.Request) {
var posts []Post
dbSesstion := context.Get(r, "database").(*mgo.Session)
err := dbSesstion.DB(dbsett.Name).C(dbsett.Collection).Find(nil).All(&posts)
if err != nil {
log.Print("error: ", nil)
w.WriteHeader(http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(posts)
if err != nil {
log.Print("error: ", nil)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
My handlers have a lot of repeating error checking like this:
if err != nil {
log.Print("error: ", nil)
w.WriteHeader(http.StatusInternalServerError)
return
}
I want to make a function, which checks for error, print logs, write the response writer if neccessary, and return, but not only to the handler, but to stop all other response writings and return the handler itself. Is it possible to do so? I am thinking about panicing, but something tells me that its not the right way.
You can't escape outermost function from innermost but you can at least compact code a bit by inverting control flow
err := dbSesstion.DB(dbsett.Name).C(dbsett.Collection).Find(nil).All(&posts)
if err == nil {
err = json.NewEncoder(w).Encode(posts)
if err == nil {
err = somethingelse()
if err == nil {
w.WriteHeader(http.StatusOK)
return //successfully
}
}
}
log.Print("error: ", nil)
w.WriteHeader(http.StatusInternalServerError)

I'm using go language to print to a POS printer, but got bad prints of Chinese charater. (English is OK)

I'm using this go package, and everything is good. But got bad prints of Chinese characters. I'm not good at this kind of low-level / hardware / encoding things. So someone give a hint how can I correct my code.
Updated code:
func main() {
testWindowPrinter()
}
func testWindowPrinter() {
// all have bad prints
printLines(simplifiedchinese.GBK.NewDecoder().Reader(strings.NewReader("你好")))
printLines(simplifiedchinese.HZGB2312.NewDecoder().Reader(strings.NewReader("你好")))
printLines(strings.NewReader("你好"))
}
func printLines(lines ...io.Reader) error {
var defaultPrinter, err = printer.Default()
if err != nil {
log.Println(1, err.Error())
return err
}
p, err := printer.Open(defaultPrinter)
if err != nil {
log.Println(2, err.Error())
return err
}
defer p.Close()
err = p.StartRawDocument("test")
if err != nil {
log.Println(3, err.Error())
return err
}
defer p.EndDocument()
err = p.StartPage()
if err != nil {
log.Println(4, err.Error())
return err
}
// Am I doing right here?
for i := range lines {
var bf = new(bytes.Buffer)
n, err := bf.ReadFrom(lines[i])
if err != nil {
println(err.Error())
} else {
println(n)
fmt.Fprintln(p, bf.String())
}
}
err = p.EndPage()
if err != nil {
log.Println(5, err.Error())
return err
}
return nil
}
More details
zsbd
It depends on your environment.
If you are executing this Go program in a Windows CMD (which only supports UTF16 encoding), you will see strange characters.
See "How to properly output a string in a Windows console with go?" as an example.
You need to convert those characters before printing them out.

text/template issue Parse() vs. ParseFiles()

I'm trying to do some simple work with the text/template package. The sample given at the top of template is what I'm working with.
How do I write the 'parsed' file so template.ParseFiles() properly reads and executes it?
package main
import (
"text/template"
"os"
)
type Inventory struct {
Material string
Count uint
}
func main() {
sweaters := Inventory{"wool", 17}
tmpl, err := template.New("test").Parse("{{.Count}} items are made of {{.Material}}")
// tmpl, err := template.New("test").ParseFiles("file.txt")
if err != nil { panic(err) }
err = tmpl.Execute(os.Stdout, sweaters)
if err != nil { panic(err) }
}
/*
Contents of file.txt:
{{.Count}} items are made of {{.Material}}
Error thrown:
panic: template: test:1: "test" is an incomplete or empty template
goroutine 1 [running]:
main.main()
/tmp/templates/t.go:19 +0x21a
goroutine 2 [syscall]:
created by runtime.main
/var/tmp/portage/dev-lang/go-1.0.1/work/go/src/pkg/runtime/proc.c:221
*/
I have a copy of this code posted at the golang playground here
Edit #1:
I've been doing some research on this issue... since it's the Execute() method that actually throws the exception, and not the ParseFiles() part, I checked the method definition:
// Execute applies a parsed template to the specified data object,
// and writes the output to wr.
func (t *Template) Execute(wr io.Writer, data interface{}) (err error) {
defer errRecover(&err)
value := reflect.ValueOf(data)
state := &state{
tmpl: t,
wr: wr,
line: 1,
vars: []variable{{"$", value}},
}
if t.Tree == nil || t.Root == nil {
state.errorf("%q is an incomplete or empty template", t.name)
}
state.walk(value, t.Root)
return
}
So, on a hunch, I dumped the value of t.Tree for the inline 'non-file' style, tmpl is: &parse.Tree{Name:"test", Root:(*parse.ListNode)(0xf840030700), funcs:[]map[string]interface {}(nil), lex:(*parse.lexer)(nil), token:[2]parse.item{parse.item{typ:6, val:""}, parse.item{typ:9, val:"{{"}}, peekCount:1, vars:[]string(nil)} and
when ran with ParseFiles(), tmpl is: (*parse.Tree)(nil). I find it odd that one is a dereference, and one value is a pointer. This may help solve the riddle
sweaters := Inventory{"wool", 17}
tmpl, err := template.ParseFiles("file.txt")
if err != nil {
panic(err)
}
err = tmpl.ExecuteTemplate(os.Stdout, "file.txt", sweaters)
if err != nil {
panic(err)
}
If you have many files, you can use ParseGlob:
tmpl, err := template.ParseGlob("*.txt")
if err != nil {
panic(err)
}
err = tmpl.ExecuteTemplate(os.Stdout, "file.txt", sweaters)
if err != nil {
panic(err)
}
err = tmpl.ExecuteTemplate(os.Stdout, "file2.txt", sweaters)
if err != nil {
panic(err)
}
There is a little trick in Go template parseFiles.
func parseFiles(t *Template, filenames ...string) (*Template, error) {
if len(filenames) == 0 {
// Not really a problem, but be consistent.
return nil, fmt.Errorf("template: no files named in call to ParseFiles")
}
for _, filename := range filenames {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
s := string(b)
name := filepath.Base(filename)
// First template becomes return value if not already defined,
// and we use that one for subsequent New calls to associate
// all the templates together. Also, if this file has the same name
// as t, this file becomes the contents of t, so
// t, err := New(name).Funcs(xxx).ParseFiles(name)
// works. Otherwise we create a new template associated with t.
var tmpl *Template
if t == nil {
t = New(name)
}
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
_, err = tmpl.Parse(s)
if err != nil {
return nil, err
}
}
return t, nil
}
Only the template with same name will be reuse, otherwise create new one.
as your sample:
tmpl, err := template.New("test").ParseFiles("file.txt")
tmpl is the template named "test", and associated another template named "file.txt", you call Execute on "test" template, this template is a empty template, so raise the error "test is an incomplete or empty template".
It worked when you change the template name to file.txt
tmpl, err := template.New("file.txt").ParseFiles("file.txt")

Resources