Skip to content
This repository has been archived by the owner on Mar 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from prysmaticlabs/bytes
Browse files Browse the repository at this point in the history
Better struct generation for ETH2 spec tests
  • Loading branch information
prestonvanloon authored Jun 18, 2019
2 parents cc20b0e + cb73b01 commit cdf6eae
Show file tree
Hide file tree
Showing 14 changed files with 35,545 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ bazel-*
node_modules
dist

autogenerated.yaml.go

# Test or generated files.
*.yaml.go
*.yaml
20 changes: 12 additions & 8 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Add rules here to build your software
# See https://docs.bazel.build/versions/master/build-ref.html#BUILD_files
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@bazel_gazelle//:def.bzl", "gazelle")

# Allow any ts_library rules in this workspace to reference the config
# Note: if you move the tsconfig.json file to a subdirectory, you can add an alias() here instead
# so that ts_library rules still use it by default.
# See https://www.npmjs.com/package/@bazel/typescript#installation
exports_files(["tsconfig.json"], visibility = ["//:__subpackages__"])
# gazelle:exclude node_modules
# gazelle:prefix github.com/prysmaticlabs/yaml-to-go
gazelle(name = "gazelle")

exports_files(
["tsconfig.json"],
visibility = ["//:__subpackages__"],
)

load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")
Expand All @@ -29,9 +32,10 @@ ts_library(
)

nodejs_binary(
name = "bin",
name = "yaml-to-go-binary",
data = [
":main",
],
entry_point = "yaml_to_go/main",
)

17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# Yaml to Go

Work in progress!

Create a go package from a given yaml file.

Caveats specifically for processing [Ethereum 2.0 yaml spec tests](https://github.com/ethereum/eth2.0-spec-tests):

- Hex strings are treated as bytes. You must handle hex encoded strings as
binary data when serializing the yaml data.
- If a hex encoded byte string is 32, 48, or 96 bytes, then ssz tag is added
to the struct. Example: `ssz:"size=32"`
- All number values are treated as unsigned 64 bit integers, unless it is a
decimal number then it would be float64.
- This library actually adds `json` tags to the structs because that seems to
be the only way it works with [github.com/ghodss/yaml](github.com/ghodss/yaml)


Note: This project is not unit tested or held to any high code standards.
It exists purely due to the nuances in eth2 spec tests where existing code
generation and yaml processing would not work. In other words, it's bad!

## Credits

Expand Down
44 changes: 42 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ workspace(
managed_directories = {"@npm": ["node_modules"]},
)

# Install the nodejs "bootstrap" package
# This provides the basic tools for running and packaging nodejs programs in Bazel
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "bc180118b9e1c7f2b74dc76a8f798d706fe9fc53470ef9296728267b4cd29441",
Expand All @@ -22,6 +21,7 @@ http_archive(
# The yarn_install rule runs yarn anytime the package.json or yarn.lock file changes.
# It also extracts and installs any Bazel rules distributed in an npm package.
load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install")

yarn_install(
# Name this npm so that Bazel Label references look like @npm//package
name = "npm",
Expand All @@ -31,4 +31,44 @@ yarn_install(

# Install any Bazel rules which were extracted earlier by the yarn_install rule.
load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")

install_bazel_dependencies()

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_go",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/0.18.6/rules_go-0.18.6.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/0.18.6/rules_go-0.18.6.tar.gz",
],
sha256 = "f04d2373bcaf8aa09bccb08a98a57e721306c8f6043a2a0ee610fd6853dcde3d",
)

http_archive(
name = "bazel_gazelle",
urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/0.17.0/bazel-gazelle-0.17.0.tar.gz"],
sha256 = "3c681998538231a2d24d0c07ed5a7658cb72bfb5fd4bf9911157c0e9ac6a2687",
)

load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")

go_rules_dependencies()

go_register_toolchains()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

gazelle_dependencies()

go_repository(
name = "in_gopkg_yaml_v2",
commit = "51d6538a90f86fe93ac480b35f37b2be17fef232",
importpath = "gopkg.in/yaml.v2",
)

go_repository(
name = "com_github_ghodss_yaml",
commit = "25d852aebe32c875e9c044af3eef9c7dc6bc777f",
importpath = "github.com/ghodss/yaml",
)
11 changes: 9 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ if (process.argv.length < 3) {
process.exitCode = 1;
} else {
const filepath = process.argv[2];
let structName = process.argv[3];

console.log(`Generating go structs from ${filepath}`);
if (structName == "") {
console.log('No struct/filename name given, using AutoGenerated');
structName = 'AutoGenerated';
}

const file = fs.readFileSync(filepath, 'utf8');
const result = yamlToGo(file, 'AutoGenerated', false /*flatten*/);
const result = yamlToGo(file, structName, false /*flatten*/);
const header = `// Code generated by yaml_to_go. DO NOT EDIT.
// source: ${filepath}
package autogenerated
`;

fs.writeFileSync("autogenerated.yaml.go", header + result.go);
const output = structName.toLowerCase() + '.yaml.go';
fs.writeFileSync(output, header + result.go);
console.log(`Wrote ${output}`);
}
Loading

0 comments on commit cdf6eae

Please sign in to comment.