LoginSignup
4
3

More than 5 years have passed since last update.

digdag変数の評価

Last updated at Posted at 2016-10-25

以下、同じ現場の方に教えていただいた内容
digファイルの中にJS処理を書いたときは型に要注意


test1.dig
_export:
  foo: ${1 + 1}
  typeOfFoo: ${typeof foo}

+task:
  echo>: var 'foo' is '${foo}'. type of var 'foo' is '${typeOfFoo}'.
test1.digの結果
var 'foo' is '2'. type of var 'foo' is 'string'.

足し算した後はstringになった


test2.dig
_export:
  foo: ${1 + 1}
  typeOfFoo: ${typeof foo}
  incr: ${foo + 1}

+task:
  echo>: type of var 'foo' is '${typeOfFoo}'. Then, var 'incr' is concatenated '${foo}' and '1' = '${incr}'.
test2.digの結果
type of var 'foo' is 'string'. Then, var 'incr' is concatenated '2' and '1' = '21'.

stringに対する「+」は文字列連結の意味になる
数値計算したかったら「incr: \${parseInt(foo) + 1}」だと2になる


test3.dig
_export:
  foo: 1
  typeOfFoo: ${typeof foo}
  incr: ${foo + 1}

+task:
  echo>: type of var 'foo' is '${typeOfFoo}'. Then, var 'incr' is ${incr}.
test3.digの結果
type of var 'foo' is 'number'. Then, var 'incr' is 2.

YAMLの中で単純に評価するとそれはstringになる
ベタに書いてたら、ちゃんと型変換される
なので、incr: ${parseInt(foo + 1)} とかしてもincrはString

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3