Beispiel #1
0
// UnmarshalPtr YAML/JSON/TOML serialized data.
func UnmarshalPtr(ptr interface{}, b []byte, f DataFmt) error {
	switch f {
	case YAML:
		if err := yaml.Unmarshal(b, ptr); err != nil {
			return err
		}
	case TOML:
		if err := toml.Unmarshal(b, ptr); err != nil {
			return err
		}
	case JSON:
		if err := json.Unmarshal(b, ptr); err != nil {
			return err
		}
	default:
		return errors.New("unsupported data format")
	}

	return nil
}
Beispiel #2
0
// Unmarshal YAML/JSON/TOML serialized data.
func Unmarshal(b []byte, f DataFmt) (interface{}, error) {
	var d interface{}

	switch f {
	case YAML:
		if err := yaml.Unmarshal(b, &d); err != nil {
			return nil, err
		}
	case TOML:
		if err := toml.Unmarshal(b, &d); err != nil {
			return nil, err
		}
	case JSON:
		if err := json.Unmarshal(b, &d); err != nil {
			return nil, err
		}
	default:
		return nil, errors.New("unsupported data format")
	}

	return d, nil
}