|
| 1 | +package gss |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/pkg/errors" |
| 5 | +) |
| 6 | + |
| 7 | +func Convert(input_string string, input_format string, output_format string) (string, error) { |
| 8 | + if input_format == "json" || input_format == "hcl" || input_format == "hcl2" || input_format == "toml" || input_format == "yaml" { |
| 9 | + if output_format == "json" || input_format == "hcl" || input_format == "hcl2" || output_format == "toml" || output_format == "yaml" { |
| 10 | + object := map[string]interface{}{} |
| 11 | + err := Deserialize(input_string, input_format, &object) |
| 12 | + if err != nil { |
| 13 | + return "", errors.Wrap(err, "Error deserializing input") |
| 14 | + } |
| 15 | + output_string, err := Serialize(object, output_format) |
| 16 | + if err != nil { |
| 17 | + return "", errors.Wrap(err, "Error serializing output") |
| 18 | + } |
| 19 | + return output_string, nil |
| 20 | + } else if output_format == "jsonl" { |
| 21 | + object := []map[string]interface{}{} |
| 22 | + err := Deserialize(input_string, input_format, &object) |
| 23 | + if err != nil { |
| 24 | + return "", errors.Wrap(err, "Error deserializing input") |
| 25 | + } |
| 26 | + output_string, err := Serialize(object, output_format) |
| 27 | + if err != nil { |
| 28 | + return "", errors.Wrap(err, "Error serializing output") |
| 29 | + } |
| 30 | + return output_string, nil |
| 31 | + } else if output_format == "csv" { |
| 32 | + return "", errors.New("Error: incompatible output format \"" + output_format + "\"") |
| 33 | + } else { |
| 34 | + return "", errors.New("Error: unknown input format \"" + input_format + "\"") |
| 35 | + } |
| 36 | + } else if input_format == "jsonl" || input_format == "csv" { |
| 37 | + if output_format == "json" || output_format == "hcl" || output_format == "hcl2" || output_format == "toml" || output_format == "yaml" || output_format == "jsonl" || output_format == "csv" { |
| 38 | + object := []map[string]interface{}{} |
| 39 | + err := Deserialize(input_string, input_format, &object) |
| 40 | + if err != nil { |
| 41 | + return "", errors.Wrap(err, "Error deserializing input") |
| 42 | + } |
| 43 | + output_string, err := Serialize(object, output_format) |
| 44 | + if err != nil { |
| 45 | + return "", errors.Wrap(err, "Error serializing output") |
| 46 | + } |
| 47 | + return output_string, nil |
| 48 | + } else { |
| 49 | + return "", errors.New("Error: unknown output format \"" + input_format + "\"") |
| 50 | + } |
| 51 | + } |
| 52 | + return "", errors.New("Error: unknown input format \"" + input_format + "\"") |
| 53 | +} |
0 commit comments