Skip to content

Commit

Permalink
variable expansion package
Browse files Browse the repository at this point in the history
  • Loading branch information
hayeah committed Dec 11, 2017
1 parent 7519b23 commit 9034590
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
16 changes: 16 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package solar

import (
"encoding/hex"
"fmt"
"log"
"net/url"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/qtumproject/solar/deployer"
"github.com/qtumproject/solar/deployer/eth"
"github.com/qtumproject/solar/deployer/qtum"
"github.com/qtumproject/solar/varstr"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

Expand Down Expand Up @@ -106,6 +108,20 @@ func (c *solarCLI) QtumRPC() *qtum.RPC {
return rpc
}

// ExpandJSONParams uses variable substitution syntax (e.g. $Foo, ${Foo}) to as placeholder for contract addresses
func (c *solarCLI) ExpandJSONParams(jsonParams string) string {
repo := c.ContractsRepository()

return varstr.Expand(jsonParams, func(key string) string {
contract, found := repo.Get(key)
if !found {
panic(errors.Errorf("Invalid address expansion: %s", key))
}

return fmt.Sprintf("%#v", hex.EncodeToString(contract.Address))
})
}

func (c *solarCLI) Deployer() (deployer deployer.Deployer) {
log := log.New(os.Stderr, "", log.Lshortfile)

Expand Down
9 changes: 8 additions & 1 deletion deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ func init() {

fmt.Printf(" \033[36mdeploy\033[0m %s => %s\n", target.file, target.name)

err = deployer.CreateContract(contract, []byte(*jsonParams), target.name, *force, *aslib)
var params []byte
if jsonParams != nil {
jsonParams := solar.ExpandJSONParams(*jsonParams)

params = []byte(jsonParams)
}

err = deployer.CreateContract(contract, params, target.name, *force, *aslib)
if err != nil {
fmt.Println("\u2757\ufe0f \033[36mdeploy\033[0m", err)
return
Expand Down
4 changes: 3 additions & 1 deletion encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func init() {

var params []interface{}
if jsonParams != nil {
err := json.Unmarshal([]byte(*jsonParams), &params)
jsonParams := solar.ExpandJSONParams(*jsonParams)

err := json.Unmarshal([]byte(jsonParams), &params)
if err != nil {
return err
}
Expand Down
48 changes: 48 additions & 0 deletions varstr/expand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package varstr

// Modified from stdlib's os.Expand function. Remove special shell variable expansion.
// https://github.com/golang/go/blob/0da486dc72743faa6e601444dcc35728704a1545/src/os/env.go#L13

// Expand replaces ${var} or $var in the string based on the mapping function.
func Expand(s string, mapping func(string) string) string {
buf := make([]byte, 0, 2*len(s))
// ${} is all ASCII, so bytes are fine for this operation.
i := 0
for j := 0; j < len(s); j++ {
if s[j] == '$' && j+1 < len(s) {
buf = append(buf, s[i:j]...)
name, w := getVarName(s[j+1:])
buf = append(buf, mapping(name)...)
j += w
i = j + 1
}
}
return string(buf) + s[i:]
}

// isAlphaNum reports whether the byte is an ASCII letter, number, or underscore
func isAlphaNum(c uint8) bool {
return c == '_' || '0' <= c && c <= '9' || 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z'
}

// getVarName returns the name that begins the string and the number of bytes
// consumed to extract it. If the name is enclosed in {}, it's part of a ${}
// expansion and two more bytes are needed than the length of the name.
func getVarName(s string) (string, int) {
if s[0] == '{' {
// Scan to closing brace
for i := 1; i < len(s); i++ {
if s[i] == '}' {
return s[1:i], i + 1
}
}
return "", 1 // Bad syntax; just eat the brace.

}

// Scan alphanumerics.
var i int
for i = 0; i < len(s) && isAlphaNum(s[i]); i++ {
}
return s[:i], i
}

0 comments on commit 9034590

Please sign in to comment.