-1

I have code which contains some nested array values:

var descriptor models.Descriptor
_ = json.NewDecoder(r.Body).Decode(&descriptor)
result, _ := collectionDescriptor.InsertOne(context.TODO(), descriptor)

type Descriptor struct {
    Id      string `json:"id" bson:"id"`
    Type    string `json:"type,omitempty" bson:"type,omitempty"`
    Name    string `json:"name,omitempty" bson:"name,omitempty"`
    Version string `json:"version,omitempty" bson:"version,omitempty"`
    Modules []string `json:"modules,omitempty" bson:"modules,omitempty"`
    Configs []Config `json:"configs" bson:"configs"`
    
}
type Config struct {
    Id       string `json:"id" bson:"id"`
    Type     string `json:"type,omitempty" bson:"type,omitempty"`
    Name     string `json:"name,omitempty" bson:"name,omitempty"`
    Protocol []Protocols `json:"protocol" bson:"protocol"`
}
type Protocols struct {
    Id    string `json:"id" bson:"id"`
    Type  string `json:"type,omitempty" bson:"type,omitempty"`
    Name  string `json:"name" bson:"name,omitempty"`
    Items []Itemes `json:"items" bson:"items"`
}

descriptor.Configs[0].Protocol[0] = nil

config = descriptor.Configs[0]
_ = json.NewDecoder(r.Body).Decode(&config)

conf, errr := collectionConfigDes.InsertOne(context.TODO(), config)
json.NewEncoder(w).Encode(conf)

I want to remove the value in descriptor.configs[0].protocol to be an empty array or an empty string. which I will then send to MongoDB. JSON form like below

this is an example of the result i need, please click here

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Synth Four
  • 21
  • 1
  • 5

2 Answers2

0

To an empty array:

Just use descriptor.Configs[0].Protocol[0] = make([]Protocols, 0) (not nil)

And be sure that bson without omitempty.

To empty string:

You can create a temp struct, to rewrite the type of Protocol.

Unfortunately bson can not use temp struct like json:

collection.InsertOne(ctx, struct {
    *Config
    Protocol string `json:"protocol"`
}{
    Config:  &descriptor.Configs[0],
    Protocol: "",
})

You should use like:

collection.InsertOne(m.ctx, struct {
    Id       string `bson:"id"`
    Type     string `bson:"type"`
    Name     string `bson:"name"`
    Protocol string `bson:"protocol"`
}{
    Id:       config.Id,
    Type:     config.Type,
    Name:     config.Name,
    Protocol: "",
})
Sangria
  • 134
  • 8
  • Thank you for the response, very helpful for project completion. by using "make([]Protocols, 0)", the array can be empty. – Synth Four Nov 25 '21 at 08:49
0

If I'm getting the problem statement correctly, you are trying to read the json data from response.Body, make a field nil/empty, and then insert into MongoDB. If that is what it is, then

  1. I'm not sure why you are making the field nil before decoding. It should be:
config := Descriptor{}
json.NewDecoder(r.Body).Decode(&desc)
config.Protocol[0] = nil

// or if decoding and encoding Descriptor or slice of it
// which is handy for updating the record in DB
desc := Config{}
json.NewDecoder(r.Body).Decode(&config)
desc[0].Configs[0].Protocol = nil
Before After
enter image description here enter image description here

Different approaches (on JSON) are,

1.1. Remove omitempty from Protocol field. This gives null in JSON.

Protocol []Protocols `json:"protocol" bson:"protocol"`

1.2. Remove omitempty like above; and use make as @Sangria mentioned. This gives empty array/slice in JSON.

desc[0].Configs[0].Protocol = make([]Protocols, 0)
Null Value Empty Array
enter image description here enter image description here

This works for JSON at least. Is there any issue to insert the same into Mongo? Try the 3 possibilities.

  1. Doesn't matter if you are storing the record as a new one. But in case of editing the existing, you should be using collection.UpdateOne() in Mongo.
vague
  • 409
  • 2
  • 5
  • Thank you for the response, very helpful for project completion. by using "make([]Protocols, 0)", the array can be empty. – Synth Four Nov 25 '21 at 08:48