[ovs-dev,13/13] json: New function json_object_put_format().

Message ID 20171007004458.5788-14-blp@ovn.org
State New
Headers show
Series
  • clustering implementation, part 1
Related show

Commit Message

Ben Pfaff Oct. 7, 2017, 12:44 a.m.
This will acquire users in an upcoming commit.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 include/openvswitch/json.h |  6 +++++-
 lib/json.c                 | 12 ++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

Comments

Russell Bryant Oct. 9, 2017, 8:08 p.m. | #1
On Fri, Oct 6, 2017 at 8:44 PM, Ben Pfaff <blp@ovn.org> wrote:
> This will acquire users in an upcoming commit.
>
> Signed-off-by: Ben Pfaff <blp@ovn.org>
> ---
>  include/openvswitch/json.h |  6 +++++-
>  lib/json.c                 | 12 ++++++++++++
>  2 files changed, 17 insertions(+), 1 deletion(-)

Acked-by: Russell Bryant <russell@ovn.org>

Is it required to put the printf function attribute in both json.h and
json.c?  I figure it's harmless, but wasn't sure if it was necessary.

> diff --git a/include/openvswitch/json.h b/include/openvswitch/json.h
> index edf53e594eb0..61b9a02cfc19 100644
> --- a/include/openvswitch/json.h
> +++ b/include/openvswitch/json.h
> @@ -1,5 +1,5 @@
>  /*
> - * Copyright (c) 2009, 2010, 2015 Nicira, Inc.
> + * Copyright (c) 2009, 2010, 2015, 2016 Nicira, Inc.
>   *
>   * Licensed under the Apache License, Version 2.0 (the "License");
>   * you may not use this file except in compliance with the License.
> @@ -38,6 +38,7 @@ extern "C" {
>  #endif
>
>  struct ds;
> +struct uuid;
>
>  /* Type of a JSON value. */
>  enum json_type {
> @@ -92,6 +93,9 @@ struct json *json_object_create(void);
>  void json_object_put(struct json *, const char *name, struct json *value);
>  void json_object_put_string(struct json *,
>                              const char *name, const char *value);
> +void json_object_put_format(struct json *,
> +                            const char *name, const char *format, ...)
> +    OVS_PRINTF_FORMAT(3, 4);
>
>  const char *json_string(const struct json *);
>  struct json_array *json_array(const struct json *);
> diff --git a/lib/json.c b/lib/json.c
> index b98e60f87f4b..5e93190b8a03 100644
> --- a/lib/json.c
> +++ b/lib/json.c
> @@ -29,6 +29,7 @@
>  #include "openvswitch/shash.h"
>  #include "unicode.h"
>  #include "util.h"
> +#include "uuid.h"
>
>  /* The type of a JSON token. */
>  enum json_token_type {
> @@ -284,6 +285,17 @@ json_object_put_string(struct json *json, const char *name, const char *value)
>      json_object_put(json, name, json_string_create(value));
>  }
>
> +void OVS_PRINTF_FORMAT(3, 4)
> +json_object_put_format(struct json *json,
> +                       const char *name, const char *format, ...)
> +{
> +    va_list args;
> +    va_start(args, format);
> +    json_object_put(json, name,
> +                    json_string_create_nocopy(xvasprintf(format, args)));
> +    va_end(args);
> +}
> +
>  const char *
>  json_string(const struct json *json)
>  {
> --
> 2.10.2
>
> _______________________________________________
> dev mailing list
> dev@openvswitch.org
> https://mail.openvswitch.org/mailman/listinfo/ovs-dev
Ben Pfaff Oct. 9, 2017, 8:18 p.m. | #2
On Mon, Oct 09, 2017 at 04:08:54PM -0400, Russell Bryant wrote:
> On Fri, Oct 6, 2017 at 8:44 PM, Ben Pfaff <blp@ovn.org> wrote:
> > This will acquire users in an upcoming commit.
> >
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> > ---
> >  include/openvswitch/json.h |  6 +++++-
> >  lib/json.c                 | 12 ++++++++++++
> >  2 files changed, 17 insertions(+), 1 deletion(-)
> 
> Acked-by: Russell Bryant <russell@ovn.org>

Thanks for the review.

> Is it required to put the printf function attribute in both json.h and
> json.c?  I figure it's harmless, but wasn't sure if it was necessary.

I don't know.  On a related issue, I've noticed that Clang forgets about
thread safety annotations if they're not in both places.  (Maybe that is
a bug, and maybe it's been fixed since I noticed it years ago.)

Patch

diff --git a/include/openvswitch/json.h b/include/openvswitch/json.h
index edf53e594eb0..61b9a02cfc19 100644
--- a/include/openvswitch/json.h
+++ b/include/openvswitch/json.h
@@ -1,5 +1,5 @@ 
 /*
- * Copyright (c) 2009, 2010, 2015 Nicira, Inc.
+ * Copyright (c) 2009, 2010, 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@  extern "C" {
 #endif
 
 struct ds;
+struct uuid;
 
 /* Type of a JSON value. */
 enum json_type {
@@ -92,6 +93,9 @@  struct json *json_object_create(void);
 void json_object_put(struct json *, const char *name, struct json *value);
 void json_object_put_string(struct json *,
                             const char *name, const char *value);
+void json_object_put_format(struct json *,
+                            const char *name, const char *format, ...)
+    OVS_PRINTF_FORMAT(3, 4);
 
 const char *json_string(const struct json *);
 struct json_array *json_array(const struct json *);
diff --git a/lib/json.c b/lib/json.c
index b98e60f87f4b..5e93190b8a03 100644
--- a/lib/json.c
+++ b/lib/json.c
@@ -29,6 +29,7 @@ 
 #include "openvswitch/shash.h"
 #include "unicode.h"
 #include "util.h"
+#include "uuid.h"
 
 /* The type of a JSON token. */
 enum json_token_type {
@@ -284,6 +285,17 @@  json_object_put_string(struct json *json, const char *name, const char *value)
     json_object_put(json, name, json_string_create(value));
 }
 
+void OVS_PRINTF_FORMAT(3, 4)
+json_object_put_format(struct json *json,
+                       const char *name, const char *format, ...)
+{
+    va_list args;
+    va_start(args, format);
+    json_object_put(json, name,
+                    json_string_create_nocopy(xvasprintf(format, args)));
+    va_end(args);
+}
+
 const char *
 json_string(const struct json *json)
 {