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

     1  package sexp
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/abesto/sexp"
     7  	"github.com/lmorg/murex/config"
     8  	"github.com/lmorg/murex/lang/stdio"
     9  )
    10  
    11  func readMapC(read stdio.Io, config *config.Config, callback func(*stdio.Map)) error {
    12  	return readMap(read, config, callback, csexp)
    13  }
    14  
    15  func readMapS(read stdio.Io, config *config.Config, callback func(*stdio.Map)) error {
    16  	return readMap(read, config, callback, sexpr)
    17  }
    18  
    19  func readMap(read stdio.Io, _ *config.Config, callback func(*stdio.Map), dataType string) error {
    20  	b, err := read.ReadAll()
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	se, err := sexp.Unmarshal(b)
    26  	if err == nil {
    27  
    28  		for i := range se {
    29  			j, err := sexp.Marshal(se[i], dataType == csexp)
    30  			if err != nil {
    31  				return err
    32  			}
    33  
    34  			callback(&stdio.Map{
    35  				Key:      strconv.Itoa(i),
    36  				Value:    string(j),
    37  				DataType: dataType,
    38  				Last:     i != len(se)-1,
    39  			})
    40  		}
    41  
    42  		return nil
    43  	}
    44  	return err
    45  }