From e1e1764e323e220c800d3ab1d43da12745a9b86b Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 18 Jun 2020 02:29:52 +0100 Subject: [PATCH] feat(nix/buildTypedGo): Add a //nix/buildGo wrapper for typed Go These functions work like buildGo.program & buildGo.package, but run the .go2 sources through go2go first before passing them to the //nix/buildGo equivalents. Change-Id: Id1ebab6085c390d6986387370181377b9f5d39e8 --- nix/buildTypedGo/default.nix | 33 ++++++++++++++++++++++++++++ nix/buildTypedGo/example/default.nix | 8 +++++++ nix/buildTypedGo/example/main.go2 | 15 +++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 nix/buildTypedGo/default.nix create mode 100644 nix/buildTypedGo/example/default.nix create mode 100644 nix/buildTypedGo/example/main.go2 diff --git a/nix/buildTypedGo/default.nix b/nix/buildTypedGo/default.nix new file mode 100644 index 000000000..1c4434849 --- /dev/null +++ b/nix/buildTypedGo/default.nix @@ -0,0 +1,33 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# A crude wrapper around //nix/buildGo that supports the Go 2 alpha. +# +# The way the alpha is implemented is via a transpiler from typed to +# untyped Go. +{ depot, pkgs, ... }: + +let + inherit (builtins) + stringLength + substring; + + inherit (depot.nix.buildGo) gpackage program; + + go2goext = file: substring 0 ((stringLength file) - 1) file; + go2go = file: pkgs.runCommandNoCC "${go2goext (toString file)}" {} '' + cp ${file} . + ${pkgs.go}/bin/go tool go2go translate *.go2 + mv *.go $out + ''; + +in rec { + program = { name, srcs, deps ? [], x_defs ? {} }: depot.nix.buildGo.program { + inherit name deps x_defs; + srcs = map go2go srcs; + }; + + package = { name, srcs, deps ? [], x_defs ? {}, sfiles ? [] }: depot.nix.buildGo.package { + inherit name deps x_defs sfiles; + srcs = map go2go srcs; + }; +} diff --git a/nix/buildTypedGo/example/default.nix b/nix/buildTypedGo/example/default.nix new file mode 100644 index 000000000..5b6d4171f --- /dev/null +++ b/nix/buildTypedGo/example/default.nix @@ -0,0 +1,8 @@ +{ depot, ... }: + +depot.nix.buildTypedGo.program { + name = "example"; + srcs = [ + ./main.go2 + ]; +} diff --git a/nix/buildTypedGo/example/main.go2 b/nix/buildTypedGo/example/main.go2 new file mode 100644 index 000000000..8986f57b9 --- /dev/null +++ b/nix/buildTypedGo/example/main.go2 @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" +) + +func Print(type T)(s []T) { + for _, v := range s { + fmt.Print(v) + } +} + +func main() { + Print([]string{"Hello, ", "TVL\n"}) +}