// processFile reads a file containing vector sets, at least in the format
// preferred by our lab, and writes the results to stdout.

processFile reads a file containing vector sets, at least in the format
preferred by our lab, and writes the results to stdout.
// processFile reads a file containing vector sets at least in the format // preferred by our lab and writes the results to stdout .
func processFile(filename string, supportedAlgos []map[string]interface{}, middle Middle) error {
	jsonBytes, err := ioutil.ReadFile(filename)
	if err != nil {
		return err
	}
	var elements []json.RawMessage
	if err := json.Unmarshal(jsonBytes, &elements); err != nil {
		return err
	}
	if len(elements) < 1 {
		return errors.New("JSON input is empty")
	}
	var header json.RawMessage
	if looksLikeHeaderElement(elements[0]) {
		header, elements = elements[0], elements[1:]
		if len(elements) == 0 {
			return errors.New("JSON input is empty")
		}
	}
	algos := make(map[string]struct{})
	for _, supportedAlgo := range supportedAlgos {
		algoInterface, ok := supportedAlgo["algorithm"]
		if !ok {
			continue
		}
		algo, ok := algoInterface.(string)
		if !ok {
			continue
		}
		algos[algo] = struct{}{}
	}
	var result bytes.Buffer
	result.WriteString("[")
	if header != nil {
		headerBytes, err := json.MarshalIndent(header, "", "    ")
		if err != nil {
			return err
		}
		result.Write(headerBytes)
		result.WriteString(",")
	}
	for i, element := range elements {
		var commonFields struct {
			Algo string `json:"algorithm"`
			ID   uint64 `json:"vsId"`
		}
		if err := json.Unmarshal(element, &commonFields); err != nil {
			return fmt.Errorf("failed to extract common fields from vector set #%d", i+1)
		}
		algo := commonFields.Algo
		if _, ok := algos[algo]; !ok {
			return fmt.Errorf("vector set #%d contains unsupported algorithm %q", i+1, algo)
		}
		replyGroups, err := middle.Process(algo, element)
		if err != nil {
			return fmt.Errorf("while processing vector set #%d: %s", i+1, err)
		}
		group := map[string]interface{}{
			"vsId":       commonFields.ID,
			"testGroups": replyGroups,
		}
		replyBytes, err := json.MarshalIndent(group, "", "    ")
		if err != nil {
			return err
		}
		if i != 0 {
			result.WriteString(",")
		}
		result.Write(replyBytes)
	}
	result.WriteString("]\n")
	os.Stdout.Write(result.Bytes())
	return nil
}
func processFile ( filename string , supportedAlgos [ ] map [ string ] interface { } , middle Middle ) error { jsonBytes , err := ioutil . ReadFile ( filename ) if err != nil { return err } var elements [ ] json . RawMessage if err := json . Unmarshal ( jsonBytes , & elements ) ; err != nil { return err } if len ( elements ) < 1 { return errors . New ( " " ) } var header json . RawMessage if looksLikeHeaderElement ( elements [ 0 ] ) { header , elements = elements [ 0 ] , elements [ 1 : ] if len ( elements ) == 0 { return errors . New ( " " ) } } algos := make ( map [ string ] struct { } ) for _ , supportedAlgo := range supportedAlgos { algoInterface , ok := supportedAlgo [ " " ] if ! ok { continue } algo , ok := algoInterface . ( string ) if ! ok { continue } algos [ algo ] = struct { } { } } var result bytes . Buffer result . WriteString ( " " ) if header != nil { headerBytes , err := json . MarshalIndent ( header , " " , " " ) if err != nil { return err } result . Write ( headerBytes ) result . WriteString ( " " ) } for i , element := range elements { var commonFields struct { Algo string `json:"algorithm"` ID uint64 `json:"vsId"` } if err := json . Unmarshal ( element , & commonFields ) ; err != nil { return fmt . Errorf ( " " , i + 1 ) } algo := commonFields . Algo if _ , ok := algos [ algo ] ; ! ok { return fmt . Errorf ( " " , i + 1 , algo ) } replyGroups , err := middle . Process ( algo , element ) if err != nil { return fmt . Errorf ( " " , i + 1 , err ) } group := map [ string ] interface { } { " " : commonFields . ID , " " : replyGroups , } replyBytes , err := json . MarshalIndent ( group , " " , " " ) if err != nil { return err } if i != 0 { result . WriteString ( " " ) } result . Write ( replyBytes ) } result . WriteString ( " \n " ) os . Stdout . Write ( result . Bytes ( ) ) return nil }
processFile reads a file containing vector sets, at least in the format
preferred by our lab, and writes the results to stdout.
processFile reads a file containing vector sets at least in the format preferred by our lab and writes the results to stdout .
// There must be at least one element in the file.
 // Build a map of which algorithms our Middle supports.
