-
Notifications
You must be signed in to change notification settings - Fork 9
/
typed_json.go
148 lines (121 loc) · 3.81 KB
/
typed_json.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* @brief JSON IPC Buffer support
*
* @file typed_json.go
*/
/* -----------------------------------------------------------------------------
* Enduro/X Middleware Platform for Distributed Transaction Processing
* Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
* Copyright (C) 2017-2019, Mavimax, Ltd. All Rights Reserved.
* This software is released under one of the following licenses:
* LGPL or Mavimax's license for commercial use.
* See LICENSE file for full text.
*
* C (as designed by Dennis Ritchie and later authors) language code is licensed
* under Enduro/X Modified GNU Affero General Public License, version 3.
* See LICENSE_C file for full text.
* -----------------------------------------------------------------------------
* LGPL license:
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License, version 3 as published
* by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 3
* for more details.
*
* You should have received a copy of the Lesser General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* -----------------------------------------------------------------------------
* A commercial use license is available from Mavimax, Ltd
* -----------------------------------------------------------------------------
*/
package atmi
/*
#cgo pkg-config: atmisrvinteg
#include <xatmi.h>
#include <string.h>
#include <stdlib.h>
#include <ubf.h>
*/
import "C"
import "unsafe"
//UBF Buffer
type TypedJSON struct {
Buf *ATMIBuf
}
//Return The ATMI buffer to caller
func (u *TypedJSON) GetBuf() *ATMIBuf {
return u.Buf
}
//Allocate new JSON buffer
//@param s - source string
func (ac *ATMICtx) NewJSON(b []byte) (*TypedJSON, ATMIError) {
var buf TypedJSON
c_val := C.CString(string(b))
defer C.free(unsafe.Pointer(c_val))
size := int64(C.strlen(c_val) + 1) /* 1 for EOS. */
if ptr, err := ac.TpAlloc("JSON", "", size); nil != err {
return nil, err
} else {
buf.Buf = ptr
C.strcpy(buf.Buf.C_ptr, c_val)
buf.Buf.TpSetCtxt(ac)
return &buf, nil
}
}
//Get the JSON Handler from ATMI Buffer
func (ac *ATMICtx) CastToJSON(abuf *ATMIBuf) (*TypedJSON, ATMIError) {
var buf TypedJSON
buf.Buf = abuf
return &buf, nil
}
//Get the string value out from buffer
//@return JSON value
func (j *TypedJSON) GetJSONText() string {
ret := C.GoString(j.Buf.C_ptr)
j.Buf.nop()
return ret
}
//Get JSON bytes..
func (j *TypedJSON) GetJSON() []byte {
ret := []byte(C.GoString(j.Buf.C_ptr))
j.Buf.nop()
return ret
}
//Set JSON bytes
func (j *TypedJSON) SetJSON(b []byte) ATMIError {
return j.SetJSONText(string(b))
}
//Set the string to the buffer
//@param str JSON value
func (j *TypedJSON) SetJSONText(gs string) ATMIError {
c_val := C.CString(gs)
defer C.free(unsafe.Pointer(c_val))
new_size := int64(C.strlen(c_val) + 1) /* 1 for EOS. */
if cur_size, err := j.Buf.Ctx.TpTypes(j.Buf, nil, nil); nil != err {
return err
} else {
if cur_size >= new_size {
C.strcpy(j.Buf.C_ptr, c_val)
} else if err := j.Buf.TpRealloc(new_size); nil != err {
return err
} else {
C.strcpy(j.Buf.C_ptr, c_val)
}
}
j.Buf.nop()
return nil
}
///////////////////////////////////////////////////////////////////////////////////
// Wrappers for memory management
///////////////////////////////////////////////////////////////////////////////////
func (u *TypedJSON) TpRealloc(size int64) ATMIError {
return u.Buf.TpRealloc(size)
}
/* vim: set ts=4 sw=4 et smartindent: */