package ocaml-protoc-plugin

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file merge.ml

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
(** Merge a two values. Need to match on the spec to merge messages recursivly *)
let merge: type t v. (t, v) Spec.compound -> t -> t -> t = function
  | Spec.Basic (_field, _spec, default) ->
    (fun t t' -> match t' = default with true -> t | false -> t')
    (* The spec states that proto2 required fields must be transmitted exactly once.
       So merging these fields is not possible. The essentially means that you cannot merge
       proto2 messages containing required fields.
       In this implementation, we choose to ignore this, and adopt 'keep last'
    *)
  | Spec.Basic_req (_field, Message (module Message)) ->
    Message.merge
  | Spec.Basic_req (_field, _spec) -> fun _ t' -> t'
  | Spec.Basic_opt (_field, Message (module Message)) ->
    begin
      fun t t' ->
        match t, t' with
        | None, None -> None
        | Some t, None -> Some t
        | None, Some t -> Some t
        | Some t, Some t' -> Some (Message.merge t t')
    end
  | Spec.Basic_opt (_field, _spec) -> begin
      fun t -> function
        | Some _ as t' -> t'
        | None -> t
    end
  | Spec.Repeated (_field, _, _) -> List.append
  | Spec.Map (_field, _) -> List.append
  (* | Spec.Oneof _ when t' = `not_set -> t *)
  | Spec.Oneof _ -> failwith "Implementation is part of generated code"