github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/types/string/map.go (about)

     1  package string
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"fmt"
     7  	"strconv"
     8  
     9  	"github.com/lmorg/murex/config"
    10  	"github.com/lmorg/murex/lang/stdio"
    11  	"github.com/lmorg/murex/lang/types"
    12  )
    13  
    14  func readMap(read stdio.Io, _ *config.Config, callback func(*stdio.Map)) error {
    15  	scanner := bufio.NewScanner(read)
    16  	i := -1
    17  	for scanner.Scan() {
    18  		i++
    19  
    20  		callback(&stdio.Map{
    21  			Key:      strconv.Itoa(i),
    22  			Value:    string(bytes.TrimSpace(scanner.Bytes())),
    23  			DataType: types.String,
    24  			Last:     false,
    25  		})
    26  	}
    27  
    28  	err := scanner.Err()
    29  	if err != nil {
    30  		return fmt.Errorf("error while reading a %s map: %s", types.String, err.Error())
    31  	}
    32  
    33  	return nil
    34  }