canoser 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README-CN.md +2 -2
- data/README.md +4 -6
- data/lib/canoser/struct.rb +5 -13
- data/lib/canoser/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd5d2302b41f369cd2aa3f7bbb4602735fd2be0e3523347ed438da306e224cac
|
4
|
+
data.tar.gz: 5dc3bc174725cc60ec2a03841841a583701ead4358547750b44a865e1d2570ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ac39e842339805ae312a43731673aa22e1540466966ca1d47b85382df34c6019a4d7db291bcd3fa92cdc23ff3601fe2829ae029b7bd26e862c646db92961c72
|
7
|
+
data.tar.gz: 26dae443e65e75f56fa582dbb5ea917cd16f46ed1ec7a5bba339d77db237e1546f973946abd60ed0a207899ea369a1a528f5581374c108d3d8034aa9fc360ee4
|
data/README-CN.md
CHANGED
@@ -145,10 +145,10 @@ bytes = obj.serialize
|
|
145
145
|
obj = AccountResource.deserialize(bytes)
|
146
146
|
```
|
147
147
|
### 从Struct对象中读取字段的值
|
148
|
-
对于所有通过define_field方法定义的字段,可以通过
|
148
|
+
对于所有通过define_field方法定义的字段,可以通过field_name获取该字段的值。比如:
|
149
149
|
|
150
150
|
```ruby
|
151
|
-
obj
|
151
|
+
obj.authentication_key
|
152
152
|
```
|
153
153
|
|
154
154
|
|
data/README.md
CHANGED
@@ -150,17 +150,15 @@ After defining Canoser::Struct, you don't need to implement serialization and de
|
|
150
150
|
obj = AccountResource.new(authentication_key:[...],...)
|
151
151
|
bytes = obj.serialize
|
152
152
|
|
153
|
-
# deserialize an object
|
153
|
+
# deserialize an object from bytes
|
154
154
|
obj = AccountResource.deserialize(bytes)
|
155
155
|
```
|
156
156
|
|
157
|
-
### Get field value
|
158
|
-
For all fields defined by the "define_field" method, the value of this field of an object can be obtained via
|
157
|
+
### Get field value from object
|
158
|
+
For all fields defined by the "define_field" method, the value of this field of an object can be obtained via field_name. such as:
|
159
159
|
|
160
160
|
```ruby
|
161
|
-
obj
|
162
|
-
#or
|
163
|
-
obj["authentication_key"]
|
161
|
+
obj.authentication_key
|
164
162
|
```
|
165
163
|
|
166
164
|
|
data/lib/canoser/struct.rb
CHANGED
@@ -16,12 +16,12 @@ module Canoser
|
|
16
16
|
@@names << #{name}
|
17
17
|
@@types ||= []
|
18
18
|
@@types << type
|
19
|
+
attr_accessor(#{name})
|
19
20
|
}
|
20
21
|
class_eval str
|
21
22
|
end
|
22
23
|
|
23
24
|
def initialize(hash={})
|
24
|
-
@values = {}
|
25
25
|
hash.each do |k,v|
|
26
26
|
idx = self.class.class_variable_get("@@names").find_index{|x| x==k}
|
27
27
|
raise "#{k} is not a field of #{self}" unless idx
|
@@ -30,22 +30,14 @@ module Canoser
|
|
30
30
|
len = type.fixed_len
|
31
31
|
raise "fix-length array #{k}: #{len} != #{v.size}" if len && v.size != len
|
32
32
|
end
|
33
|
-
|
33
|
+
instance_variable_set("@#{k}", v)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
def [](name)
|
38
|
-
@values[name.to_sym]
|
39
|
-
end
|
40
|
-
|
41
|
-
def []=(name, value)
|
42
|
-
@values[name.to_sym] = value
|
43
|
-
end
|
44
|
-
|
45
37
|
def ==(other)
|
46
38
|
return true if self.equal?(other)
|
47
39
|
self.class.class_variable_get("@@names").each_with_index do |name, idx|
|
48
|
-
unless
|
40
|
+
unless instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}")
|
49
41
|
return false
|
50
42
|
end
|
51
43
|
end
|
@@ -60,7 +52,7 @@ module Canoser
|
|
60
52
|
output = ""
|
61
53
|
self.class.class_variable_get("@@names").each_with_index do |name, idx|
|
62
54
|
type = self.class.class_variable_get("@@types")[idx]
|
63
|
-
value =
|
55
|
+
value = instance_variable_get("@#{name}")
|
64
56
|
output << type.encode(value)
|
65
57
|
end
|
66
58
|
output
|
@@ -80,7 +72,7 @@ module Canoser
|
|
80
72
|
def decode(cursor)
|
81
73
|
self.class.class_variable_get("@@names").each_with_index do |name, idx|
|
82
74
|
type = self.class.class_variable_get("@@types")[idx]
|
83
|
-
|
75
|
+
instance_variable_set("@#{name}", type.decode(cursor))
|
84
76
|
end
|
85
77
|
self
|
86
78
|
end
|
data/lib/canoser/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canoser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yuan xinyu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|