@@ -16,6 +16,8 @@ struct ParseResult {
1616 char *Error;
1717 int ErrorCount;
1818 char **Errors;
19+ int WarningCount;
20+ char **Warnings;
1921};
2022
2123typedef uintptr_t ParserHandle;
@@ -50,9 +52,12 @@ func NewParser(disableNetwork C.bool, branch *C.char, baseURL *C.char) C.ParserH
5052//export ParseString
5153func ParseString (handle C.ParserHandle , content * C.char ) * C.struct_ParseResult {
5254 result := (* C .struct_ParseResult )(C .calloc (1 , C .size_t (C .sizeof_struct_ParseResult )))
55+ result .Data = nil
5356 result .Error = nil
54- result .Errors = nil
5557 result .ErrorCount = 0
58+ result .Errors = nil
59+ result .WarningCount = 0
60+ result .Warnings = nil
5661
5762 parser , err := toGoParser (handle )
5863 if err != nil {
@@ -67,31 +72,53 @@ func ParseString(handle C.ParserHandle, content *C.char) *C.struct_ParseResult {
6772
6873 if err != nil {
6974 if validationRes , ok := err .(publiccode.ValidationResults ); ok {
70- var ve []publiccode.ValidationError
7175 for _ , res := range validationRes {
72- switch v := res .(type ) {
76+ switch res .(type ) {
7377 case publiccode.ValidationError :
74- ve = append (ve , v )
78+ result .ErrorCount += 1
79+ case publiccode.ValidationWarning :
80+ result .WarningCount += 1
7581 }
7682 }
7783
78- errCount := len (ve )
79- result .ErrorCount = C .int (errCount )
84+ var errorsSlice []* C.char
85+ cErrors := unsafe .Pointer (nil )
86+ if result .ErrorCount > 0 {
87+ cErrors = C .malloc (C .size_t (result .ErrorCount ) * C .size_t (unsafe .Sizeof (uintptr (0 ))))
88+ errorsSlice = (* [1 << 28 ]* C.char )(cErrors )[:result .ErrorCount :result .ErrorCount ]
89+ }
8090
81- if errCount > 0 {
82- result .Error = C .CString (err .Error ())
83- cErrors := C .malloc (C .size_t (errCount ) * C .size_t (unsafe .Sizeof (uintptr (0 ))))
84- errorsSlice := (* [1 << 28 ]* C.char )(cErrors )[:errCount :errCount ]
91+ var warningsSlice []* C.char
92+ cWarnings := unsafe .Pointer (nil )
93+ if result .WarningCount > 0 {
94+ cWarnings = C .malloc (C .size_t (result .WarningCount ) * C .size_t (unsafe .Sizeof (uintptr (0 ))))
95+ warningsSlice = (* [1 << 28 ]* C.char )(cWarnings )[:result .WarningCount :result .WarningCount ]
96+ }
8597
86- for i , e := range ve {
87- errorsSlice [i ] = C .CString (e .Error ())
98+ errIdx := 0
99+ warnIdx := 0
100+ for _ , res := range validationRes {
101+ switch res .(type ) {
102+ case publiccode.ValidationError :
103+ errorsSlice [errIdx ] = C .CString (res .Error ())
104+ errIdx += 1
105+ case publiccode.ValidationWarning :
106+ warningsSlice [warnIdx ] = C .CString (res .Error ())
107+ warnIdx += 1
88108 }
89-
90- result .Errors = (* * C .char )(cErrors )
91109 }
110+
111+ result .Errors = (* * C .char )(cErrors )
112+ result .Warnings = (* * C .char )(cWarnings )
113+ } else {
114+ result .Error = C .CString (err .Error ())
115+
116+ return result
92117 }
93118
94- return result
119+ if result .ErrorCount > 0 {
120+ return result
121+ }
95122 }
96123
97124 jsonData , err := json .Marshal (pc )
@@ -132,6 +159,19 @@ func FreeResult(result *C.struct_ParseResult) {
132159 result .Errors = nil
133160 result .ErrorCount = 0
134161 }
162+ if result .Warnings != nil && result .WarningCount > 0 {
163+ warningsSlice := unsafe .Slice ((* * C .char )(result .Warnings ), result .WarningCount )
164+ for i := 0 ; i < int (result .WarningCount ); i ++ {
165+ if warningsSlice [i ] != nil {
166+ C .free (unsafe .Pointer (warningsSlice [i ]))
167+ warningsSlice [i ] = nil
168+ }
169+ }
170+ C .free (unsafe .Pointer (result .Warnings ))
171+ result .Warnings = nil
172+ result .WarningCount = 0
173+ }
174+
135175}
136176
137177func toGoParser (handle C.ParserHandle ) (* publiccode.Parser , error ) {
0 commit comments