Ejemplo n.º 1
0
func replaceMethodAliases(decoder *json.Decoder) (string, error) {
	// We want to allow for a syntactic suger where the user
	// can specify a method with a string such as "map" or "reduce".
	// To that end, we check for the "method" field and replace
	// the string with an actual method object before we unmarshal
	// the json spec into a protobuf message
	pipeline, err := gabs.ParseJSONDecoder(decoder)
	if err != nil {
		return "", err
	}

	// No need to do anything if the pipeline does not specify inputs
	if !pipeline.ExistsP("inputs") {
		return pipeline.String(), nil
	}

	inputs := pipeline.S("inputs")
	children, err := inputs.Children()
	if err != nil {
		return "", err
	}
	for _, input := range children {
		if !input.ExistsP("method") {
			continue
		}
		methodAlias, ok := input.S("method").Data().(string)
		if ok {
			strat, ok := pach.MethodAliasMap[methodAlias]
			if ok {
				input.Set(strat, "method")
			} else {
				return "", fmt.Errorf("unrecognized input alias: %s", methodAlias)
			}
		} else {
			if !(input.ExistsP("method.partition") && input.ExistsP("method.incremental")) {
				return "", fmt.Errorf("an input method needs to be either a string alias or a json object; please read the pipeline specification for details")
			}
			partition, ok := input.S("method", "partition").Data().(string)
			if ok {
				switch partition {
				case "block":
					input.SetP(ppsclient.Partition_BLOCK, "method.partition")
				case "file":
					input.SetP(ppsclient.Partition_FILE, "method.partition")
				case "repo":
					input.SetP(ppsclient.Partition_REPO, "method.partition")
				default:
					return "", fmt.Errorf("partition needs to be 'block', 'file', or 'repo'; got %s instead", partition)
				}
			} else {
				return "", fmt.Errorf("partition needs to be a string")
			}
		}
	}

	return pipeline.String(), nil
}
Ejemplo n.º 2
0
// EncodeJSONBytes encodes JSON-formatted bytes into JSONx. It is designed to
// be used for multiple entries so does not prepend the JSONx header tag or
// append the JSONx footer tag. You can use jsonx.Header and jsonx.Footer to
// easily add these when necessary.
func EncodeJSONBytes(input []byte) ([]byte, error) {
	o := bytes.NewBuffer(nil)
	reader := bytes.NewReader(input)
	dec := json.NewDecoder(reader)
	dec.UseNumber()

	cont, err := gabs.ParseJSONDecoder(dec)
	if err != nil {
		return nil, err
	}

	if err := sortAndTransformObject(o, &namedContainer{Container: cont}); err != nil {
		return nil, err
	}

	return o.Bytes(), nil
}