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

add generate-package-code #167

Closed
wants to merge 1 commit into from

Conversation

chyroc
Copy link
Contributor

@chyroc chyroc commented Mar 26, 2018

No description provided.

@codecov-io
Copy link

codecov-io commented Mar 26, 2018

Codecov Report

Merging #167 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #167   +/-   ##
=======================================
  Coverage   81.72%   81.72%           
=======================================
  Files           8        8           
  Lines        1882     1882           
=======================================
  Hits         1538     1538           
  Misses        257      257           
  Partials       87       87

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9bd0532...efaebbe. Read the comment docs.

@chyroc
Copy link
Contributor Author

chyroc commented Mar 26, 2018

At present, we only take bytes as an example of pr. If you think ok, I will add other packages.

@chyroc
Copy link
Contributor Author

chyroc commented Mar 26, 2018

No, I mean maybe these standard packages can be done by generating code, because each package may have updates, and it is considered difficult to detect and update the code.
Then I just give the generated code and use bytes as the sample code. If I can, I will use the code to generate the code under the wrong package.

@MichaelS11
Copy link
Contributor

I now see what this is doing, very cool. The concept is great. :)

I am not sure if it will work in all cases. In particular, packages that have new methods adding to them, like sort as an example. There is a sort 1.8 build. Also note that some packages have appengine build constraints/tags.

It should at least be able to use as a good starting template for checking what we have or adding new packages.

@mattn
Copy link
Owner

mattn commented Mar 26, 2018

It is difficult to create a portable package file using this tool. Because syscall has different methods depending on OS. So I think that it must have a dictionary or whitelist.

@chyroc
Copy link
Contributor Author

chyroc commented Mar 26, 2018

can use makefile to make only specific packages use the generated code

@MichaelS11
Copy link
Contributor

Might be better for how it is now to remove the makefile and just run it manually as wanted. Great tool, can do lots with it, just not full automatic.

@MichaelS11
Copy link
Contributor

@chyroc
Could you please fix bytes?

@chyroc
Copy link
Contributor Author

chyroc commented Mar 28, 2018

I have resolved the conflict, but there are still some problems.

Columns such as

	json.Number // type Number string
	time.Nanosecond // const Nanosecond  Duration = 1
	cookiejar.PublicSuffixList // type PublicSuffixList interface { }

are a type(but is not a struct) or a var or interface, so they cannot use AAA{}. At the moment I have added them to the blacklist, but I don't think this is a good way. Maybe we should support this type(but is not a struct) / var / interface, but what should we do?

@MichaelS11
Copy link
Contributor

time.Nanosecond is already added:
https://github.com/mattn/anko/blob/master/packages/time.go

@MichaelS11
Copy link
Contributor

I think something like this will work:
PackageTypes["encoding/json"] = map[string]interface{}{
"Number": json.Number(""),
}

Also can check out Signal:
https://github.com/mattn/anko/blob/master/packages/os.go

@MichaelS11
Copy link
Contributor

Note that the build tests are failing because json.Valid was not added until go 1.9

@MichaelS11
Copy link
Contributor

@chyroc
Just wanted to double check, you know this PR cannot be merged because the build is failing, right?

@chyroc
Copy link
Contributor Author

chyroc commented Apr 3, 2018

I noticed, I will fix him, I'm sorry to be busy recently

@chyroc
Copy link
Contributor Author

chyroc commented Apr 8, 2018

I have a problem: The type of import is now through:
1, bytes.Buffer{} imported in this way,
2, the type of non-structure is imported in this way

var signal os.Signal
reflect.TypeOf(&signal).Elem()

Whether to continue this way, or unified as the second way

@mattn
Copy link
Owner

mattn commented Apr 8, 2018

interface can be nil. For example, This is typed nil.

v := io.Writer(nil)

@MichaelS11
Copy link
Contributor

A note, I think this code should try to handle all the cases. This code is great for generating a template that can be updated by hand.

@chyroc
Copy link
Contributor Author

chyroc commented Apr 9, 2018

how to deal with math.MaxUint64, it return constant 18446744073709551615 overflows int in my machine

@chyroc chyroc force-pushed the add/generate-package-code branch from 40b4b97 to 56a5ee5 Compare April 9, 2018 07:27
@MichaelS11
Copy link
Contributor

Should be able to just convert it to a uint64.

golang/go#19621
golang/go#23086

@MichaelS11
Copy link
Contributor

Have you read up on Build Constraints?

https://golang.org/pkg/go/build/

Looks like there are extra Build Constraints that are not wanted.

For example:

// +build go1.8,!go1.10

Which means build if (1.8 or greater) OR (not 1.10 or great), which probably not what we are going for.

@MichaelS11
Copy link
Contributor

Also there is the custom build tag appengine which I believe is used to create the engine for the web site. So // +build !appengine needs to be accounted for.

@@ -8,4 +8,6 @@ func init() {
Packages["errors"] = map[string]interface{}{
"New": errors.New,
}
PackageTypes["errors"] = map[string]interface{}{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty PackageTypes can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

func init_math() {

Packages["math"]["Erfcinv"] = math.Erfcinv

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but would be nice to remove the extra spaces.

packages/os.go Outdated
"ErrExist": os.ErrExist,
"ErrInvalid": os.ErrInvalid,
"ErrNotExist": os.ErrNotExist,
"ErrPermission": os.ErrPermission,
"Executable": os.Executable,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the added function should be added to // +build !appengine like Executable, Unsetenv, and etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Then just ask, how to know the list of // +build !appengine

packages/sort.go Outdated
"Search": sort.Search,
"SearchFloat64s": sort.SearchFloat64s,
"SearchInts": sort.SearchInts,
"SearchStrings": sort.SearchStrings,
"Slice": sort.Slice,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slice* should be added to // +build go1.8

@chyroc
Copy link
Contributor Author

chyroc commented Apr 9, 2018

Yes, I read ~
The comma means and and the space means or, so the // +build go1.8,!go1.10 meaning here is 1.8 and above, but does not include 1.10

For example, the function init_math:
// +build go1.8,!go1.10 will build under 1.8/1.9 in file packages/math.18.go
// +build go1.10 will build under 1.10 in file packages/math.110.go
and they called by file packages/math.go

Take the function init_encoding_json for example:
// +build go1.8,!go1.9,!go1.10 will build under 1.8 in file packages/encoding.json.18.go
// +build go1.9 go1.10 will build under 1.9/1.10 in file packages/encoding.json.19.110.go
and they called by file packages/encoding.json.go

@MichaelS11
Copy link
Contributor

Also, how are you checking what version each of the newly adding functions and types are in?

@MichaelS11
Copy link
Contributor

Looking at json.Valid, that should be // +build go1.9 and the other file should be // +build !go1.9.

@chyroc
Copy link
Contributor Author

chyroc commented Apr 9, 2018

Maybe we can run ./cmd/generate-std-packages/run.sh in the ci test. If there is a change in the code, there is a new file, and ci should fail.
Of course, it can also be performed manually on a regular basis (so as not to block other people's pr)

@MichaelS11
Copy link
Contributor

While we are only testing 1.8, 1.9, 1.10 in travis, still want to try to be compatible to lower versions when/if possible. That means trying to figure out which version functions and types were added, for all the newly added functions and types.

@MichaelS11
Copy link
Contributor

Note #49 for overflows int issue.

@chyroc chyroc force-pushed the add/generate-package-code branch from 76a4572 to f993ed4 Compare April 9, 2018 14:48
@chyroc
Copy link
Contributor Author

chyroc commented Apr 9, 2018

Should we first create maps of all packages and then add functions or variables one by one.
then add

l.Lock()
if Packages["name"] = nil {
Packages["name"]  = make(map[string]interface{})
}
l.Unlock()

This avoids the need to create two file builds similar to the appengine environment

@chyroc chyroc force-pushed the add/generate-package-code branch from f993ed4 to 4f7d68f Compare April 9, 2018 15:18
@MichaelS11
Copy link
Contributor

MichaelS11 commented Apr 9, 2018

We are adding functions or variables one at a time, don't think a lock is needed. Not seeing how this avoids the need for build files.

If you want to have only have two appengine build files, can have those files sets a flag, then use that flag to set the data.

@chyroc chyroc force-pushed the add/generate-package-code branch 5 times, most recently from a87070e to 9bd7bd3 Compare June 15, 2018 07:39
@chyroc chyroc force-pushed the add/generate-package-code branch from 9bd7bd3 to efaebbe Compare June 15, 2018 07:42
@chyroc
Copy link
Contributor Author

chyroc commented Jun 15, 2018

run such code to generate code:

#!/bin/bash

mkdir go && cd go

for i in 1.6.4 1.7.6 1.8.7 1.9.6 1.10.3
do
   echo "download go $i ing."
   wget https://dl.google.com/go/go$i.linux-amd64.tar.gz && tar zxvf go$i.linux-amd64.tar.gz && mv go $i
   rm go$i.linux-amd64.tar.gz
   echo "done."
done

cd ..

find packages | grep -vE '_test|Engine' | grep '/' | xargs rm

go run cmd/generate-std-packages/*.go -go16 ./go/1.6.4/bin/go -go17 ./go/1.7.6/bin/go -go18 ./go/1.8.7/bin/go -go19 ./go/1.9.6/bin/go -go110 ./1.10.3/bin/go && gofmt -s -w ./packages/*

@chyroc
Copy link
Contributor Author

chyroc commented Jun 15, 2018

@MichaelS11 @mattn please see again, thank you.

@MichaelS11
Copy link
Contributor

I leave this to @mattn

I was fine with the simple starting template and making changes by hand.

@mattn
Copy link
Owner

mattn commented Jun 16, 2018

I don't have strong opnion, Is it possible to make composite literal like?

Packages["bytes"] = map[string]interface{}{
   ...
}

@chyroc
Copy link
Contributor Author

chyroc commented Jun 16, 2018

should i add // Code generated by cmd/generate-std-packages. DO NOT EDIT. on the head of file

@chyroc chyroc closed this Jul 27, 2018
@chyroc chyroc deleted the add/generate-package-code branch July 27, 2018 12:12
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

Successfully merging this pull request may close these issues.

4 participants