You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

654 lines
17 KiB

  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2016 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto
  32. import (
  33. "fmt"
  34. "reflect"
  35. "strings"
  36. "sync"
  37. "sync/atomic"
  38. )
  39. // Merge merges the src message into dst.
  40. // This assumes that dst and src of the same type and are non-nil.
  41. func (a *InternalMessageInfo) Merge(dst, src Message) {
  42. mi := atomicLoadMergeInfo(&a.merge)
  43. if mi == nil {
  44. mi = getMergeInfo(reflect.TypeOf(dst).Elem())
  45. atomicStoreMergeInfo(&a.merge, mi)
  46. }
  47. mi.merge(toPointer(&dst), toPointer(&src))
  48. }
  49. type mergeInfo struct {
  50. typ reflect.Type
  51. initialized int32 // 0: only typ is valid, 1: everything is valid
  52. lock sync.Mutex
  53. fields []mergeFieldInfo
  54. unrecognized field // Offset of XXX_unrecognized
  55. }
  56. type mergeFieldInfo struct {
  57. field field // Offset of field, guaranteed to be valid
  58. // isPointer reports whether the value in the field is a pointer.
  59. // This is true for the following situations:
  60. // * Pointer to struct
  61. // * Pointer to basic type (proto2 only)
  62. // * Slice (first value in slice header is a pointer)
  63. // * String (first value in string header is a pointer)
  64. isPointer bool
  65. // basicWidth reports the width of the field assuming that it is directly
  66. // embedded in the struct (as is the case for basic types in proto3).
  67. // The possible values are:
  68. // 0: invalid
  69. // 1: bool
  70. // 4: int32, uint32, float32
  71. // 8: int64, uint64, float64
  72. basicWidth int
  73. // Where dst and src are pointers to the types being merged.
  74. merge func(dst, src pointer)
  75. }
  76. var (
  77. mergeInfoMap = map[reflect.Type]*mergeInfo{}
  78. mergeInfoLock sync.Mutex
  79. )
  80. func getMergeInfo(t reflect.Type) *mergeInfo {
  81. mergeInfoLock.Lock()
  82. defer mergeInfoLock.Unlock()
  83. mi := mergeInfoMap[t]
  84. if mi == nil {
  85. mi = &mergeInfo{typ: t}
  86. mergeInfoMap[t] = mi
  87. }
  88. return mi
  89. }
  90. // merge merges src into dst assuming they are both of type *mi.typ.
  91. func (mi *mergeInfo) merge(dst, src pointer) {
  92. if dst.isNil() {
  93. panic("proto: nil destination")
  94. }
  95. if src.isNil() {
  96. return // Nothing to do.
  97. }
  98. if atomic.LoadInt32(&mi.initialized) == 0 {
  99. mi.computeMergeInfo()
  100. }
  101. for _, fi := range mi.fields {
  102. sfp := src.offset(fi.field)
  103. // As an optimization, we can avoid the merge function call cost
  104. // if we know for sure that the source will have no effect
  105. // by checking if it is the zero value.
  106. if unsafeAllowed {
  107. if fi.isPointer && sfp.getPointer().isNil() { // Could be slice or string
  108. continue
  109. }
  110. if fi.basicWidth > 0 {
  111. switch {
  112. case fi.basicWidth == 1 && !*sfp.toBool():
  113. continue
  114. case fi.basicWidth == 4 && *sfp.toUint32() == 0:
  115. continue
  116. case fi.basicWidth == 8 && *sfp.toUint64() == 0:
  117. continue
  118. }
  119. }
  120. }
  121. dfp := dst.offset(fi.field)
  122. fi.merge(dfp, sfp)
  123. }
  124. // TODO: Make this faster?
  125. out := dst.asPointerTo(mi.typ).Elem()
  126. in := src.asPointerTo(mi.typ).Elem()
  127. if emIn, err := extendable(in.Addr().Interface()); err == nil {
  128. emOut, _ := extendable(out.Addr().Interface())
  129. mIn, muIn := emIn.extensionsRead()
  130. if mIn != nil {
  131. mOut := emOut.extensionsWrite()
  132. muIn.Lock()
  133. mergeExtension(mOut, mIn)
  134. muIn.Unlock()
  135. }
  136. }
  137. if mi.unrecognized.IsValid() {
  138. if b := *src.offset(mi.unrecognized).toBytes(); len(b) > 0 {
  139. *dst.offset(mi.unrecognized).toBytes() = append([]byte(nil), b...)
  140. }
  141. }
  142. }
  143. func (mi *mergeInfo) computeMergeInfo() {
  144. mi.lock.Lock()
  145. defer mi.lock.Unlock()
  146. if mi.initialized != 0 {
  147. return
  148. }
  149. t := mi.typ
  150. n := t.NumField()
  151. props := GetProperties(t)
  152. for i := 0; i < n; i++ {
  153. f := t.Field(i)
  154. if strings.HasPrefix(f.Name, "XXX_") {
  155. continue
  156. }
  157. mfi := mergeFieldInfo{field: toField(&f)}
  158. tf := f.Type
  159. // As an optimization, we can avoid the merge function call cost
  160. // if we know for sure that the source will have no effect
  161. // by checking if it is the zero value.
  162. if unsafeAllowed {
  163. switch tf.Kind() {
  164. case reflect.Ptr, reflect.Slice, reflect.String:
  165. // As a special case, we assume slices and strings are pointers
  166. // since we know that the first field in the SliceSlice or
  167. // StringHeader is a data pointer.
  168. mfi.isPointer = true
  169. case reflect.Bool:
  170. mfi.basicWidth = 1
  171. case reflect.Int32, reflect.Uint32, reflect.Float32:
  172. mfi.basicWidth = 4
  173. case reflect.Int64, reflect.Uint64, reflect.Float64:
  174. mfi.basicWidth = 8
  175. }
  176. }
  177. // Unwrap tf to get at its most basic type.
  178. var isPointer, isSlice bool
  179. if tf.Kind() == reflect.Slice && tf.Elem().Kind() != reflect.Uint8 {
  180. isSlice = true
  181. tf = tf.Elem()
  182. }
  183. if tf.Kind() == reflect.Ptr {
  184. isPointer = true
  185. tf = tf.Elem()
  186. }
  187. if isPointer && isSlice && tf.Kind() != reflect.Struct {
  188. panic("both pointer and slice for basic type in " + tf.Name())
  189. }
  190. switch tf.Kind() {
  191. case reflect.Int32:
  192. switch {
  193. case isSlice: // E.g., []int32
  194. mfi.merge = func(dst, src pointer) {
  195. // NOTE: toInt32Slice is not defined (see pointer_reflect.go).
  196. /*
  197. sfsp := src.toInt32Slice()
  198. if *sfsp != nil {
  199. dfsp := dst.toInt32Slice()
  200. *dfsp = append(*dfsp, *sfsp...)
  201. if *dfsp == nil {
  202. *dfsp = []int64{}
  203. }
  204. }
  205. */
  206. sfs := src.getInt32Slice()
  207. if sfs != nil {
  208. dfs := dst.getInt32Slice()
  209. dfs = append(dfs, sfs...)
  210. if dfs == nil {
  211. dfs = []int32{}
  212. }
  213. dst.setInt32Slice(dfs)
  214. }
  215. }
  216. case isPointer: // E.g., *int32
  217. mfi.merge = func(dst, src pointer) {
  218. // NOTE: toInt32Ptr is not defined (see pointer_reflect.go).
  219. /*
  220. sfpp := src.toInt32Ptr()
  221. if *sfpp != nil {
  222. dfpp := dst.toInt32Ptr()
  223. if *dfpp == nil {
  224. *dfpp = Int32(**sfpp)
  225. } else {
  226. **dfpp = **sfpp
  227. }
  228. }
  229. */
  230. sfp := src.getInt32Ptr()
  231. if sfp != nil {
  232. dfp := dst.getInt32Ptr()
  233. if dfp == nil {
  234. dst.setInt32Ptr(*sfp)
  235. } else {
  236. *dfp = *sfp
  237. }
  238. }
  239. }
  240. default: // E.g., int32
  241. mfi.merge = func(dst, src pointer) {
  242. if v := *src.toInt32(); v != 0 {
  243. *dst.toInt32() = v
  244. }
  245. }
  246. }
  247. case reflect.Int64:
  248. switch {
  249. case isSlice: // E.g., []int64
  250. mfi.merge = func(dst, src pointer) {
  251. sfsp := src.toInt64Slice()
  252. if *sfsp != nil {
  253. dfsp := dst.toInt64Slice()
  254. *dfsp = append(*dfsp, *sfsp...)
  255. if *dfsp == nil {
  256. *dfsp = []int64{}
  257. }
  258. }
  259. }
  260. case isPointer: // E.g., *int64
  261. mfi.merge = func(dst, src pointer) {
  262. sfpp := src.toInt64Ptr()
  263. if *sfpp != nil {
  264. dfpp := dst.toInt64Ptr()
  265. if *dfpp == nil {
  266. *dfpp = Int64(**sfpp)
  267. } else {
  268. **dfpp = **sfpp
  269. }
  270. }
  271. }
  272. default: // E.g., int64
  273. mfi.merge = func(dst, src pointer) {
  274. if v := *src.toInt64(); v != 0 {
  275. *dst.toInt64() = v
  276. }
  277. }
  278. }
  279. case reflect.Uint32:
  280. switch {
  281. case isSlice: // E.g., []uint32
  282. mfi.merge = func(dst, src pointer) {
  283. sfsp := src.toUint32Slice()
  284. if *sfsp != nil {
  285. dfsp := dst.toUint32Slice()
  286. *dfsp = append(*dfsp, *sfsp...)
  287. if *dfsp == nil {
  288. *dfsp = []uint32{}
  289. }
  290. }
  291. }
  292. case isPointer: // E.g., *uint32
  293. mfi.merge = func(dst, src pointer) {
  294. sfpp := src.toUint32Ptr()
  295. if *sfpp != nil {
  296. dfpp := dst.toUint32Ptr()
  297. if *dfpp == nil {
  298. *dfpp = Uint32(**sfpp)
  299. } else {
  300. **dfpp = **sfpp
  301. }
  302. }
  303. }
  304. default: // E.g., uint32
  305. mfi.merge = func(dst, src pointer) {
  306. if v := *src.toUint32(); v != 0 {
  307. *dst.toUint32() = v
  308. }
  309. }
  310. }
  311. case reflect.Uint64:
  312. switch {
  313. case isSlice: // E.g., []uint64
  314. mfi.merge = func(dst, src pointer) {
  315. sfsp := src.toUint64Slice()
  316. if *sfsp != nil {
  317. dfsp := dst.toUint64Slice()
  318. *dfsp = append(*dfsp, *sfsp...)
  319. if *dfsp == nil {
  320. *dfsp = []uint64{}
  321. }
  322. }
  323. }
  324. case isPointer: // E.g., *uint64
  325. mfi.merge = func(dst, src pointer) {
  326. sfpp := src.toUint64Ptr()
  327. if *sfpp != nil {
  328. dfpp := dst.toUint64Ptr()
  329. if *dfpp == nil {
  330. *dfpp = Uint64(**sfpp)
  331. } else {
  332. **dfpp = **sfpp
  333. }
  334. }
  335. }
  336. default: // E.g., uint64
  337. mfi.merge = func(dst, src pointer) {
  338. if v := *src.toUint64(); v != 0 {
  339. *dst.toUint64() = v
  340. }
  341. }
  342. }
  343. case reflect.Float32:
  344. switch {
  345. case isSlice: // E.g., []float32
  346. mfi.merge = func(dst, src pointer) {
  347. sfsp := src.toFloat32Slice()
  348. if *sfsp != nil {
  349. dfsp := dst.toFloat32Slice()
  350. *dfsp = append(*dfsp, *sfsp...)
  351. if *dfsp == nil {
  352. *dfsp = []float32{}
  353. }
  354. }
  355. }
  356. case isPointer: // E.g., *float32
  357. mfi.merge = func(dst, src pointer) {
  358. sfpp := src.toFloat32Ptr()
  359. if *sfpp != nil {
  360. dfpp := dst.toFloat32Ptr()
  361. if *dfpp == nil {
  362. *dfpp = Float32(**sfpp)
  363. } else {
  364. **dfpp = **sfpp
  365. }
  366. }
  367. }
  368. default: // E.g., float32
  369. mfi.merge = func(dst, src pointer) {
  370. if v := *src.toFloat32(); v != 0 {
  371. *dst.toFloat32() = v
  372. }
  373. }
  374. }
  375. case reflect.Float64:
  376. switch {
  377. case isSlice: // E.g., []float64
  378. mfi.merge = func(dst, src pointer) {
  379. sfsp := src.toFloat64Slice()
  380. if *sfsp != nil {
  381. dfsp := dst.toFloat64Slice()
  382. *dfsp = append(*dfsp, *sfsp...)
  383. if *dfsp == nil {
  384. *dfsp = []float64{}
  385. }
  386. }
  387. }
  388. case isPointer: // E.g., *float64
  389. mfi.merge = func(dst, src pointer) {
  390. sfpp := src.toFloat64Ptr()
  391. if *sfpp != nil {
  392. dfpp := dst.toFloat64Ptr()
  393. if *dfpp == nil {
  394. *dfpp = Float64(**sfpp)
  395. } else {
  396. **dfpp = **sfpp
  397. }
  398. }
  399. }
  400. default: // E.g., float64
  401. mfi.merge = func(dst, src pointer) {
  402. if v := *src.toFloat64(); v != 0 {
  403. *dst.toFloat64() = v
  404. }
  405. }
  406. }
  407. case reflect.Bool:
  408. switch {
  409. case isSlice: // E.g., []bool
  410. mfi.merge = func(dst, src pointer) {
  411. sfsp := src.toBoolSlice()
  412. if *sfsp != nil {
  413. dfsp := dst.toBoolSlice()
  414. *dfsp = append(*dfsp, *sfsp...)
  415. if *dfsp == nil {
  416. *dfsp = []bool{}
  417. }
  418. }
  419. }
  420. case isPointer: // E.g., *bool
  421. mfi.merge = func(dst, src pointer) {
  422. sfpp := src.toBoolPtr()
  423. if *sfpp != nil {
  424. dfpp := dst.toBoolPtr()
  425. if *dfpp == nil {
  426. *dfpp = Bool(**sfpp)
  427. } else {
  428. **dfpp = **sfpp
  429. }
  430. }
  431. }
  432. default: // E.g., bool
  433. mfi.merge = func(dst, src pointer) {
  434. if v := *src.toBool(); v {
  435. *dst.toBool() = v
  436. }
  437. }
  438. }
  439. case reflect.String:
  440. switch {
  441. case isSlice: // E.g., []string
  442. mfi.merge = func(dst, src pointer) {
  443. sfsp := src.toStringSlice()
  444. if *sfsp != nil {
  445. dfsp := dst.toStringSlice()
  446. *dfsp = append(*dfsp, *sfsp...)
  447. if *dfsp == nil {
  448. *dfsp = []string{}
  449. }
  450. }
  451. }
  452. case isPointer: // E.g., *string
  453. mfi.merge = func(dst, src pointer) {
  454. sfpp := src.toStringPtr()
  455. if *sfpp != nil {
  456. dfpp := dst.toStringPtr()
  457. if *dfpp == nil {
  458. *dfpp = String(**sfpp)
  459. } else {
  460. **dfpp = **sfpp
  461. }
  462. }
  463. }
  464. default: // E.g., string
  465. mfi.merge = func(dst, src pointer) {
  466. if v := *src.toString(); v != "" {
  467. *dst.toString() = v
  468. }
  469. }
  470. }
  471. case reflect.Slice:
  472. isProto3 := props.Prop[i].proto3
  473. switch {
  474. case isPointer:
  475. panic("bad pointer in byte slice case in " + tf.Name())
  476. case tf.Elem().Kind() != reflect.Uint8:
  477. panic("bad element kind in byte slice case in " + tf.Name())
  478. case isSlice: // E.g., [][]byte
  479. mfi.merge = func(dst, src pointer) {
  480. sbsp := src.toBytesSlice()
  481. if *sbsp != nil {
  482. dbsp := dst.toBytesSlice()
  483. for _, sb := range *sbsp {
  484. if sb == nil {
  485. *dbsp = append(*dbsp, nil)
  486. } else {
  487. *dbsp = append(*dbsp, append([]byte{}, sb...))
  488. }
  489. }
  490. if *dbsp == nil {
  491. *dbsp = [][]byte{}
  492. }
  493. }
  494. }
  495. default: // E.g., []byte
  496. mfi.merge = func(dst, src pointer) {
  497. sbp := src.toBytes()
  498. if *sbp != nil {
  499. dbp := dst.toBytes()
  500. if !isProto3 || len(*sbp) > 0 {
  501. *dbp = append([]byte{}, *sbp...)
  502. }
  503. }
  504. }
  505. }
  506. case reflect.Struct:
  507. switch {
  508. case !isPointer:
  509. panic(fmt.Sprintf("message field %s without pointer", tf))
  510. case isSlice: // E.g., []*pb.T
  511. mi := getMergeInfo(tf)
  512. mfi.merge = func(dst, src pointer) {
  513. sps := src.getPointerSlice()
  514. if sps != nil {
  515. dps := dst.getPointerSlice()
  516. for _, sp := range sps {
  517. var dp pointer
  518. if !sp.isNil() {
  519. dp = valToPointer(reflect.New(tf))
  520. mi.merge(dp, sp)
  521. }
  522. dps = append(dps, dp)
  523. }
  524. if dps == nil {
  525. dps = []pointer{}
  526. }
  527. dst.setPointerSlice(dps)
  528. }
  529. }
  530. default: // E.g., *pb.T
  531. mi := getMergeInfo(tf)
  532. mfi.merge = func(dst, src pointer) {
  533. sp := src.getPointer()
  534. if !sp.isNil() {
  535. dp := dst.getPointer()
  536. if dp.isNil() {
  537. dp = valToPointer(reflect.New(tf))
  538. dst.setPointer(dp)
  539. }
  540. mi.merge(dp, sp)
  541. }
  542. }
  543. }
  544. case reflect.Map:
  545. switch {
  546. case isPointer || isSlice:
  547. panic("bad pointer or slice in map case in " + tf.Name())
  548. default: // E.g., map[K]V
  549. mfi.merge = func(dst, src pointer) {
  550. sm := src.asPointerTo(tf).Elem()
  551. if sm.Len() == 0 {
  552. return
  553. }
  554. dm := dst.asPointerTo(tf).Elem()
  555. if dm.IsNil() {
  556. dm.Set(reflect.MakeMap(tf))
  557. }
  558. switch tf.Elem().Kind() {
  559. case reflect.Ptr: // Proto struct (e.g., *T)
  560. for _, key := range sm.MapKeys() {
  561. val := sm.MapIndex(key)
  562. val = reflect.ValueOf(Clone(val.Interface().(Message)))
  563. dm.SetMapIndex(key, val)
  564. }
  565. case reflect.Slice: // E.g. Bytes type (e.g., []byte)
  566. for _, key := range sm.MapKeys() {
  567. val := sm.MapIndex(key)
  568. val = reflect.ValueOf(append([]byte{}, val.Bytes()...))
  569. dm.SetMapIndex(key, val)
  570. }
  571. default: // Basic type (e.g., string)
  572. for _, key := range sm.MapKeys() {
  573. val := sm.MapIndex(key)
  574. dm.SetMapIndex(key, val)
  575. }
  576. }
  577. }
  578. }
  579. case reflect.Interface:
  580. // Must be oneof field.
  581. switch {
  582. case isPointer || isSlice:
  583. panic("bad pointer or slice in interface case in " + tf.Name())
  584. default: // E.g., interface{}
  585. // TODO: Make this faster?
  586. mfi.merge = func(dst, src pointer) {
  587. su := src.asPointerTo(tf).Elem()
  588. if !su.IsNil() {
  589. du := dst.asPointerTo(tf).Elem()
  590. typ := su.Elem().Type()
  591. if du.IsNil() || du.Elem().Type() != typ {
  592. du.Set(reflect.New(typ.Elem())) // Initialize interface if empty
  593. }
  594. sv := su.Elem().Elem().Field(0)
  595. if sv.Kind() == reflect.Ptr && sv.IsNil() {
  596. return
  597. }
  598. dv := du.Elem().Elem().Field(0)
  599. if dv.Kind() == reflect.Ptr && dv.IsNil() {
  600. dv.Set(reflect.New(sv.Type().Elem())) // Initialize proto message if empty
  601. }
  602. switch sv.Type().Kind() {
  603. case reflect.Ptr: // Proto struct (e.g., *T)
  604. Merge(dv.Interface().(Message), sv.Interface().(Message))
  605. case reflect.Slice: // E.g. Bytes type (e.g., []byte)
  606. dv.Set(reflect.ValueOf(append([]byte{}, sv.Bytes()...)))
  607. default: // Basic type (e.g., string)
  608. dv.Set(sv)
  609. }
  610. }
  611. }
  612. }
  613. default:
  614. panic(fmt.Sprintf("merger not found for type:%s", tf))
  615. }
  616. mi.fields = append(mi.fields, mfi)
  617. }
  618. mi.unrecognized = invalidField
  619. if f, ok := t.FieldByName("XXX_unrecognized"); ok {
  620. if f.Type != reflect.TypeOf([]byte{}) {
  621. panic("expected XXX_unrecognized to be of type []byte")
  622. }
  623. mi.unrecognized = toField(&f)
  624. }
  625. atomic.StoreInt32(&mi.initialized, 1)
  626. }