/
nswAllPearsonY5.go
110 lines (91 loc) · 2.63 KB
/
nswAllPearsonY5.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package reports
import (
"bufio"
"fmt"
"log"
"strconv"
"strings"
"github.com/nsip/dev-nrt/records"
"github.com/tidwall/sjson"
)
type NswAllPearsonY5 struct {
baseReport // embed common setup capability
}
//
// Pearson/ACARA fixed width encoding of domain item response correctness
//
func NswAllPearsonY5Report() *NswAllPearsonY5 {
r := NswAllPearsonY5{}
r.initialise("./config/NswAllPearsonY5.toml")
r.printStatus()
return &r
}
//
// implement the ...Pipe interface, core work of the
// report engine.
//
func (r *NswAllPearsonY5) ProcessStudentRecords(in chan *records.StudentOrientedRecord) chan *records.StudentOrientedRecord {
out := make(chan *records.StudentOrientedRecord)
go func() {
defer close(out)
// open the fixed format file writer - not a csv file
w := bufio.NewWriter(r.outF)
defer r.outF.Close()
defer w.Flush()
var result, paddedResult string
var length int
var convErr error
var row strings.Builder // single string record for this format
for sor := range in {
if !r.config.activated { // only process if activated
out <- sor
continue
}
if sor.GetValueString("CalculatedFields.YrLevel") != "5" { // only for yr 3
out <- sor
continue
}
//
// generate any calculated fields required
//
sor.CalculatedFields = r.calculateFields(sor)
//
// now loop through the ouput definitions to create a
// row of results
//
for i, query := range r.config.queries {
result = sor.GetValueString(query)
length, convErr = strconv.Atoi(r.config.header[i])
if convErr != nil {
log.Println("WARNING: unexpected value for field length in ", r.configFileName, r.config.header[i])
}
paddingToken := defaultPaddingToken
if r.config.header[i][0] == '0' {
paddingToken = zeroPaddingToken
}
paddedResult = PadLeft(result, length, paddingToken)
row.WriteString(paddedResult)
}
// write the row to the output file
if _, err := fmt.Fprintln(w, row.String()); err != nil {
fmt.Println("Warning: error writing record to output file:", r.config.name, err)
}
row.Reset()
out <- sor
}
}()
return out
}
//
// generates a block of json that can be added to the
// record containing values that are not in the original data
//
//
func (r *NswAllPearsonY5) calculateFields(sor *records.StudentOrientedRecord) []byte {
json := sor.CalculatedFields // keep any existing data
// create truncated format birthdate
fullDob := sor.GetValueString("StudentPersonal.PersonInfo.Demographics.BirthDate")
truncDob := strings.ReplaceAll(fullDob, "-", "")
json, _ = sjson.SetBytes(json, "CalculatedFields.TruncatedDOB", truncDob)
return json
}