How to convert a subset of YAML into an indexed ar

2019-08-22 16:07发布

I am using Bash 4.3 on linux.

I have this simple YAML-esque data file:

products:
  product1:
    name: "Product one"
    price: 100
  product2:
    name: "Product two"
    price: 200
myList:
  - one
  - two

And I need a shell function that, taking the above YAML file as input, can generate and then execute the below Bash code:

unset products product1  product2

# declare the associative arrays
declare -A product1
declare -A product2


# define the data
product1=(
  [name]="Product 1"
  [price]=100
)

product2=(
  [name]="Product 2"
  [price]=200
)

myList=(one two)

# declare the arrays which will contain the names of our associative arrays
products=(product1 product2)

Once I have this wonderful function, I will use the YAML files to automatically generate data, to be used in my custom CMS templating system like so:

{{#foreach product in products}}
  <h3>{{product.name | uppercase}}</h3>
  * {{product.price | money_with_currency £ GBP | without_trailing_zeros}}
{{/foreach}}

I have already tried various YAML parsers, but have not found one that can generate the associative arrays that I need, and some simply didn't work at all (for me, at least):

Most of these, as far as I understand their usage generate things like product_product1_name="foo" :(

1条回答
▲ chillily
2楼-- · 2019-08-22 16:32

yaml.sh, which you linked in the question, is a surprisingly good parser. It's a lot easier to convert its output into the format you need than to do anything else.

cleanupValue() {
  local result
  case $1 in
    '"'*'"') result=${1#'"'}; result=${result%'"'} ;;
    "["*"]") result=${1#'['}; result=${result%']'} ;;
    *)       result=$1
  esac
  printf '%s\n' "$result"
}

record_data() {
  local key value="$(cleanupValue "$1")"; shift
  while (( $# >= 2 )); do
    key=$(cleanupValue "$2")
    if (( $# > 2 )); then
      declare -g -a "$1" || continue
      declare -g -A "_${1}__seen" || continue
      local -n __array="$1"
      local -n __seen_array="_${1}__seen"
      if ! [[ ${__seen_array[$key]} ]]; then
        __seen_array[$key]=1
        __array+=( "$key" )
      fi
      unset -n __seen_array
    else
      declare -g -A "$1"    || continue  # "continue" to skip invalid variable names
      local -n __array="$1" || continue
      __array[$key]=$value
    fi
    unset -n __array
    shift
  done
}

while IFS='=' read -r key value; do
  IFS=. read -r -a key_pieces <<<"$key"
  record_data "$value" "${key_pieces[@]}"
done < <(ysh -f your.yml)

# demonstrate results
declare -p products product1 product2 myList

...emits as output:

declare -a products=([0]="product1" [1]="product2")
declare -A product1=([price]="100" [name]="Product one" )
declare -A product2=([price]="200" [name]="Product two" )
declare -A myList=([1]="two" [0]="one" )
查看更多
登录 后发表回答