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 type_compat script - backwards compat fixes type errors #149

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ before_script:

script: "make test"
otp_release:
- 17.0-rc1
- 17.1
- 17.0
- R16B03-1
- R16B
- R15B03
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ APP_NAME=emysql
MODULES=$(shell ls -1 src/*.erl | awk -F[/.] '{ print $$2 }' | sed '$$q;s/$$/,/g')
MAKETIME=$(shell date)

all: crypto_compat app
all: compat app
(cd src;$(MAKE))

app: ebin/$(PKGNAME).app

crypto_compat:
compat:
(escript support/crypto_compat.escript)
(escript support/type_compat.escript)

ebin/$(PKGNAME).app: src/$(PKGNAME).app.src
mkdir -p ebin
Expand Down Expand Up @@ -55,6 +56,7 @@ clean:
rm -f *.beam
rm -f *.html
rm -f include/crypto_compat.hrl
rm -f include/type_compat.hrl
rm -fr logs

package: clean
Expand Down
8 changes: 4 additions & 4 deletions include/emysql.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
%% 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.

-include("type_compat.hrl").

-record(pool, {pool_id :: atom(),
size :: number(),
Expand All @@ -34,9 +34,9 @@
port :: number(),
database :: string(),
encoding :: utf8 | latin1 | {utf8, utf8_unicode_ci} | {utf8, utf8_general_ci},
available=queue:new() :: queue(),
locked=gb_trees:empty() :: gb_tree(),
waiting=queue:new() :: queue(),
available=queue:new() :: emysql_queue(),
locked=gb_trees:empty() :: emysql_gb_tree(),
waiting=queue:new() :: emysql_queue(),
start_cmds=[] :: string(),
conn_test_period=0 :: number(),
connect_timeout=infinity :: number() | infinity,
Expand Down
5 changes: 3 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
% -*- Erlang -*-
% vim: ts=4 sw=4 et ft=erlang
{erl_opts, [
nowarn_deprecated_type
]}.
{pre_hooks,[
{"linux|bsd|darwin|solaris", compile, "escript ./support/crypto_compat.escript"},
{"win32", compile, "escript.exe support/crypto_compat.escript"}
{"linux|bsd|darwin|solaris", compile, "escript ./support/type_compat.escript"},
{"win32", compile, "escript.exe support/crypto_compat.escript"},
{"win32", compile, "escript.exe support/type_compat.escript"}
]}.
2 changes: 1 addition & 1 deletion src/emysql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ result_type(#eof_packet{}) -> eof.
-spec as_dict(Result) -> Dict
when
Result :: #result_packet{},
Dict :: dict().
Dict :: emysql_dict().
as_dict(Res) -> emysql_conv:as_dict(Res).


Expand Down
2 changes: 1 addition & 1 deletion src/emysql_conn_mgr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

-include("emysql.hrl").

-record(state, {pools, lockers = dict:new() :: dict()}).
-record(state, {pools, lockers = dict:new() :: emysql_dict()}).

%%====================================================================
%% API
Expand Down
58 changes: 58 additions & 0 deletions support/type_compat.escript
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env escript
%% vim: ts=4 sw=4 et ft=erlang

%% Erlang 17 Changed the expected types of dict(), gb_trees(), etc to require
%% prefixing with the module name. This caused a non-backwards compatibile
%% break with versions prior to Erlang 17.
%%
%% This script will check the Erlang version and if otp_release >= 17, it will create
%% custom types called emysql_dict(), etc which just refer to dict:dict(), making the
%% types fully backwards compatible, and if OTP prior to 17, will create
%% emysql_dict() which refers to dict().
%%
%% It will do this for dict(), queue(), and gb_tree()
%%
%% Note: This should be run *before* compiling!

-define(TYPE_PREFIX, "emysql_").
-define(TYPES, [
%{old version, new version}
{"dict()", "dict:dict()"},
{"queue()", "queue:queue()"},
{"gb_tree()", "gb_trees:tree()"}
]).

main([]) ->
crypto:start(),

Filename = "include/type_compat.hrl",
io:format("Generating ~p ...~n", [Filename]),

TypeStrings = process_otp_version(erlang:system_info(otp_release)),

Contents = [
"%% Note: This file was automatically generated. Do not include it in source control\n",
TypeStrings
],

ContentsBin = iolist_to_binary(Contents),
case file:read_file(Filename) of
{ok, ContentsBin} ->
io:format("...no changes needed to ~p. Skipping writing new file~n", [Filename]);
_ ->
io:format("...writing ~p~n", [Filename]),
file:write_file(Filename, Contents)
end.

process_otp_version(Version = "R" ++ _) ->
io:format("...OTP ~s does not require module prefixes for types...~n",[Version]),
[make_type_pre_17(OldType) || {OldType, _} <- ?TYPES];
process_otp_version(Version) ->
io:format("...OTP ~s requires module prefixes for types...~n",[Version]),
[make_type_17_plus(Types) || Types <- ?TYPES].

make_type_pre_17(Type) ->
["-type ",?TYPE_PREFIX,Type, " :: ",Type,".\n"].

make_type_17_plus({Type, FullType}) ->
["-type ",?TYPE_PREFIX,Type, " :: ",FullType,".\n"].