Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precompile go file, write function created wrong #35

Closed
miachel-lv opened this issue May 2, 2020 · 5 comments
Closed

Precompile go file, write function created wrong #35

miachel-lv opened this issue May 2, 2020 · 5 comments

Comments

@miachel-lv
Copy link

This is model file:
截屏2020-05-02下午7 40 00

This is my jade file:

截屏2020-05-02下午7 30 57

after do precompiled to go file:
截屏2020-05-02下午7 34 04

I don't understand why in function 'Simple' loop, StroyId UserId are call 'WriteAll' function write to buffer, and why not call 'WriteUint' function write to buffer.

The jade command I executed: ./jade index.jade

my project tree:
截屏2020-05-02下午7 44 07

Hope you can reply me, thanks!

@Joker
Copy link
Owner

Joker commented May 2, 2020

WriteAll() function is used for all non-primitive types
because at the stage of the jade file translation it's impossible to determine what type the line li= story.UserId contains.
The translator uses only jade files to get information about types.

@Qubitium
Copy link

Qubitium commented May 5, 2020

@Joker I am a co-worker of @miachel-lv

We are evaluating Jade/Golang for a large project and thus far we a perplexed at the randomless of how some templates are using optimized WriteXXX methods versus the default but very slow WriteAll.

So our question:

  1. When is Jade parse able to detect actual go struct types? We have templates that imports go struct types that is able to generate optimized WriteUint() but we don't see much consistency as when it works and when it doesn't.

  2. The GoAST files are the code that does the go import and field type detection. We suspect this is a bug somewhere that allows Jade parsing to detect types on some imported structs and fail on others, based on error logging.

  3. We need more in depth assistance in how Jade/Golang AST/import parse deal with native golang int/uint/string types.

  4. Without Native type WriteXXX support, Jade is at significant performance disadvantage compared to Hero which use expliciti type declaration/hinting in the template language itself. We don't see explicit type declaration possible with JadeLang or are we wrong here?

Thanks for all the help. We are willing to assist in helping make this pkg better since we really like the Jade template language.

@Qubitium
Copy link

Qubitium commented May 5, 2020

@Joker.

Look at the https://github.com/SlinSo/goTemplateBenchmark/blob/master/jade/simple.jade

:go:func Simple( u *model.User)

:go:import "github.com/SlinSo/goTemplateBenchmark/model"

html
    body
        h1= u.FirstName
        p Here's a list of your favorite colors:
        ul
            each colorName in u.FavoriteColors
                li= colorName

and generated https://github.com/SlinSo/goTemplateBenchmark/blob/master/jade/simple.jade.go

// Code generated by "jade.go"; DO NOT EDIT.

package jade

import (
	"github.com/SlinSo/goTemplateBenchmark/model"
	pool "github.com/valyala/bytebufferpool"
)

const (
	simple__0 = `<html><body><h1>`
	simple__1 = `</h1><p>Here's a list of your favorite colors:</p><ul>`
	simple__2 = `</ul></body></html>`
	simple__3 = `<li>`
	simple__4 = `</li>`
)

func Simple(u *model.User, buffer *pool.ByteBuffer) {

	buffer.WriteString(simple__0)
	WriteEscString(u.FirstName, buffer)
	buffer.WriteString(simple__1)

	for _, colorName := range u.FavoriteColors {
		buffer.WriteString(simple__3)
		WriteEscString(colorName, buffer)
		buffer.WriteString(simple__4)
	}
	buffer.WriteString(simple__2)

}

Model:

package model

type User struct {
	FirstName      string
	Email string
	FavoriteColors []string
	RawContent string
	EscapedContent string
}

type Navigation struct {
	Item string
	Link string
}

It is able to import a go struct with a simple String type field and Jade parser was able to detect the native String type and use WriteEscString() instead of WriteAll(str, true).

I don't understand how that example is different in the one provided by @miachel-lv which failed struct native type detection.

@Joker
Copy link
Owner

Joker commented May 6, 2020

Thanks a lot, your question helped me to find a bug which occurred due to new Go version.

@Joker Joker closed this as completed in ee26e3c May 6, 2020
@Qubitium
Copy link

Qubitium commented May 6, 2020

@Joker Awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants