I've managed to achieve what I was looking for with:

reflect.New(reflect.TypeOf(valuePtr).Elem()).Interface()


On Tue, Aug 2, 2016 at 8:55 PM, Nathan Fisher <nfis...@junctionbox.ca>
wrote:

> It sounds like you could do something similar-ish to the way sorting is
> handled in the sort package with interfaces. Some immediate concerns that
> jump out wrt a read and partial update that is done outside the data source:
>
> - partial update implies some form of locking (either optimistic,
> distributed, or row level).
> - full update will provide a last write wins semantic, whereas a partial
> update without locking could result in an undesirable when contended (can
> provide an example assuming it's not something you've considers).
>
> Also of note the example data model accommodates for empty values and not
> absent values. Nothing wrong just citing it as something you may or may not
> want to accommodate.
>
>
>
> On Tue, 2 Aug 2016 at 16:58, roger peppe <rogpe...@gmail.com> wrote:
>
>> On 2 August 2016 at 16:46, Guianu Leon <guianul...@gmail.com> wrote:
>> > Well I will only get the fields that have to be updated, not the entire
>> > object. Therefore I need to get the old object from the database, see
>> what
>> > has changed, then insert it back.
>>
>> If you're seeing only the fields that have to be updated, how
>> will you know that getting the object will get all the fields?
>>
>> For example, if I have this object in the database:
>>
>>     {"Age": 10, "Height": 6}
>>
>> and I do this:
>>
>>     type Person struct {
>>         Address string
>>     }
>>     db.Update("bob", &Person{Address: "12 Some Street"})
>>
>> then doing a Get into a value of type *Person won't get all the fields
>> that you need to update.
>>
>> I think you'll need a slightly cleverer implementation if you want to do
>> this.
>> Have you looked into the approach taken by gopkg.in/mgo.v2,
>> which addresses a similar problem?
>>
>>   cheers,
>>     rog.
>>
>> >
>> >
>> > On Aug 2, 2016 18:35, "roger peppe" <rogpe...@gmail.com> wrote:
>> >>
>> >> On 2 August 2016 at 09:58, Rayland <guianul...@gmail.com> wrote:
>> >> > I am working in the context of an ODM.
>> >> >
>> >> > I have an update method, like this:
>> >> >
>> >> > func Update(key string, valuePtr interface{}) error
>> >> >
>> >> >
>> >> > Inside of this method I need to do a Get in order to get the old
>> value,
>> >> > compare the modified fields and update them, then insert the new
>> object
>> >> > in
>> >> > the database.
>> >> >
>> >> > The problem is that inside the Update method I don't know what type
>> >> > valuePtr
>> >> > is in order to make a second object and feed to the Get method, and
>> then
>> >> > if
>> >> > I call the Get method with an interface{} object it will crash
>> because
>> >> > it
>> >> > can't Unmarshal (objects are kept as json in the database).
>> >>
>> >> FWIW Axel's solution can work well and I've use similar techniques in
>> >> places,
>> >> but I have sympathy for Jan's reservations. Why do you need to do a Get
>> >> before doing the Put - how can you tell which fields need to be
>> updated?
>> >> I can't immediately see how what you suggest would be different from
>> >> just updating the database with the value directly.
>> >>
>> >>   cheers,
>> >>     rog.
>> >>
>> >> >
>> >> > On Tuesday, August 2, 2016 at 11:26:01 AM UTC+3, Dave Cheney wrote:
>> >> >>
>> >> >> Hypothetically, if there was a syntax in Go that did this, what
>> would
>> >> >> you
>> >> >> do next ? In other words, what is the problem you are trying to
>> solve
>> >> >> that
>> >> >> has lead you to needing this feature?
>> >> >>
>> >> >> On Tuesday, 2 August 2016 18:18:05 UTC+10, Rayland wrote:
>> >> >>>
>> >> >>> I basically need something like this:
>> >> >>>
>> >> >>> var obj = &Person{Name: "John"} // I don't know what type obj will
>> be
>> >> >>> everytime
>> >> >>> var newObj interface{}
>> >> >>>
>> >> >>> initNewObj(obj) // now newObj is an empty object of type *Person
>> >> >>>
>> >> >>>
>> >> >>>
>> >> >>>
>> >> >>> On Tuesday, August 2, 2016 at 10:21:01 AM UTC+3, Dave Cheney wrote:
>> >> >>>>
>> >> >>>> TL;DR a shallow copy is easy, but it's almost never what you want.
>> >> >>>>
>> >> >>>> https://play.golang.org/p/wpWN3Znop8
>> >> >>>>
>> >> >>>> Needing to copy an arbitrary value is usually an anti pattern as
>> >> >>>> values
>> >> >>>> can contain references to other values, which contain references
>> to
>> >> >>>> other
>> >> >>>> values, which contain references to other values, sometimes
>> leading
>> >> >>>> back to
>> >> >>>> the previous values.
>> >> >>>>
>> >> >>>> On Tuesday, 2 August 2016 17:13:21 UTC+10, Rayland wrote:
>> >> >>>>>
>> >> >>>>> Let's say I have an object A of some type. Is there a way to
>> create
>> >> >>>>> a
>> >> >>>>> new object B with the same type as A without knowing the type of
>> A
>> >> >>>>> in
>> >> >>>>> advance?
>> >> >
>> >> > --
>> >> > You received this message because you are subscribed to the Google
>> >> > Groups
>> >> > "golang-nuts" group.
>> >> > To unsubscribe from this group and stop receiving emails from it,
>> send
>> >> > an
>> >> > email to golang-nuts+unsubscr...@googlegroups.com.
>> >> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> - from my thumbs to yours
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to