Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI

on:
push:
branches:
- master
pull_request:

jobs:
ci:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6

- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: stable

- name: Test
run: go test -timeout 60s -v ./...

- name: golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: latest
23 changes: 23 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
on:
release:
types: [created]

permissions:
contents: write
packages: write

jobs:
release-go-binary:
name: release Go Binary
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux, darwin]
goarch: [amd64, arm64]
steps:
- uses: actions/checkout@v6
- uses: wangyoucao577/go-release-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ json2csv

Converts a stream of newline separated json data to csv format.

[![Build Status](https://travis-ci.org/jehiah/json2csv.png?branch=master)](https://travis-ci.org/jehiah/json2csv) [![GitHub release](https://img.shields.io/github/release/jehiah/json2csv.svg)](https://github.com/jehiah/json2csv/releases/latest)
![Build Status](https://github.com/jehiah/json2csv/actions/workflows/ci.yaml/badge.svg?branch=master)
[![GitHub release](https://img.shields.io/github/release/jehiah/json2csv.svg)](https://github.com/jehiah/json2csv/releases/latest)


Installation
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/jehiah/json2csv

go 1.13
go 1.20

require github.com/stretchr/testify v1.4.0
require github.com/stretchr/testify v1.11.1

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
13 changes: 6 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26 changes: 19 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ func get_value(data map[string]interface{}, keyparts []string) string {
subdata, _ := data[keyparts[0]].(map[string]interface{})
return get_value(subdata, keyparts[1:])
} else if v, ok := data[keyparts[0]]; ok {
switch v.(type) {
switch v := v.(type) {
case nil:
return ""
case float64:
f, _ := v.(float64)
if math.Mod(f, 1.0) == 0.0 {
return fmt.Sprintf("%d", int(f))
if math.Mod(v, 1.0) == 0.0 {
return fmt.Sprintf("%d", int(v))
} else {
return fmt.Sprintf("%f", f)
return fmt.Sprintf("%f", v)
}
default:
return fmt.Sprintf("%+v", v)
Expand Down Expand Up @@ -114,7 +113,11 @@ func json2csv(r LineReader, w *csv.Writer, keys []string, printHeader bool) {
}

if printHeader {
w.Write(keys)
err = w.Write(keys)
if err != nil {
log.Printf("ERROR Writing header: %s", err)
break
}
w.Flush()
printHeader = false
}
Expand All @@ -131,7 +134,16 @@ func json2csv(r LineReader, w *csv.Writer, keys []string, printHeader bool) {
record = append(record, get_value(data, expanded_key))
}

w.Write(record)
err = w.Write(record)
if err != nil {
log.Printf("ERROR Writing record: %s", err)
break
}
w.Flush()
err = w.Error()
if err != nil {
log.Printf("ERROR Flushing record: %s", err)
break
}
}
}
17 changes: 0 additions & 17 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ package main
import (
"bytes"
"encoding/csv"
"io/ioutil"
"log"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetTopic(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)

reader := bytes.NewBufferString(`{"a": 1, "b": "asdf\n"}
{"a" : null}`)
buf := bytes.NewBuffer([]byte{})
Expand All @@ -27,9 +21,6 @@ func TestGetTopic(t *testing.T) {
}

func TestGetLargeInt(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)

reader := bytes.NewBufferString(`{"a": 1356998399}`)
buf := bytes.NewBuffer([]byte{})
writer := csv.NewWriter(buf)
Expand All @@ -41,8 +32,6 @@ func TestGetLargeInt(t *testing.T) {
}

func TestGetFloat(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)

reader := bytes.NewBufferString(`{"a": 1356998399.32}`)
buf := bytes.NewBuffer([]byte{})
Expand All @@ -55,9 +44,6 @@ func TestGetFloat(t *testing.T) {
}

func TestGetNested(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)

reader := bytes.NewBufferString(`{"a": {"b": "asdf"}}`)
buf := bytes.NewBuffer([]byte{})
writer := csv.NewWriter(buf)
Expand All @@ -69,9 +55,6 @@ func TestGetNested(t *testing.T) {
}

func TestHeader(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)

reader := bytes.NewBufferString(`{"a": "b"}
{"a": "c"}`)
buf := bytes.NewBuffer([]byte{})
Expand Down