github.com/kaiya/goutils@v1.0.1-0.20230226104005-4ae4a4dc3688/io/file/parse.go (about) 1 package file 2 3 import ( 4 "bufio" 5 "encoding/csv" 6 "io" 7 "os" 8 9 "github.com/goccy/go-json" 10 "github.com/pkg/errors" 11 ) 12 13 func ReadCsvRecordsFromFile(filename string) ([][]string, error) { 14 file, err := os.Open(filename) 15 if err != nil { 16 return nil, errors.Wrap(err, "open file") 17 } 18 csvReader := csv.NewReader(file) 19 csvReader.FieldsPerRecord = -1 20 records, err := csvReader.ReadAll() 21 if err != nil { 22 return nil, errors.Wrap(err, "read csv") 23 } 24 return records, nil 25 } 26 27 func WriteRecordsToFile(filename string, records [][]string, header ...string) error { 28 f, err := os.Create(filename) 29 if err != nil { 30 return errors.Wrap(err, "create file") 31 } 32 writer := csv.NewWriter(f) 33 if len(header) > 0 { 34 writer.Write(header) 35 writer.Flush() 36 } 37 return writer.WriteAll(records) 38 } 39 40 func ParseJsonFile(path string, out interface{}) error { 41 file, err := os.Open(path) 42 if err != nil { 43 return errors.Wrap(err, "open file") 44 } 45 bytes, err := io.ReadAll(bufio.NewReader(file)) 46 if err != nil { 47 return errors.Wrap(err, "readall") 48 } 49 return json.Unmarshal(bytes, out) 50 } 51 52 // json format {"data": ["a","b"]} 53 func DiffJsonFiles(allPath, setPath string) ([]string, error) { 54 setDocs := struct { 55 Data []string `json:"data"` 56 }{} 57 err := ParseJsonFile(setPath, &setDocs) 58 if err != nil { 59 return nil, err 60 } 61 allDocs := struct { 62 Data []string `json:"data"` 63 }{} 64 err = ParseJsonFile(allPath, &allDocs) 65 if err != nil { 66 return nil, err 67 } 68 return DiffSlices(allDocs.Data, setDocs.Data), nil 69 } 70 71 // get not exists of all in set 72 // TODO replace in place 73 func DiffSlices(all, set []string) []string { 74 setMap := make(map[string]struct{}) 75 for _, s := range set { 76 setMap[s] = struct{}{} 77 } 78 ret := make([]string, 0) 79 for _, a := range all { 80 if _, ok := setMap[a]; !ok { 81 ret = append(ret, a) 82 } 83 } 84 return ret 85 }